| Conditions | 9 |
| Paths | 20 |
| Total Lines | 64 |
| Code Lines | 39 |
| 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 |
||
| 48 | public function process(Request $request) |
||
| 49 | { |
||
| 50 | $requestHandler = $this->requestHandlerManager->getRequestHandler($request); |
||
| 51 | |||
| 52 | $code = $request->get('code'); |
||
| 53 | try { |
||
| 54 | $passwordReset = $this->forgotPasswordCodeRepository->findActiveByCode($code); |
||
| 55 | } catch (NoEntityFoundException $e) { |
||
| 56 | return $requestHandler->generateErrorOutput(null); |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($passwordReset->isUsed()) { |
||
| 60 | $this->getLogger()->warning('A user has tried to reset their password with an used code', ['code' => $code]); |
||
| 61 | |||
| 62 | return $requestHandler->generateErrorOutput(null); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($passwordReset->isExpired()) { |
||
| 66 | $this->getLogger()->warning('A user has tried to reset their password with an expired code', ['code' => $code]); |
||
| 67 | |||
| 68 | return $requestHandler->generateErrorOutput(null); |
||
| 69 | } |
||
| 70 | |||
| 71 | if (!$request->isMethod('POST')) { |
||
| 72 | return $requestHandler->generateDefaultOutput(null); |
||
| 73 | } |
||
| 74 | |||
| 75 | $passwordReset->setUsed(true); |
||
| 76 | $passwordReset->setUsedAt(new \DateTime('now')); |
||
| 77 | /** @var UserInterface $user */ |
||
| 78 | $user = $this->userRepository->getById($passwordReset->getUserId()); |
||
| 79 | |||
| 80 | $this->eventDispatcher->dispatch(new PrePasswordResetConfirmEvent($user), PrePasswordResetConfirmEvent::NAME); |
||
| 81 | |||
| 82 | if ($requestHandler instanceof JsonRequestHandler) { |
||
| 83 | $json = json_decode($request->getContent(), true); |
||
| 84 | $newPassword = $json['password']; |
||
| 85 | } else { |
||
| 86 | $newPassword = $request->get('password'); |
||
| 87 | } |
||
| 88 | $newPasswordHash = $this->encoderFactory->getPasswordHasher($user)->hash($newPassword); |
||
| 89 | |||
| 90 | $user->setPassword($newPasswordHash); |
||
| 91 | $hasActivatedUser = false; |
||
| 92 | if (!$user->isConfirmed()) { |
||
| 93 | $user->setActivatedAt(new \DateTime('now')); |
||
| 94 | $user->setIsConfirmed(true); |
||
| 95 | $hasActivatedUser = true; |
||
| 96 | } |
||
| 97 | $this->userRepository->save($user); |
||
| 98 | $this->forgotPasswordCodeRepository->save($passwordReset); |
||
| 99 | |||
| 100 | if ($hasActivatedUser) { |
||
| 101 | $this->eventDispatcher->dispatch(new PostUserConfirmEvent($user), PostUserConfirmEvent::NAME); |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->eventDispatcher->dispatch(new PostPasswordResetConfirmEvent($user), PostPasswordResetConfirmEvent::NAME); |
||
| 105 | $this->getLogger()->info('A user has reset their password', ['email' => $user->getEmail()]); |
||
| 106 | |||
| 107 | if ($requestHandler instanceof JsonRequestHandler) { |
||
| 108 | return new JsonResponse(['success' => true]); |
||
| 109 | } |
||
| 110 | |||
| 111 | return new RedirectResponse($this->urlGenerator->generate('parthenon_user_login')); |
||
| 112 | } |
||
| 114 |