| Conditions | 4 |
| Paths | 4 |
| Total Lines | 62 |
| Code Lines | 32 |
| 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 declare(strict_types=1); |
||
| 116 | public function doResetAccount(array $data, Form $form): HTTPResponse |
||
| 117 | { |
||
| 118 | $memberID = $this->owner->getRequest()->getSession()->get('MemberID'); |
||
| 119 | |||
| 120 | // If the ID isn't in the session, politely assume the session has expired |
||
| 121 | if (!$memberID) { |
||
| 122 | $form->sessionMessage( |
||
| 123 | _t( |
||
| 124 | __CLASS__ . '.RESETTIMEDOUT', |
||
| 125 | "The account reset process timed out. Please click the link in the email and try again." |
||
| 126 | ), |
||
| 127 | ValidationResult::TYPE_ERROR |
||
| 128 | ); |
||
| 129 | |||
| 130 | return $this->owner->redirectBack(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** @var Member&MemberExtension $member */ |
||
| 134 | $member = Member::get()->byID(intval($memberID)); |
||
| 135 | |||
| 136 | // Fail if passwords do not match |
||
| 137 | if ($data['NewPassword1'] !== $data['NewPassword2']) { |
||
| 138 | $form->sessionMessage( |
||
| 139 | _t( |
||
| 140 | 'SilverStripe\\Security\\Member.ERRORNEWPASSWORD', |
||
| 141 | 'You have entered your new password differently, try again' |
||
| 142 | ), |
||
| 143 | ValidationResult::TYPE_ERROR |
||
| 144 | ); |
||
| 145 | |||
| 146 | return $this->owner->redirectBack(); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Check if the new password is accepted |
||
| 150 | $validationResult = $member->changePassword($data['NewPassword1']); |
||
| 151 | if (!$validationResult->isValid()) { |
||
| 152 | $form->setSessionValidationResult($validationResult); |
||
| 153 | |||
| 154 | return $this->owner->redirectBack(); |
||
| 155 | } |
||
| 156 | |||
| 157 | // Clear locked out status |
||
| 158 | $member->LockedOutUntil = null; |
||
| 159 | $member->FailedLoginCount = null; |
||
| 160 | |||
| 161 | // Clear account reset data |
||
| 162 | $member->AccountResetHash = null; |
||
| 163 | $member->AccountResetExpired = DBDatetime::create()->now(); |
||
| 164 | $member->write(); |
||
| 165 | |||
| 166 | // Pass off to extensions to perform any additional reset actions |
||
| 167 | $this->extend('handleAccountReset', $member); |
||
| 168 | |||
| 169 | // Send the user along to the login form (allowing any additional factors to kick in as needed) |
||
| 170 | $this->owner->setSessionMessage( |
||
| 171 | _t( |
||
| 172 | __CLASS__ . '.RESETSUCCESSMESSAGE', |
||
| 173 | 'Reset complete. Please log in with your new password.' |
||
| 174 | ), |
||
| 175 | ValidationResult::TYPE_GOOD |
||
| 176 | ); |
||
| 177 | return $this->owner->redirect($this->owner->Link('login')); |
||
| 178 | } |
||
| 180 |