Completed
Push — master ( d15cf4...5163a0 )
by Valentyn
06:05
created

ExceptionListener::onKernelException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2.0078
1
<?php
2
3
namespace App\EventListener;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
8
use Symfony\Component\HttpKernel\KernelEvents;
9
10
class ExceptionListener implements EventSubscriberInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 2
    public function onKernelException(GetResponseForExceptionEvent $event)
16
    {
17 2
        if (\getenv('APP_ENV') === 'dev') {
18
            // for dev env we need to show all exception data
19
            return;
20
        }
21
22 2
        $exception = $event->getException();
23 2
        $response = new JsonResponse([
24 2
            'error' => $exception->getMessage(),
25
        ]);
26 2
        $event->setResponse($response);
27 2
    }
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public static function getSubscribedEvents()
32
    {
33
        return array(
34 1
            KernelEvents::EXCEPTION => array('onKernelException', -100),
35
        );
36
    }
37
}