Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckVerifiedUserSubscriber
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onCheckPassport
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 onValidationFailure
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 getSubscribedEvents
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\EventSubscriber;
4
5use App\Exception\UserNotVerifiedException;
6use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7use Symfony\Component\HttpFoundation\RedirectResponse;
8use Symfony\Component\HttpFoundation\RequestStack;
9use Symfony\Component\Routing\RouterInterface;
10use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
11use Symfony\Component\Security\Http\Event\CheckPassportEvent;
12use Symfony\Component\Security\Http\Event\LoginFailureEvent;
13
14class CheckVerifiedUserSubscriber implements EventSubscriberInterface
15{
16
17    public function __construct(private readonly RouterInterface $router)
18    {
19        // empty body
20    }
21
22
23    public function onCheckPassport(CheckPassportEvent $event)
24    {
25        $passport = $event->getPassport();
26        /*
27         * var User $user
28         */
29        $user = $passport->getUser();
30
31        if (!$user->isVerified()) {
32            throw new UserNotVerifiedException();
33        }
34    }
35
36
37    public function onValidationFailure(LoginFailureEvent $failureEvent)
38    {
39        if (!$failureEvent->getException() instanceof UserNotVerifiedException) {
40            return;
41        }
42
43        $request = $failureEvent->getRequest();
44        $email = $failureEvent->getPassport()->getUser()->getEmail();
45        $request->getSession()->set('non_verified_email', $email);
46
47        $response = new RedirectResponse(
48            $this->router->generate('security_resend_verify_email')
49        );
50        $failureEvent->setResponse($response);
51    }
52
53
54    public static function getSubscribedEvents(): array
55    {
56        return [
57            CheckPassportEvent::class => ['onCheckPassport', -10],
58            LoginFailureEvent::class => 'onValidationFailure'
59        ];
60    }
61}