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