| Conditions | 8 |
| Paths | 7 |
| Total Lines | 64 |
| Code Lines | 41 |
| 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 |
||
| 57 | public function authenticate(Request $request): SelfValidatingPassport |
||
| 58 | { |
||
| 59 | $cookieValue = (string) $request->cookies->get(self::COOKIE_NAME); |
||
| 60 | |||
| 61 | $parsed = $this->parseCookieValue($cookieValue); |
||
| 62 | if (null === $parsed) { |
||
| 63 | $request->attributes->set('_remember_me_clear', true); |
||
| 64 | throw new AuthenticationException('Invalid remember-me cookie format/signature.'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $userId = (int) $parsed['userId']; |
||
| 68 | $rawToken = (string) $parsed['token']; |
||
| 69 | $hash = hash('sha256', $rawToken); |
||
| 70 | |||
| 71 | // Opportunistic cleanup. |
||
| 72 | $cutoff = (new DateTimeImmutable())->modify('-'.self::TTL_SECONDS.' seconds'); |
||
| 73 | $this->tokenRepository->deleteExpiredRememberMeTokens($cutoff); |
||
| 74 | |||
| 75 | $tokenEntity = $this->tokenRepository->findRememberMeToken($userId, $hash); |
||
| 76 | if (!$tokenEntity instanceof ValidationToken) { |
||
| 77 | $request->attributes->set('_remember_me_clear', true); |
||
| 78 | throw new AuthenticationException('Remember-me token not found.'); |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($this->isExpired($tokenEntity)) { |
||
| 82 | $this->tokenRepository->remove($tokenEntity, true); |
||
| 83 | $request->attributes->set('_remember_me_clear', true); |
||
| 84 | throw new AuthenticationException('Remember-me token expired.'); |
||
| 85 | } |
||
| 86 | |||
| 87 | $user = $this->userRepository->find($userId); |
||
| 88 | if (!$user instanceof User) { |
||
| 89 | $this->tokenRepository->remove($tokenEntity, true); |
||
| 90 | $request->attributes->set('_remember_me_clear', true); |
||
| 91 | throw new AuthenticationException('User not found for remember-me token.'); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Safety checks. |
||
| 95 | if (User::ACTIVE !== $user->getActive()) { |
||
| 96 | $this->tokenRepository->remove($tokenEntity, true); |
||
| 97 | $request->attributes->set('_remember_me_clear', true); |
||
| 98 | throw new AuthenticationException('User not active.'); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (null !== $user->getExpirationDate() && $user->getExpirationDate() <= new \DateTime()) { |
||
| 102 | $this->tokenRepository->remove($tokenEntity, true); |
||
| 103 | $request->attributes->set('_remember_me_clear', true); |
||
| 104 | throw new AuthenticationException('User expired.'); |
||
| 105 | } |
||
| 106 | |||
| 107 | // Prepare rotation (subscriber will commit rotation + set cookie on response). |
||
| 108 | $newRaw = $this->generateRawToken(); |
||
| 109 | $newHash = hash('sha256', $newRaw); |
||
| 110 | $newCookieValue = $this->buildCookieValue($userId, $newRaw); |
||
| 111 | |||
| 112 | $request->attributes->set('_remember_me_rotate', [ |
||
| 113 | 'oldId' => $tokenEntity->getId(), |
||
| 114 | 'userId' => $userId, |
||
| 115 | 'newHash' => $newHash, |
||
| 116 | 'newCookie' => $newCookieValue, |
||
| 117 | ]); |
||
| 118 | |||
| 119 | return new SelfValidatingPassport( |
||
| 120 | new UserBadge((string) $userId, static fn () => $user) |
||
| 121 | ); |
||
| 198 |