| Conditions | 4 |
| Paths | 4 |
| Total Lines | 62 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 143 | public function doResetAccount(array $data, Form $form): HTTPResponse |
||
| 144 | { |
||
| 145 | $memberID = $this->owner->getRequest()->getSession()->get('MemberID'); |
||
| 146 | |||
| 147 | // If the ID isn't in the session, politely assume the session has expired |
||
| 148 | if (!$memberID) { |
||
| 149 | $form->sessionMessage( |
||
| 150 | _t( |
||
| 151 | __CLASS__ . '.RESETTIMEDOUT', |
||
| 152 | "The account reset process timed out. Please click the link in the email and try again." |
||
| 153 | ), |
||
| 154 | ValidationResult::TYPE_ERROR |
||
| 155 | ); |
||
| 156 | |||
| 157 | return $this->owner->redirectBack(); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** @var Member&MemberExtension $member */ |
||
| 161 | $member = Member::get()->byID((int) $memberID); |
||
| 162 | |||
| 163 | // Fail if passwords do not match |
||
| 164 | if ($data['NewPassword1'] !== $data['NewPassword2']) { |
||
| 165 | $form->sessionMessage( |
||
| 166 | _t( |
||
| 167 | 'SilverStripe\\Security\\Member.ERRORNEWPASSWORD', |
||
| 168 | 'You have entered your new password differently, try again' |
||
| 169 | ), |
||
| 170 | ValidationResult::TYPE_ERROR |
||
| 171 | ); |
||
| 172 | |||
| 173 | return $this->owner->redirectBack(); |
||
| 174 | } |
||
| 175 | |||
| 176 | // Check if the new password is accepted |
||
| 177 | $validationResult = $member->changePassword($data['NewPassword1']); |
||
| 178 | if (!$validationResult->isValid()) { |
||
| 179 | $form->setSessionValidationResult($validationResult); |
||
| 180 | |||
| 181 | return $this->owner->redirectBack(); |
||
| 182 | } |
||
| 183 | |||
| 184 | // Clear locked out status |
||
| 185 | $member->LockedOutUntil = null; |
||
| 186 | $member->FailedLoginCount = null; |
||
| 187 | |||
| 188 | // Clear account reset data |
||
| 189 | $member->AccountResetHash = null; |
||
| 190 | $member->AccountResetExpired = DBDatetime::create()->now(); |
||
| 191 | $member->write(); |
||
| 192 | |||
| 193 | // Pass off to extensions to perform any additional reset actions |
||
| 194 | $this->extend('handleAccountReset', $member); |
||
| 195 | |||
| 196 | // Send the user along to the login form (allowing any additional factors to kick in as needed) |
||
| 197 | $this->owner->setSessionMessage( |
||
| 198 | _t( |
||
| 199 | __CLASS__ . '.RESETSUCCESSMESSAGE', |
||
| 200 | 'Reset complete. Please log in with your new password.' |
||
| 201 | ), |
||
| 202 | ValidationResult::TYPE_GOOD |
||
| 203 | ); |
||
| 204 | return $this->owner->redirect($this->owner->Link('login')); |
||
| 205 | } |
||
| 207 |