]> git.mxchange.org Git - friendica.git/commitdiff
Refactor user console command to re-use common code
authorMatthew Exon <git.mexon@spamgourmet.com>
Sun, 25 Apr 2021 11:30:51 +0000 (13:30 +0200)
committerMatthew Exon <git.mexon@spamgourmet.com>
Sun, 25 Apr 2021 17:27:10 +0000 (19:27 +0200)
src/Console/User.php

index a9378a61e957e1ed043b2999c965cdd2981d76c4..89a9ad6032bc3154480917319d00f8c440ca1c95 100644 (file)
@@ -137,21 +137,59 @@ HELP;
        }
 
        /**
-        * Sets a new password
+        * Retrieves the user nick, either as an argument or from a prompt
         *
-        * @return int Return code of this command
+        * @param int $arg_index Index of the nick argument in the arguments list
         *
-        * @throws \Exception
+        * @return string nick of the user
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private function password()
+       private function getNick($arg_index)
        {
-               $nick = $this->getArgument(1);
+               $nick = $this->getArgument($arg_index);
+
+               if (!$nick) {
+                       $this->out($this->l10n->t('Enter user nickname: '));
+                       $nick = CliPrompt::prompt();
+                       if (empty($nick)) {
+                               throw new RuntimeException('A nick name must be set.');
+                       }
+               }
+
+               return $nick;
+       }
+
+       /**
+        * Retrieves the user from a nick supplied as an argument or from a prompt
+        *
+        * @param int $arg_index Index of the nick argument in the arguments list
+        *
+        * @return mixed user data or dba failure result
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       private function getUserByNick($arg_index)
+       {
+               $nick = $this->getNick($arg_index);
 
                $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
                if (!$this->dba->isResult($user)) {
                        throw new RuntimeException($this->l10n->t('User not found'));
                }
 
+               return $user;
+       }
+
+       /**
+        * Sets a new password
+        *
+        * @return int Return code of this command
+        *
+        * @throws \Exception
+        */
+       private function password()
+       {
+               $user = $this->getUserByNick(1);
+
                $password = $this->getArgument(2);
 
                if (is_null($password)) {
@@ -235,20 +273,7 @@ HELP;
         */
        private function pendingUser(bool $allow = true)
        {
-               $nick = $this->getArgument(1);
-
-               if (!$nick) {
-                       $this->out($this->l10n->t('Enter user nickname: '));
-                       $nick = CliPrompt::prompt();
-                       if (empty($nick)) {
-                               throw new RuntimeException('A nick name must be set.');
-                       }
-               }
-
-               $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
-               if (empty($user)) {
-                       throw new RuntimeException($this->l10n->t('User not found'));
-               }
+               $user = $this->getUserByNick(1);
 
                $pending = Register::getPendingForUser($user['uid'] ?? 0);
                if (empty($pending)) {
@@ -268,20 +293,7 @@ HELP;
         */
        private function blockUser(bool $block = true)
        {
-               $nick = $this->getArgument(1);
-
-               if (!$nick) {
-                       $this->out($this->l10n->t('Enter user nickname: '));
-                       $nick = CliPrompt::prompt();
-                       if (empty($nick)) {
-                               throw new RuntimeException('A nick name must be set.');
-                       }
-               }
-
-               $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
-               if (empty($user)) {
-                       throw new RuntimeException($this->l10n->t('User not found'));
-               }
+               $user = $this->getUserByNick(1);
 
                return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
        }
@@ -294,20 +306,7 @@ HELP;
         */
        private function deleteUser()
        {
-               $nick = $this->getArgument(1);
-
-               if (!$nick) {
-                       $this->out($this->l10n->t('Enter user nickname: '));
-                       $nick = CliPrompt::prompt();
-                       if (empty($nick)) {
-                               throw new RuntimeException('A nick name must be set.');
-                       }
-               }
-
-               $user = $this->dba->selectFirst('user', ['uid', 'account_removed'], ['nickname' => $nick]);
-               if (empty($user)) {
-                       throw new RuntimeException($this->l10n->t('User not found'));
-               }
+               $user = $this->getUserByNick(1);
 
                if (!empty($user['account_removed'])) {
                        $this->out($this->l10n->t('User has already been marked for deletion.'));
@@ -315,7 +314,7 @@ HELP;
                }
 
                if (!$this->getOption('y')) {
-                       $this->out($this->l10n->t('Type "yes" to delete %s', $nick));
+                       $this->out($this->l10n->t('Type "yes" to delete %s', $this->getArgument(1)));
                        if (CliPrompt::prompt() !== 'yes') {
                                throw new RuntimeException($this->l10n->t('Deletion aborted.'));
                        }