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