Completed
Pull Request — master (#144)
by
unknown
12:31
created

ExceptionListener::onKernelException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 2
b 1
f 1
1
<?php
2
3
namespace AppBundle\EventListener;
4
5
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\Security\Core\Exception\AuthenticationException;
8
9
class ExceptionListener
10
{
11
    /**
12
     * @param GetResponseForExceptionEvent $event
13
     */
14
    public function onKernelException(GetResponseForExceptionEvent $event)
15
    {
16
        $exception = $event->getException();
17
18
        if ($exception instanceof AuthenticationException) {
19
            $response = new JsonResponse([
20
                'code' => $exception->getPrevious()->getCode(),
21
                'message' => $exception->getMessage(),
22
            ], 403);
23
            $event->setResponse($response);
24
        }
25
    }
26
}
27