| Conditions | 5 |
| Paths | 5 |
| Total Lines | 70 |
| Code Lines | 41 |
| 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 |
||
| 46 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 47 | { |
||
| 48 | if ($request->getMethod() === 'GET') { |
||
| 49 | return new TemplateResponse( |
||
| 50 | $this->template, |
||
| 51 | 'user/create', |
||
| 52 | [ |
||
| 53 | 'param' => new UserParam([]) |
||
| 54 | ] |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | $param = new RequestData($request); |
||
| 59 | $formParam = new UserParam($param->posts()); |
||
| 60 | $validator = new UserValidator($formParam); |
||
| 61 | |||
| 62 | if (!$validator->validate()) { |
||
| 63 | return new TemplateResponse( |
||
| 64 | $this->template, |
||
| 65 | 'user/create', |
||
| 66 | [ |
||
| 67 | 'errors' => $validator->getErrors(), |
||
| 68 | 'param' => $formParam |
||
| 69 | ] |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | $username = $param->post('username'); |
||
| 74 | $userExist = $this->userRepository->findBy(['username' => $username]); |
||
| 75 | |||
| 76 | if ($userExist) { |
||
| 77 | $this->logger->error('User with username {username} already exists', ['username' => $username]); |
||
| 78 | return new TemplateResponse( |
||
| 79 | $this->template, |
||
| 80 | 'user/create', |
||
| 81 | [ |
||
| 82 | 'param' => $formParam |
||
| 83 | ] |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | $password = $param->post('password'); |
||
| 88 | |||
| 89 | $hash = new BcryptHash(); |
||
| 90 | $passwordHash = $hash->hash($password); |
||
|
|
|||
| 91 | |||
| 92 | $user = $this->userRepository->create([ |
||
| 93 | 'username' => $formParam->getUsername(), |
||
| 94 | 'fname' => Str::ucfirst($formParam->getFirstname()), |
||
| 95 | 'lname' => Str::upper($formParam->getLastname()), |
||
| 96 | 'password' => $passwordHash, |
||
| 97 | 'status' => 1, |
||
| 98 | 'age' => (int) $formParam->getAge(), |
||
| 99 | 'deleted' => 0, |
||
| 100 | ]); |
||
| 101 | |||
| 102 | $result = $this->userRepository->save($user); |
||
| 103 | |||
| 104 | if (!$result) { |
||
| 105 | $this->logger->error('Error when saved the user'); |
||
| 106 | return new TemplateResponse( |
||
| 107 | $this->template, |
||
| 108 | 'user/create', |
||
| 109 | [ |
||
| 110 | 'param' => $formParam |
||
| 111 | ] |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | return (new RedirectResponse('list'))->redirect(); |
||
| 116 | } |
||
| 118 |