| Conditions | 4 |
| Paths | 4 |
| Total Lines | 52 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 53 | public function register(Request $request): Response |
||
| 54 | { |
||
| 55 | $data = json_decode($request->getContent(), true); |
||
| 56 | $email = $data['email']; |
||
| 57 | $username = $data['username']; |
||
| 58 | $password = $data['password']; |
||
| 59 | $rules_accepted = $data['rules_accepted'] ?? false; |
||
| 60 | |||
| 61 | // Check username |
||
| 62 | if (!$rules_accepted) { |
||
| 63 | return $this->getResponse(false, $this->translator->trans('registration.must_accept_rules')); |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->checkIp($request, $username); |
||
| 67 | |||
| 68 | // Check username |
||
| 69 | $user = $this->userManager->findUserByUsername($username); |
||
| 70 | if ($user !== null) { |
||
| 71 | return $this->getResponse(false, $this->translator->trans('registration.username_exists')); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Check email |
||
| 75 | $user = $this->userManager->findUserByEmail($email); |
||
| 76 | if ($user !== null) { |
||
| 77 | return $this->getResponse(false, $this->translator->trans('registration.email_exists')); |
||
| 78 | } |
||
| 79 | |||
| 80 | $user = $this->userManager->createUser(); |
||
| 81 | $user->setEnabled(false); |
||
| 82 | $user->setEmail($email); |
||
| 83 | $user->setUsername($username); |
||
| 84 | $user->setPlainPassword($password); |
||
| 85 | $user->setConfirmationToken($this->tokenGenerator->generateToken()); |
||
| 86 | |||
| 87 | |||
| 88 | $this->userManager->updateUser($user); |
||
| 89 | |||
| 90 | // Send email to activate account |
||
| 91 | $body = sprintf( |
||
| 92 | $this->translator->trans('registration.email.message'), |
||
| 93 | $user->getUsername(), |
||
| 94 | ($request->server->get('HTTP_ORIGIN') ?? null) . '/en/register?token=' . $user->getConfirmationToken() |
||
| 95 | ); |
||
| 96 | |||
| 97 | $this->mailer->send( |
||
| 98 | sprintf($this->translator->trans('registration.email.subject'), $user->getUsername()), |
||
| 99 | $body, |
||
| 100 | null, |
||
| 101 | $user->getEmail() |
||
| 102 | ); |
||
| 103 | |||
| 104 | return $this->getResponse(true, sprintf($this->translator->trans('registration.check_email'), $email)); |
||
| 105 | } |
||
| 176 |