| Conditions | 10 |
| Paths | 8 |
| Total Lines | 69 |
| Code Lines | 35 |
| 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 |
||
| 42 | public function recovery($id = null, $token = null) |
||
| 43 | { |
||
| 44 | if (App::$User->isAuth()) { |
||
| 45 | throw new ForbiddenException(__('You are always authorized on website, recovery is rejected')); |
||
| 46 | } |
||
| 47 | |||
| 48 | // check if recovery token and user_id is passed and validate it |
||
| 49 | if (Any::isInt($id) && Str::length($token) >= 64) { |
||
| 50 | $rObject = UserRecovery::where('id', $id) |
||
| 51 | ->where('token', $token) |
||
| 52 | ->where('archive', false); |
||
| 53 | |||
| 54 | // check if recovery row exist |
||
| 55 | if ($rObject->count() !== 1) { |
||
| 56 | throw new NotFoundException(__('This recovery data is not found')); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** @var UserRecovery $rData */ |
||
| 60 | $rData = $rObject->first(); |
||
| 61 | // check if user with this "user_id" in recovery row exist |
||
| 62 | $rUser = App::$User->identity($rData->user_id); |
||
| 63 | if ($rUser === null) { |
||
| 64 | throw new NotFoundException(__('User is not found')); |
||
| 65 | } |
||
| 66 | |||
| 67 | // email link valid, show new password set form |
||
| 68 | $modelPwd = new FormPasswordChange($rUser); |
||
| 69 | // process new password submit |
||
| 70 | if ($modelPwd->send() && $modelPwd->validate()) { |
||
| 71 | // new password is valid, update user data |
||
| 72 | $modelPwd->make(); |
||
| 73 | // set password change token as archived row |
||
| 74 | $rData->archive = true; |
||
| 75 | $rData->save(); |
||
| 76 | // add event notification |
||
| 77 | // add success event |
||
| 78 | App::$Event->run(static::EVENT_USER_RECOVERY_SUCCESS, [ |
||
| 79 | 'model' => $modelPwd |
||
| 80 | ]); |
||
| 81 | // add notification |
||
| 82 | App::$Session->getFlashBag()->add('success', __('Your account password is successful changed!')); |
||
| 83 | |||
| 84 | // lets open user session with recovered data |
||
| 85 | $loginModel = new FormLogin(); |
||
| 86 | $loginModel->openSession($rUser); |
||
| 87 | $this->response->redirect('/'); // session is opened, refresh page |
||
| 88 | } |
||
| 89 | |||
| 90 | return $this->view->render('password_recovery', [ |
||
| 91 | 'model' => $modelPwd |
||
| 92 | ]); |
||
| 93 | } |
||
| 94 | |||
| 95 | // initialize and process recovery form data |
||
| 96 | $model = new FormRecovery(true); |
||
| 97 | if ($model->send()) { |
||
| 98 | if ($model->validate()) { |
||
| 99 | $model->make(); |
||
| 100 | App::$Session->getFlashBag()->add('success', __('We send to you email with instruction to recovery your account')); |
||
| 101 | } else { |
||
| 102 | App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // render visual form content |
||
| 107 | return $this->view->render('recovery', [ |
||
| 108 | 'model' => $model |
||
| 109 | ]); |
||
| 110 | } |
||
| 111 | } |
||
| 112 |