| Conditions | 11 |
| Paths | 15 |
| Total Lines | 74 |
| Code Lines | 36 |
| 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 |
||
| 31 | public function signup(): ?string |
||
| 32 | { |
||
| 33 | // check if user is authorized |
||
| 34 | if (App::$User->isAuth()) { |
||
| 35 | throw new ForbiddenException(__('You are always authorized on website, registration not allowed')); |
||
| 36 | } |
||
| 37 | |||
| 38 | // load configs |
||
| 39 | $configs = $this->getConfigs(); |
||
| 40 | |||
| 41 | // init register model |
||
| 42 | $registerForm = new FormRegister($configs['captchaOnRegister'] === 1); |
||
| 43 | |||
| 44 | // registration based on invite. Check conditions. |
||
| 45 | if ($configs['registrationType'] === 0) { |
||
| 46 | // get token and email |
||
| 47 | $inviteToken = $this->request->query->get('token'); |
||
| 48 | $inviteEmail = $this->request->query->get('email'); |
||
| 49 | // check if token length & email is valid format |
||
| 50 | if (Str::length($inviteToken) < 32 || !Str::isEmail($inviteEmail)) { |
||
| 51 | throw new ForbiddenException(__('Registration allowed only if you have invite!')); |
||
| 52 | } |
||
| 53 | |||
| 54 | // remove deprecated data |
||
| 55 | Invite::clean(); |
||
| 56 | // try to find token |
||
| 57 | $find = Invite::where('token', $inviteToken) |
||
| 58 | ->where('email', $inviteEmail) |
||
| 59 | ->count(); |
||
| 60 | |||
| 61 | // token not foud? invalid invite key |
||
| 62 | if ($find !== 1) { |
||
| 63 | throw new ForbiddenException(__('Your invite token is invalid! Contact with administrator')); |
||
| 64 | } |
||
| 65 | |||
| 66 | // notify the invite token is accepted |
||
| 67 | if (!$registerForm->send()) { |
||
| 68 | App::$Session->getFlashBag()->add('success', __('Invite was accepted! Continue registration')); |
||
| 69 | } |
||
| 70 | |||
| 71 | // set email from token data |
||
| 72 | $registerForm->email = $inviteEmail; |
||
| 73 | } |
||
| 74 | |||
| 75 | // if register data is send and valid |
||
| 76 | if ($registerForm->send() && $registerForm->validate()) { |
||
| 77 | $activation = $configs['registrationType'] === 1; |
||
| 78 | if ($registerForm->tryRegister($activation)) { |
||
| 79 | // initialize succes signup event |
||
| 80 | App::$Event->run(static::EVENT_USER_REGISTER_SUCCESS, [ |
||
|
|
|||
| 81 | 'model' => $registerForm |
||
| 82 | ]); |
||
| 83 | // if no activation is required - just open session and redirect user to main page |
||
| 84 | if (!$activation) { |
||
| 85 | $loginModel = new FormLogin(); |
||
| 86 | $loginModel->openSession($registerForm->_userObject); |
||
| 87 | $this->response->redirect('/'); // session is opened, refresh page |
||
| 88 | } |
||
| 89 | // send notification of successful registering |
||
| 90 | App::$Session->getFlashBag()->add('success', __('Your account is registered. You must confirm account via email')); |
||
| 91 | } else { |
||
| 92 | // init fail signup event |
||
| 93 | App::$Event->run(static::EVENT_USER_REGISTER_FAIL, [ |
||
| 94 | 'model' => $registerForm |
||
| 95 | ]); |
||
| 96 | App::$Session->getFlashBag()->add('error', __('Email is always used on website')); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // render view |
||
| 101 | return $this->view->render('user/signup', [ |
||
| 102 | 'model' => $registerForm, |
||
| 103 | 'config' => $configs, |
||
| 104 | 'useCaptcha' => $configs['captchaOnRegister'] === 1 |
||
| 105 | ]); |
||
| 108 |