LogoutListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Security\EventListener;
15
16
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie\JWTCookieProvider;
17
use Silverback\ApiComponentsBundle\Mercure\MercureAuthorization;
18
use Silverback\ApiComponentsBundle\RefreshToken\Storage\RefreshTokenStorageInterface;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Security\Http\Event\LogoutEvent;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class LogoutListener
26
{
27
    public function __construct(
28
        private readonly RefreshTokenStorageInterface $storage,
29
        private readonly JWTCookieProvider $cookieProvider,
30
        private readonly MercureAuthorization $mercureAuthorization
31
    ) {
32
    }
33
34
    public function __invoke(LogoutEvent $event): void
35
    {
36
        $this->storage->expireAll($event->getToken()->getUser());
37
        $response = $event->getResponse() ?? new Response();
38
        $response->headers->setCookie($this->cookieProvider->createCookie('x.x.x', null, 1));
39
        $response->headers->setCookie($this->mercureAuthorization->getClearAuthorizationCookie());
40
        $response->headers->remove('Location');
41
        $response->setStatusCode(Response::HTTP_OK)->setContent('');
42
        $event->setResponse($response);
43
    }
44
}
45