EncodeJsonResponseListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onKernelView() 0 18 4
A getSubscribedEvents() 0 4 1
1
<?php
2
3
namespace Alchemy\RestBundle\EventListener;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
9
use Symfony\Component\HttpKernel\KernelEvents;
10
11
class EncodeJsonResponseListener implements EventSubscriberInterface
12
{
13
14 6
    public function onKernelView(GetResponseForControllerResultEvent $event)
15
    {
16 6
        if (! $event->getRequest()->attributes->get('_rest[encode_response]', false, true)) {
17 2
            return;
18
        }
19
20 4
        $result = $event->getControllerResult();
21
22 4
        if (! $result instanceof Response) {
23 4
            if (!is_array($result)) {
24 2
                throw new \LogicException('Invalid controller result: array was expected.');
25
            }
26
27 2
            $result = new JsonResponse($result);
28 2
        }
29
30 2
        $event->setResponse($result);
31 2
    }
32
33 2
    public static function getSubscribedEvents()
34
    {
35 2
        return array(KernelEvents::VIEW => 'onKernelView');
36
    }
37
}
38