Lines
100.00%
23 / 23
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getLoginUrl | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| authenticate | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 3 | ||
| onAuthenticationSuccess | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | ||
| 21 | class LoginFormAuthenticator extends AbstractLoginFormAuthenticator | |
| 22 | { | |
| 23 | use TargetPathTrait; | |
| 24 | ||
| 25 | public function __construct( | |
| 26 | private readonly RouterInterface $router, | |
| 27 | #[Autowire(env: 'APP_ENV')] private readonly string $appEnv, | |
| 28 | ) { | |
| 29 | } | |
| 30 | ||
| 31 | #[\Override] | |
| 32 | protected function getLoginUrl(Request $request): string | |
| 33 | { | |
| 34 | return $this->router->generate('app_login'); | |
| 35 | } | |
| 36 | ||
| 37 | #[\Override] | |
| 38 | public function authenticate(Request $request): Passport | |
| 39 | { | |
| 40 | if (false === \in_array($this->appEnv, ['dev', 'test'], true)) { | |
| 41 | throw new \UnexpectedValueException('GTFO!'); | |
| 42 | } | |
| 43 | ||
| 44 | $identifier = (string) $request->request->get('identifier'); | |
| 45 | ||
| 46 | if ('' === $identifier) { | |
| 47 | throw new AuthenticationException('User identifier cannot be empty.'); | |
| 48 | } | |
| 49 | ||
| 50 | return new SelfValidatingPassport( | |
| 51 | new UserBadge($identifier), | |
| 52 | [ | |
| 53 | new CsrfTokenBadge( | |
| 54 | 'login', | |
| 55 | (string) $request->request->get('_csrf_token') | |
| 56 | ), | |
| 57 | new RememberMeBadge(), | |
| 58 | ] | |
| 59 | ); | |
| 60 | } | |
| 61 | ||
| 62 | #[\Override] | |
| 63 | public function onAuthenticationSuccess( | |
| 64 | Request $request, | |
| 65 | TokenInterface $token, | |
| 66 | string $firewallName | |
| 67 | ): RedirectResponse { | |
| 68 | if ($targetPath = $this->getTargetPath( | |
| 69 | $request->getSession(), | |
| 70 | $firewallName | |
| 71 | ) | |
| 72 | ) { | |
| 73 | return new RedirectResponse($targetPath); | |
| 74 | } | |
| 75 | ||
| 76 | return new RedirectResponse($this->router->generate('app_default')); | |
| 77 | } | |
| 78 | } |