Conditions | 16 |
Paths | 19 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
105 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
106 | $username = $input->getArgument('user'); |
||
107 | $emailLink = $input->getOption('send-email'); |
||
108 | $displayLink = $input->getOption('output-link'); |
||
109 | |||
110 | /** @var $user \OCP\IUser */ |
||
111 | $user = $this->userManager->get($username); |
||
112 | if ($user === null) { |
||
113 | $output->writeln('<error>User does not exist</error>'); |
||
114 | return 1; |
||
115 | } |
||
116 | |||
117 | if ($input->getOption('password-from-env')) { |
||
118 | $password = $this->environmentHelper->getEnvVar('OC_PASS'); |
||
119 | if (!$password) { |
||
120 | $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
||
121 | return 1; |
||
122 | } |
||
123 | } elseif ($emailLink || $displayLink) { |
||
124 | $userId = $user->getUID(); |
||
125 | list($link, $token) = $this->lostController->generateTokenAndLink($userId); |
||
126 | |||
127 | if ($emailLink && $this->hasValidEmailAddress($user->getEMailAddress())) { |
||
128 | try { |
||
129 | $this->config->deleteUserValue($userId, 'owncloud', 'lostpassword'); |
||
130 | $this->lostController->sendEmail($userId, $token, $link); |
||
131 | } catch (\Exception $e) { |
||
132 | $output->writeln('<error>Can\'t send new user mail to ' . $user->getEMailAddress() . ': ' . $e->getMessage() . '</error>'); |
||
133 | return 1; |
||
134 | } |
||
135 | } else { |
||
136 | if ($emailLink) { |
||
137 | $output->writeln('<error>Email address is not set for the user ' . $user->getUID() . '</error>'); |
||
138 | return 1; |
||
139 | } |
||
140 | } |
||
141 | $this->config->setUserValue($userId, 'owncloud', 'lostpassword', $this->timeFactory->getTime() . ':' . $token); |
||
142 | $output->writeln('The password reset link is: ' . $link); |
||
143 | return 0; |
||
144 | } elseif ($input->isInteractive()) { |
||
145 | /** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */ |
||
146 | $dialog = $this->getHelperSet()->get('question'); |
||
147 | |||
148 | if (\OCP\App::isEnabled('encryption')) { |
||
149 | $output->writeln( |
||
150 | '<error>Warning: Resetting the password when using encryption will result in data loss!</error>' |
||
151 | ); |
||
152 | if (!$dialog->ask($input, $output, new Question('<question>Do you want to continue?</question>', true))) { |
||
153 | return 1; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $q = new Question('<question>Enter a new password: </question>', false); |
||
158 | $q->setHidden(true); |
||
159 | $password = $dialog->ask($input, $output, $q); |
||
160 | if ($password === false) { |
||
161 | // When user presses RETURN key or no password characters are entered, |
||
162 | // $password gets a boolean value false. |
||
163 | $output->writeln("<error>Password cannot be empty!</error>"); |
||
164 | return 1; |
||
165 | } |
||
166 | $q = new Question('<question>Confirm the new password: </question>', false); |
||
167 | $q->setHidden(true); |
||
168 | $confirm = $dialog->ask($input, $output, $q); |
||
169 | if ($password !== $confirm) { |
||
170 | $output->writeln("<error>Passwords did not match!</error>"); |
||
171 | return 1; |
||
172 | } |
||
173 | } else { |
||
174 | $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
||
175 | return 1; |
||
176 | } |
||
177 | |||
178 | $success = $user->setPassword($password); |
||
179 | if ($success) { |
||
180 | $output->writeln("<info>Successfully reset password for " . $username . "</info>"); |
||
181 | } else { |
||
182 | $output->writeln("<error>Error while resetting password!</error>"); |
||
183 | return 1; |
||
184 | } |
||
185 | } |
||
186 | |||
197 |