| Conditions | 15 |
| Paths | 42 |
| Total Lines | 130 |
| Code Lines | 83 |
| 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 |
||
| 59 | public function run(RequestInterface $request, ResponseInterface $response) |
||
| 60 | { |
||
| 61 | $translator = $this->translator(); |
||
| 62 | |||
| 63 | $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; |
||
| 64 | |||
| 65 | $token = $request->getParam('token'); |
||
|
|
|||
| 66 | $email = $request->getParam('email'); |
||
| 67 | $password1 = $request->getParam('password1'); |
||
| 68 | $password2 = $request->getParam('password2'); |
||
| 69 | |||
| 70 | if (!$token) { |
||
| 71 | $this->addFeedback('error', $translator->translate('Missing reset token.')); |
||
| 72 | $this->setSuccess(false); |
||
| 73 | |||
| 74 | return $response->withStatus(400); |
||
| 75 | } |
||
| 76 | |||
| 77 | if (!$email) { |
||
| 78 | $this->addFeedback('error', $translator->translate('Missing email.')); |
||
| 79 | $this->setSuccess(false); |
||
| 80 | |||
| 81 | return $response->withStatus(400); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!$password1) { |
||
| 85 | $this->addFeedback('error', $translator->translate('Missing password')); |
||
| 86 | $this->setSuccess(false); |
||
| 87 | |||
| 88 | return $response->withStatus(400); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (!$password2) { |
||
| 92 | $this->addFeedback('error', $translator->translate('Missing password confirmation')); |
||
| 93 | $this->setSuccess(false); |
||
| 94 | |||
| 95 | return $response->withStatus(400); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($password1 != $password2) { |
||
| 99 | $this->addFeedback('error', $translator->translate('Passwords do not match')); |
||
| 100 | $this->setSuccess(false); |
||
| 101 | |||
| 102 | return $response->withStatus(400); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($this->recaptchaEnabled() && $this->validateCaptchaFromRequest($request, $response) === false) { |
||
| 106 | if ($ip) { |
||
| 107 | $logMessage = sprintf( |
||
| 108 | '[Admin] Reset Password — CAPTCHA challenge failed for "%s" from %s', |
||
| 109 | $email, |
||
| 110 | $ip |
||
| 111 | ); |
||
| 112 | } else { |
||
| 113 | $logMessage = sprintf( |
||
| 114 | '[Admin] Reset Password — CAPTCHA challenge failed for "%s"', |
||
| 115 | |||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | $this->logger->warning($logMessage); |
||
| 120 | |||
| 121 | return $response; |
||
| 122 | } |
||
| 123 | |||
| 124 | $failMessage = $translator->translation('An error occurred while processing the password change.'); |
||
| 125 | |||
| 126 | $user = $this->loadUser($email); |
||
| 127 | if ($user === null) { |
||
| 128 | if ($ip) { |
||
| 129 | $logMessage = sprintf( |
||
| 130 | '[Admin] Reset Password — Can not find "%s" user in database for %s.', |
||
| 131 | $email, |
||
| 132 | $ip |
||
| 133 | ); |
||
| 134 | } else { |
||
| 135 | $logMessage = sprintf( |
||
| 136 | '[Admin] Reset Password — Can not find "%s" user in database.', |
||
| 137 | |||
| 138 | ); |
||
| 139 | } |
||
| 140 | $this->logger->error($logMessage); |
||
| 141 | |||
| 142 | $this->addFeedback('error', $failMessage); |
||
| 143 | $this->setSuccess(false); |
||
| 144 | |||
| 145 | return $response->withStatus(500); |
||
| 146 | } |
||
| 147 | |||
| 148 | if (!$this->validateToken($token, $user->id())) { |
||
| 149 | $this->setFailureUrl($this->adminUrl('account/lost-password?notice=invalidtoken')); |
||
| 150 | $this->addFeedback('error', $translator->translate('Your password reset token is invalid or expired.')); |
||
| 151 | $this->setSuccess(false); |
||
| 152 | |||
| 153 | return $response->withStatus(400); |
||
| 154 | } |
||
| 155 | |||
| 156 | try { |
||
| 157 | $user->resetPassword($password1); |
||
| 158 | $this->deleteToken($token); |
||
| 159 | |||
| 160 | $this->addFeedback('success', $translator->translate('Your password has been successfully changed.')); |
||
| 161 | $this->setSuccessUrl((string)$this->adminUrl('login?notice=newpass')); |
||
| 162 | $this->setSuccess(true); |
||
| 163 | |||
| 164 | return $response; |
||
| 165 | } catch (Exception $e) { |
||
| 166 | if ($ip) { |
||
| 167 | $logMessage = sprintf( |
||
| 168 | '[Admin] Reset Password — Failed to process change for "%s" from %s: %s', |
||
| 169 | $email, |
||
| 170 | $ip, |
||
| 171 | $e->getMessage() |
||
| 172 | ); |
||
| 173 | } else { |
||
| 174 | $logMessage = sprintf( |
||
| 175 | '[Admin] Reset Password — Failed to process change for "%s": %s', |
||
| 176 | $email, |
||
| 177 | $e->getMessage() |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | $this->logger->error($logMessage); |
||
| 181 | |||
| 182 | $this->addFeedback('error', $failMessage); |
||
| 183 | $this->setSuccess(false); |
||
| 184 | |||
| 185 | return $response->withStatus(500); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $response; |
||
| 189 | } |
||
| 245 |