| 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); |
||
| 97 | public function doResetAccount(array $data, Form $form): HTTPResponse |
||
| 98 | { |
||
| 99 | $memberID = $this->owner->getRequest()->getSession()->get('MemberID'); |
||
| 100 | |||
| 101 | // If the ID isn't in the session, politely assume the session has expired |
||
| 102 | if (!$memberID) { |
||
| 103 | $form->sessionMessage( |
||
| 104 | _t( |
||
| 105 | __CLASS__ . '.RESETTIMEDOUT', |
||
| 106 | "The account reset process timed out. Please click the link in the email and try again." |
||
| 107 | ), |
||
| 108 | ValidationResult::TYPE_ERROR |
||
| 109 | ); |
||
| 110 | |||
| 111 | return $this->owner->redirectBack(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** @var Member&MemberExtension $member */ |
||
| 115 | $member = Member::get()->byID(intval($memberID)); |
||
| 116 | |||
| 117 | // Fail if passwords do not match |
||
| 118 | if ($data['NewPassword1'] !== $data['NewPassword2']) { |
||
| 119 | $form->sessionMessage( |
||
| 120 | _t( |
||
| 121 | 'SilverStripe\\Security\\Member.ERRORNEWPASSWORD', |
||
| 122 | 'You have entered your new password differently, try again' |
||
| 123 | ), |
||
| 124 | ValidationResult::TYPE_ERROR |
||
| 125 | ); |
||
| 126 | |||
| 127 | return $this->owner->redirectBack(); |
||
| 128 | } |
||
| 129 | |||
| 130 | // Check if the new password is accepted |
||
| 131 | $validationResult = $member->changePassword($data['NewPassword1']); |
||
| 132 | if (!$validationResult->isValid()) { |
||
| 133 | $form->setSessionValidationResult($validationResult); |
||
| 134 | |||
| 135 | return $this->owner->redirectBack(); |
||
| 136 | } |
||
| 137 | |||
| 138 | // Clear locked out status |
||
| 139 | $member->LockedOutUntil = null; |
||
| 140 | $member->FailedLoginCount = null; |
||
| 141 | |||
| 142 | // Clear account reset data |
||
| 143 | $member->AccountResetHash = null; |
||
| 144 | $member->AccountResetExpired = DBDatetime::create()->now(); |
||
| 145 | $member->write(); |
||
| 146 | |||
| 147 | // Pass off to extensions to perform any additional reset actions |
||
| 148 | $this->extend('handleAccountReset', $member); |
||
| 149 | |||
| 150 | // Send the user along to the login form (allowing any additional factors to kick in as needed) |
||
| 151 | $this->owner->setSessionMessage( |
||
| 152 | _t( |
||
| 153 | __CLASS__ . '.RESETSUCCESSMESSAGE', |
||
| 154 | 'Reset complete. Please log in with your new password.' |
||
| 155 | ), |
||
| 156 | ValidationResult::TYPE_GOOD |
||
| 157 | ); |
||
| 158 | return $this->owner->redirect($this->owner->Link('login')); |
||
| 159 | } |
||
| 161 |