Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LoginSubscriber
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSubscribedEvents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onLogin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\EventListener;
4
5use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
8use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
10class LoginSubscriber extends AbstractController implements EventSubscriberInterface
11{
12    /**
13     * Class LoginSubscriber
14     *
15     * This class is responsible for handling login events.
16     * It is used to display a flash message when a user logs in.
17     * It is also used to redirect the user to the homepage after login.
18     * We don't want phpstan to check the constructor of this class so we ignore it.
19     *
20     * @phpstan-ignore-next-line
21     * @param UrlGeneratorInterface $urlGenerator
22     * @return void
23     */
24    public function __construct(private UrlGeneratorInterface $urlGenerator)
25    {
26    }
27
28    /**
29     * Returns the subscribed events for the LoginSubscriber class.
30     *
31     * @return array<mixed> The subscribed events.
32     */
33    public static function getSubscribedEvents(): array
34    {
35        return [InteractiveLoginEvent::class => 'onLogin'];
36    }
37
38    /**
39     * Event listener method called when a user logs in.
40     *
41     * @param InteractiveLoginEvent $event The event object.
42     * @return void
43     */
44    public function onLogin(InteractiveLoginEvent $event): void
45    {
46        $this->addFlash('success', 'Vous êtes connecté !!');
47    }
48}