| Conditions | 12 |
| Paths | 37 |
| Total Lines | 120 |
| Code Lines | 76 |
| 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 |
||
| 241 | public function update(ServerRequestInterface $request): ResponseInterface |
||
| 242 | { |
||
| 243 | $context = []; |
||
| 244 | $param = new RequestData($request); |
||
| 245 | |||
| 246 | $id = (int) $request->getAttribute('id'); |
||
| 247 | |||
| 248 | /** @var User|null $user */ |
||
| 249 | $user = $this->userRepository->find($id); |
||
| 250 | |||
| 251 | if ($user === null) { |
||
| 252 | $this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
||
| 253 | |||
| 254 | return new RedirectResponse( |
||
| 255 | $this->routeHelper->generateUrl('user_list') |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | $context['user'] = $user; |
||
| 259 | $context['param'] = (new UserParam())->fromEntity($user); |
||
| 260 | $currentRolesId = Arr::getColumn($user->roles, 'id'); |
||
| 261 | $context['param']->setRoles($currentRolesId); |
||
| 262 | |||
| 263 | $context['roles'] = $this->roleRepository->orderBy('name') |
||
| 264 | ->all(); |
||
| 265 | |||
| 266 | $context['user_status'] = $this->statusList->getUserStatus(); |
||
| 267 | |||
| 268 | if ($request->getMethod() === 'GET') { |
||
| 269 | return new TemplateResponse( |
||
| 270 | $this->template, |
||
| 271 | 'user/update', |
||
| 272 | $context |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | $formParam = new UserParam($param->posts()); |
||
| 276 | $context['param'] = $formParam; |
||
| 277 | |||
| 278 | $validator = new UserValidator($formParam, $this->lang, true); |
||
| 279 | if ($validator->validate() === false) { |
||
| 280 | $context['errors'] = $validator->getErrors(); |
||
| 281 | |||
| 282 | return new TemplateResponse( |
||
| 283 | $this->template, |
||
| 284 | 'user/update', |
||
| 285 | $context |
||
| 286 | ); |
||
| 287 | } |
||
| 288 | |||
| 289 | $usernameExist = $this->userRepository->findBy([ |
||
| 290 | 'username' => $formParam->getUsername(), |
||
| 291 | ]); |
||
| 292 | |||
| 293 | if ($usernameExist !== null && $usernameExist->id !== $id) { |
||
| 294 | $this->flash->setError($this->lang->tr('This username already exist')); |
||
| 295 | |||
| 296 | return new TemplateResponse( |
||
| 297 | $this->template, |
||
| 298 | 'user/update', |
||
| 299 | $context |
||
| 300 | ); |
||
| 301 | } |
||
| 302 | |||
| 303 | $emailExist = $this->userRepository->findBy([ |
||
| 304 | 'email' => $formParam->getEmail(), |
||
| 305 | ]); |
||
| 306 | |||
| 307 | if ($emailExist !== null && $emailExist->id !== $id) { |
||
| 308 | $this->flash->setError($this->lang->tr('This email already exist')); |
||
| 309 | |||
| 310 | return new TemplateResponse( |
||
| 311 | $this->template, |
||
| 312 | 'user/update', |
||
| 313 | $context |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | $user->username = $formParam->getUsername(); |
||
| 318 | $user->lastname = $formParam->getLastname(); |
||
| 319 | $user->firstname = $formParam->getFirstname(); |
||
| 320 | $user->email = $formParam->getEmail(); |
||
| 321 | $user->status = $formParam->getStatus(); |
||
| 322 | $user->role = $formParam->getRole(); |
||
| 323 | |||
| 324 | $password = $formParam->getPassword(); |
||
| 325 | if (!empty($password)) { |
||
| 326 | $passwordHash = $this->hash->hash($password); |
||
| 327 | |||
| 328 | $user->password = $passwordHash; |
||
| 329 | } |
||
| 330 | |||
| 331 | $rolesId = $formParam->getRoles(); |
||
| 332 | $rolesIdToDelete = array_diff($currentRolesId, $rolesId); |
||
| 333 | if (count($rolesIdToDelete) > 0) { |
||
| 334 | $deletedRoles = $this->roleRepository->findAll(...$rolesIdToDelete); |
||
| 335 | $user->removeRoles($deletedRoles); |
||
| 336 | } |
||
| 337 | |||
| 338 | $newRolesId = array_diff($rolesId, $currentRolesId); |
||
| 339 | if (count($newRolesId) > 0) { |
||
| 340 | $newRoles = $this->roleRepository->findAll(...$newRolesId); |
||
| 341 | $user->setRoles($newRoles); |
||
| 342 | } |
||
| 343 | |||
| 344 | try { |
||
| 345 | $this->userRepository->save($user); |
||
| 346 | |||
| 347 | $this->flash->setSuccess($this->lang->tr('Data successfully updated')); |
||
| 348 | |||
| 349 | return new RedirectResponse( |
||
| 350 | $this->routeHelper->generateUrl('user_detail', ['id' => $id]) |
||
| 351 | ); |
||
| 352 | } catch (Exception $ex) { |
||
| 353 | $this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
||
| 354 | |||
| 355 | $this->flash->setError($this->lang->tr('Data processing error')); |
||
| 356 | |||
| 357 | return new TemplateResponse( |
||
| 358 | $this->template, |
||
| 359 | 'user/update', |
||
| 360 | $context |
||
| 361 | ); |
||
| 404 |