| Conditions | 7 |
| Paths | 48 |
| Total Lines | 80 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 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 |
||
| 40 | public function createOne($data) |
||
| 41 | { |
||
| 42 | // password |
||
| 43 | $password = isset($data['password'])?$data['password']:null; |
||
| 44 | $password2 = isset($data['password2'])?$data['password2']:null; |
||
| 45 | |||
| 46 | if (empty($password)) { |
||
| 47 | throw ValidatorException::exception('password', __('Password can\'t be empty')); |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($password !== $password2) { |
||
| 51 | throw ValidatorException::exception('password2', __('Password is not equal')); |
||
| 52 | } |
||
| 53 | |||
| 54 | if($data['id'] == ''){ |
||
| 55 | unset($data['id']); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** @var $row Row */ |
||
| 59 | $row = $this->getTable()->create(); |
||
| 60 | $row->setFromArray($data); |
||
| 61 | $row->status = Table::STATUS_PENDING; |
||
| 62 | $row->save(); |
||
| 63 | |||
| 64 | $userId = $row->id; |
||
| 65 | |||
| 66 | // create auth |
||
| 67 | Auth\Table::getInstance()->generateEquals($row, $password); |
||
| 68 | |||
| 69 | // create activation token |
||
| 70 | // valid for 5 days |
||
| 71 | $actionRow = UsersActions\Table::getInstance()->generate($userId, UsersActions\Table::ACTION_ACTIVATION, 5); |
||
| 72 | |||
| 73 | // send activation email |
||
| 74 | // generate activation URL |
||
| 75 | $activationUrl = Router::getFullUrl( |
||
| 76 | 'users', |
||
| 77 | 'activation', |
||
| 78 | ['code' => $actionRow->code, 'id' => $userId] |
||
| 79 | ); |
||
| 80 | |||
| 81 | $subject = "Activation"; |
||
| 82 | |||
| 83 | $body = Application::getInstance()->dispatch( |
||
| 84 | 'users', |
||
| 85 | 'mail/template', |
||
| 86 | [ |
||
| 87 | 'template' => 'registration', |
||
| 88 | 'vars' => ['user' => $row, 'activationUrl' => $activationUrl, 'password' => $password] |
||
| 89 | ] |
||
| 90 | )->render(); |
||
| 91 | |||
| 92 | try { |
||
| 93 | $mail = Mailer::create(); |
||
| 94 | $mail->Subject = $subject; |
||
| 95 | $mail->msgHTML(nl2br($body)); |
||
| 96 | $mail->addAddress($data['email']); |
||
| 97 | Mailer::send($mail); |
||
| 98 | } catch (\Exception $e) { |
||
| 99 | Logger::log( |
||
| 100 | 'error', |
||
| 101 | $e->getMessage(), |
||
| 102 | ['module' => 'users', 'controller' => 'change-email', 'userId' => $userId] |
||
| 103 | ); |
||
| 104 | |||
| 105 | throw new Exception('Unable to send email. Please contact administrator.'); |
||
| 106 | } |
||
| 107 | |||
| 108 | // show notification and redirect |
||
| 109 | Messages::addSuccess( |
||
|
|
|||
| 110 | "Your account has been created and an activation link has". |
||
| 111 | "been sent to the e-mail address you entered.<br/>". |
||
| 112 | "Note that you must activate the account by clicking on the activation link". |
||
| 113 | "when you get the e-mail before you can login." |
||
| 114 | ); |
||
| 115 | // wtf? |
||
| 116 | // redirectTo('index', 'index'); |
||
| 117 | |||
| 118 | return $userId; |
||
| 119 | } |
||
| 120 | } |
||
| 121 |
This check looks for function calls that miss required arguments.