Test Setup Failed
Push — master ( 0c8bf0...b71d97 )
by Herberto
05:57
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Auth\Authorization;
16
17
use Acme\App\Core\Port\Auth\Authorization\AccessDeniedException;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20
use Symfony\Component\HttpKernel\KernelEvents;
21
use Symfony\Component\Security\Core\Exception\AccessDeniedException as SymfonyAccessDeniedException;
22
23
final class AccessDeniedExceptionSubscriber implements EventSubscriberInterface
24
{
25
    /**
26
     * Return the subscribed events, their methods and possibly their priorities
27
     * (the higher the priority the earlier the method is called).
28
     *
29
     * @see http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
30
     */
31
    public static function getSubscribedEvents(): array
32
    {
33
        return [
34
            KernelEvents::EXCEPTION => ['handleException', 256],
35
        ];
36
    }
37
38
    public function handleException(GetResponseForExceptionEvent $event): void
39
    {
40
        $exception = $event->getException();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...onEvent::getException() has been deprecated with message: since Symfony 4.4, use getThrowable instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
41
42
        switch (true) {
43
            case $exception instanceof AccessDeniedException:
44
                /*
45
                 * We translate our AccessDeniedException into Symfony AccessDeniedException,
46
                 * so that the system continues to respond in the same way, by redirecting to the login screen
47
                 */
48
                $newException = new SymfonyAccessDeniedException($exception->getMessage(), $exception);
49
                $newException->setAttributes(array_merge($exception->getRoleList(), [$exception->getAction()]));
50
                $newException->setSubject($exception->getSubject());
51
52
                $event->setException($newException);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...onEvent::setException() has been deprecated with message: since Symfony 4.4, use setThrowable instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
53
54
                break;
55
        }
56
    }
57
}
58