ExceptionListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 56
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A onKernelException() 0 15 3
A getSubscribedEvents() 0 4 1
1
<?php
2
3
namespace Alchemy\RestBundle\EventListener;
4
5
use Alchemy\Rest\Request\ContentTypeMatcher;
6
use Alchemy\Rest\Response\ExceptionTransformer;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
class ExceptionListener implements EventSubscriberInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    private $handledContentTypes;
19
20
    /**
21
     * @var ContentTypeMatcher
22
     */
23
    private $contentTypeMatcher;
24
25
    /**
26
     * @var ExceptionTransformer
27
     */
28
    private $exceptionTransformer;
29
30
    /**
31
     * @param ContentTypeMatcher $matcher
32
     * @param ExceptionTransformer $exceptionTransformer
33
     * @param array $handledContentTypes
34
     */
35 4
    public function __construct(
36
        ContentTypeMatcher $matcher,
37
        ExceptionTransformer $exceptionTransformer = null,
38
        array $handledContentTypes = null
39
    ) {
40 4
        $this->contentTypeMatcher = $matcher;
41 4
        $this->exceptionTransformer = $exceptionTransformer ?: new ExceptionTransformer\DefaultExceptionTransformer();
42 4
        $this->handledContentTypes = $handledContentTypes ?: array('application/json');
43 4
    }
44
45
    /**
46
     * @param GetResponseForExceptionEvent $event
47
     */
48 4
    public function onKernelException(GetResponseForExceptionEvent $event)
49
    {
50 4
        $request = $event->getRequest();
51 4
        $acceptHeader = $request->headers->get('Accept', '*/*');
52
53 4
        if (!$this->contentTypeMatcher->matches($acceptHeader, $this->handledContentTypes)) {
0 ignored issues
show
Bug introduced by
It seems like $acceptHeader defined by $request->headers->get('Accept', '*/*') on line 51 can also be of type array; however, Alchemy\Rest\Request\ContentTypeMatcher::matches() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54 2
            return;
55
        }
56
57 2
        $exception = $event->getException();
58 2
        $data = $this->exceptionTransformer->transformException($exception);
59 2
        $status = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
60
61 2
        $event->setResponse(new JsonResponse($data, $status));
62 2
    }
63
64 2
    public static function getSubscribedEvents()
65
    {
66 2
        return array(KernelEvents::EXCEPTION => 'onKernelException');
67
    }
68
}
69