JWTClearTokenListener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 22
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onJwtExpired() 0 3 1
A onJwtInvalid() 0 3 1
A clearJwtCookie() 0 4 1
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\EventListener\Jwt;
15
16
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
17
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
18
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie\JWTCookieProvider;
19
use Silverback\ApiComponentsBundle\Mercure\MercureAuthorization;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class JWTClearTokenListener
26
{
27
    public function __construct(
28
        private readonly JWTCookieProvider $cookieProvider,
29
        private readonly MercureAuthorization $mercureAuthorization
30
    ) {
31
    }
32
33
    public function onJwtInvalid(JWTInvalidEvent $event): void
34
    {
35
        $this->clearJwtCookie($event->getResponse());
36
    }
37
38
    public function onJwtExpired(JWTExpiredEvent $event): void
39
    {
40
        $this->clearJwtCookie($event->getResponse());
41
    }
42
43
    private function clearJwtCookie(Response $response): void
44
    {
45
        $response->headers->setCookie($this->cookieProvider->createCookie('x.x.x', null, 1));
46
        $response->headers->setCookie($this->mercureAuthorization->getClearAuthorizationCookie());
47
    }
48
}
49