Completed
Push — dev ( adc692...f71603 )
by
unknown
9s
created

JsonExceptionListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 31
ccs 13
cts 14
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onKernelException() 0 18 3
1
<?php
2
3
namespace AppBundle\Listener;
4
5
use AppBundle\Exception\JsonHttpException;
6
use Psr\Log\LoggerInterface;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
9
10
class JsonExceptionListener
11
{
12
    /**
13
     * @var LoggerInterface
14
     */
15
    private $logger;
16
17 7
    public function __construct(LoggerInterface $logger)
18
    {
19 7
        $this->logger = $logger;
20 7
    }
21
22 1
    public function onKernelException(GetResponseForExceptionEvent $event)
23
    {
24 1
        $exception = $event->getException();
25 1
        if ($exception instanceof JsonHttpException) {
26
            $errorData = [
27
                'error' => [
28 1
                    'code' => $exception->getStatusCode(),
29 1
                    'message' => $exception->getMessage(),
30
                ]
31
            ];
32 1
            if (($data = $exception->getData())) {
33
                $errorData['error']['fields'] = $data;
34
            }
35 1
            $response = new JsonResponse($errorData);
36 1
            $event->setResponse($response);
37 1
            $this->logger->error($exception->getMessage(), $errorData);
38
        }
39 1
    }
40
}
41