| Conditions | 12 |
| Paths | 24 |
| Total Lines | 78 |
| Code Lines | 40 |
| 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 |
||
| 18 | public function doChangePassword(array $data) |
||
| 19 | { |
||
| 20 | $member = Member::currentUser(); |
||
| 21 | // The user was logged in, check the current password |
||
| 22 | if ($member && ( |
||
| 23 | empty($data['OldPassword']) || |
||
| 24 | !$member->checkPassword($data['OldPassword'])->isValid() |
||
| 25 | )) { |
||
| 26 | $this->form->sessionMessage( |
||
| 27 | _t('Member.ERRORPASSWORDNOTMATCH', "Your current password does not match, please try again"), |
||
| 28 | "bad" |
||
| 29 | ); |
||
| 30 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 31 | return $this->redirectBackToForm(); |
||
| 32 | } |
||
| 33 | |||
| 34 | if (!$member) { |
||
| 35 | if (Session::get('AutoLoginHash')) { |
||
| 36 | $member = Member::member_from_autologinhash(Session::get('AutoLoginHash')); |
||
| 37 | } |
||
| 38 | |||
| 39 | // The user is not logged in and no valid auto login hash is available |
||
| 40 | if (!$member) { |
||
| 41 | Session::clear('AutoLoginHash'); |
||
| 42 | return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login'))); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | // Check the new password |
||
| 47 | if (empty($data['NewPassword1'])) { |
||
| 48 | $this->form->sessionMessage( |
||
| 49 | _t('Member.EMPTYNEWPASSWORD', "The new password can't be empty, please try again"), |
||
| 50 | "bad" |
||
| 51 | ); |
||
| 52 | |||
| 53 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 54 | return $this->redirectBackToForm(); |
||
| 55 | } |
||
| 56 | |||
| 57 | // Fail if passwords do not match |
||
| 58 | if ($data['NewPassword1'] !== $data['NewPassword2']) { |
||
| 59 | $this->form->sessionMessage( |
||
| 60 | _t('Member.ERRORNEWPASSWORD', "You have entered your new password differently, try again"), |
||
| 61 | "bad" |
||
| 62 | ); |
||
| 63 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 64 | return $this->redirectBackToForm(); |
||
| 65 | } |
||
| 66 | |||
| 67 | // Check if the new password is accepted |
||
| 68 | $validationResult = $member->changePassword($data['NewPassword1']); |
||
| 69 | if (!$validationResult->isValid()) { |
||
| 70 | $this->form->setSessionValidationResult($validationResult); |
||
| 71 | return $this->redirectBackToForm(); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Clear locked out status |
||
| 75 | $member->LockedOutUntil = null; |
||
| 76 | $member->FailedLoginCount = null; |
||
| 77 | $member->write(); |
||
| 78 | |||
| 79 | if ($member->canLogIn()->isValid()) { |
||
| 80 | $member->logIn(); |
||
| 81 | } |
||
| 82 | |||
| 83 | // TODO Add confirmation message to login redirect |
||
| 84 | Session::clear('AutoLoginHash'); |
||
| 85 | |||
| 86 | // Redirect to backurl |
||
| 87 | $backURL = $this->getBackURL(); |
||
| 88 | if ($backURL) { |
||
| 89 | return $this->redirect($backURL); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Redirect to default location - the login form saying "You are logged in as..." |
||
| 93 | $url = Security::singleton()->Link('login'); |
||
| 94 | return $this->redirect($url); |
||
| 95 | } |
||
| 96 | |||
| 104 |