Lines 100.00% 8 / 8
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ChooseVisibilityListener 100.00% 8 / 8 100.00% 2 / 2 6 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 __invoke 100.00% 7 / 7 100.00% 1 / 1 5
1<?php
2
3declare(strict_types=1);
4
5namespace App\EventListener;
6
7use App\Entity\User;
8use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
9use Symfony\Component\HttpFoundation\RedirectResponse;
10use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
12
13/**
14 * A web sign-in of a user who has not chosen the visibility of the profile parts yet leads to
15 * /profile. Not for the API (no response to replace) or a remember-me login (neither).
16 */
17#[AsEventListener]
18final readonly class ChooseVisibilityListener
19{
20    public function __construct(private UrlGeneratorInterface $urlGenerator)
21    {
22    }
23
24    public function __invoke(LoginSuccessEvent $event): void
25    {
26        $user = $event->getUser();
27        if ('main' !== $event->getFirewallName()
28            || !$event->getResponse() instanceof RedirectResponse
29            || !$user instanceof User
30            || $user->getVisibilityChosenAt() instanceof \DateTimeImmutable
31        ) {
32            return;
33        }
34
35        $event->setResponse(new RedirectResponse($this->urlGenerator->generate('app_profile')));
36    }
37}