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

ExceptionListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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
}