| Conditions | 10 |
| Paths | 13 |
| Total Lines | 103 |
| Code Lines | 63 |
| 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 |
||
| 95 | public function update(ServerRequestInterface $request): ResponseInterface |
||
| 96 | { |
||
| 97 | $context = []; |
||
| 98 | $param = new RequestData($request); |
||
| 99 | |||
| 100 | $id = $this->authentication->getUser()->getId(); |
||
| 101 | |||
| 102 | /** @var User|null $user */ |
||
| 103 | $user = $this->userRepository->find($id); |
||
| 104 | |||
| 105 | if ($user === null) { |
||
| 106 | $this->authentication->logout(); |
||
| 107 | |||
| 108 | $this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
||
| 109 | |||
| 110 | return new RedirectResponse( |
||
| 111 | $this->routeHelper->generateUrl('user_login') |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | $context['user'] = $user; |
||
| 115 | $context['param'] = (new UserParam())->fromEntity($user); |
||
| 116 | |||
| 117 | if ($request->getMethod() === 'GET') { |
||
| 118 | return new TemplateResponse( |
||
| 119 | $this->template, |
||
| 120 | 'user/update_profile', |
||
| 121 | $context |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | $formParam = new UserParam($param->posts()); |
||
| 125 | $formParam->setStatus($user->status); |
||
| 126 | |||
| 127 | $context['param'] = $formParam; |
||
| 128 | |||
| 129 | $validator = new UserValidator($formParam, $this->lang, true); |
||
| 130 | if ($validator->validate() === false) { |
||
| 131 | $context['errors'] = $validator->getErrors(); |
||
| 132 | |||
| 133 | return new TemplateResponse( |
||
| 134 | $this->template, |
||
| 135 | 'user/update_profile', |
||
| 136 | $context |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | $usernameExist = $this->userRepository->findBy([ |
||
| 141 | 'username' => $formParam->getUsername(), |
||
| 142 | ]); |
||
| 143 | |||
| 144 | if ($usernameExist !== null && $usernameExist->id !== $id) { |
||
| 145 | $this->flash->setError($this->lang->tr('This username already exist')); |
||
| 146 | |||
| 147 | return new TemplateResponse( |
||
| 148 | $this->template, |
||
| 149 | 'user/update_profile', |
||
| 150 | $context |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | $emailExist = $this->userRepository->findBy([ |
||
| 155 | 'email' => $formParam->getEmail(), |
||
| 156 | ]); |
||
| 157 | |||
| 158 | if ($emailExist !== null && $emailExist->id !== $id) { |
||
| 159 | $this->flash->setError($this->lang->tr('This email already exist')); |
||
| 160 | |||
| 161 | return new TemplateResponse( |
||
| 162 | $this->template, |
||
| 163 | 'user/update_profile', |
||
| 164 | $context |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | $user->username = $formParam->getUsername(); |
||
| 169 | $user->lastname = $formParam->getLastname(); |
||
| 170 | $user->firstname = $formParam->getFirstname(); |
||
| 171 | $user->email = $formParam->getEmail(); |
||
| 172 | $user->status = $formParam->getStatus(); |
||
| 173 | |||
| 174 | $password = $formParam->getPassword(); |
||
| 175 | if (!empty($password)) { |
||
| 176 | $passwordHash = $this->hash->hash($password); |
||
| 177 | |||
| 178 | $user->password = $passwordHash; |
||
| 179 | } |
||
| 180 | |||
| 181 | try { |
||
| 182 | $this->userRepository->save($user); |
||
| 183 | |||
| 184 | $this->flash->setSuccess($this->lang->tr('Data successfully updated')); |
||
| 185 | |||
| 186 | return new RedirectResponse( |
||
| 187 | $this->routeHelper->generateUrl('user_profile') |
||
| 188 | ); |
||
| 189 | } catch (Exception $ex) { |
||
| 190 | $this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
||
| 191 | |||
| 192 | $this->flash->setError($this->lang->tr('Data processing error')); |
||
| 193 | |||
| 194 | return new TemplateResponse( |
||
| 195 | $this->template, |
||
| 196 | 'user/update_profile', |
||
| 197 | $context |
||
| 198 | ); |
||
| 202 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.