Test Setup Failed
Branch master (1b2352)
by Valery
09:42
created

ExceptionListener::onKernelException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\EventListener;
6
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
9
use Symfony\Component\Security\Csrf\Exception\TokenNotFoundException;
10
11
final class ExceptionListener
12
{
13
    public function onKernelException(ExceptionEvent $event): void
14
    {
15
        $exception = $event->getThrowable();
16
        if ($exception instanceof TokenNotFoundException) {
17
            $customResponse = new JsonResponse([
18
                'status' => 'fail', 'message' => $exception->getMessage(),
19
            ], 419);
20
            $event->setResponse($customResponse);
21
        }
22
    }
23
}
24