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