Passed
Push — main ( a33f85...42eea9 )
by Daniel
05:30
created

AccessDeniedListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 8
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onPostRespond() 0 8 3
1
<?php
2
3
namespace Silverback\ApiComponentsBundle\Security\EventListener;
4
5
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
6
use Silverback\ApiComponentsBundle\EventListener\Api\ApiEventListenerTrait;
7
use Silverback\ApiComponentsBundle\Mercure\MercureAuthorization;
8
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
9
use Symfony\Component\HttpKernel\Event\ResponseEvent;
10
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
11
12
class AccessDeniedListener
13
{
14
    use ApiEventListenerTrait;
0 ignored issues
show
Bug introduced by
The trait Silverback\ApiComponents...i\ApiEventListenerTrait requires the property $attributes which is not provided by Silverback\ApiComponents...er\AccessDeniedListener.
Loading history...
15
16
    public function __construct(
17
        private readonly MercureAuthorization $mercureAuthorization
18
    ) {
19
    }
20
21
    public function onPostRespond(ResponseEvent $event): void
22
    {
23
        $request = $event->getRequest();
24
        $attributes = $this->getAttributes($request);
25
        if ($attributes['operation']->getName() !== 'me' || !($response = $event->getResponse()) instanceof JWTAuthenticationFailureResponse) {
26
            return;
27
        }
28
        $response->headers->setCookie($this->mercureAuthorization->getClearAuthorizationCookie());
29
    }
30
}
31