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