Completed
Push — master ( aafb5f...ed91ed )
by Christian
14s queued 10s
created

ViewResponseListener::onKernelView()   C

Complexity

Conditions 14
Paths 78

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 14.6238

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 29
cts 34
cp 0.8529
rs 6.2666
c 0
b 0
f 0
cc 14
nc 78
nop 1
crap 14.6238

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\EventListener;
13
14
use FOS\RestBundle\Controller\Annotations\View as ViewAnnotation;
15
use FOS\RestBundle\FOSRestBundle;
16
use FOS\RestBundle\View\View;
17
use FOS\RestBundle\View\ViewHandlerInterface;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Event\ViewEvent;
21
use Symfony\Component\HttpKernel\KernelEvents;
22
23
/**
24
 * The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation.
25
 *
26
 * @author Lukas Kahwe Smith <[email protected]>
27
 *
28
 * @internal
29
 */
30
class ViewResponseListener implements EventSubscriberInterface
31
{
32
    private $viewHandler;
33
    private $forceView;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param ViewHandlerInterface $viewHandler
39
     * @param bool                 $forceView
40
     */
41
    public function __construct(ViewHandlerInterface $viewHandler, $forceView)
42
    {
43 21
        $this->viewHandler = $viewHandler;
44
        $this->forceView = $forceView;
45 21
    }
46 21
47 21
    /**
48
     * Renders the parameters and initializes a new response object with the
49
     * rendered content.
50
     *
51
     * @param ViewEvent $event
52
     */
53
    public function onKernelView($event)
54
    {
55 21
        $request = $event->getRequest();
56
57 21
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
58
            return false;
59 21
        }
60
61
        $configuration = $request->attributes->get('_template');
62
63 21
        $view = $event->getControllerResult();
64
        if (!$view instanceof View) {
65 21
            if (!$configuration instanceof ViewAnnotation && !$this->forceView) {
66 21
                return;
67 11
            }
68 1
69
            $view = new View($view);
70
        }
71 10
72 10
        if ($configuration instanceof ViewAnnotation) {
73
            if (null !== $configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
74 20
                $view->setStatusCode($configuration->getStatusCode());
75 16
            }
76
77
            $context = $view->getContext();
78 16
            if ($configuration->getSerializerGroups()) {
79 1
                if (null === $context->getGroups()) {
80 1
                    $context->setGroups($configuration->getSerializerGroups());
81
                } else {
82 16
                    $context->setGroups(array_merge($context->getGroups(), $configuration->getSerializerGroups()));
83 16
                }
84
            }
85
            if (true === $configuration->getSerializerEnableMaxDepthChecks()) {
86 16
                $context->enableMaxDepth();
87 1
            } elseif (false === $configuration->getSerializerEnableMaxDepthChecks()) {
88 1
                $context->disableMaxDepth();
89 16
            }
90 1
        }
91 16
92 1
        if (null === $view->getFormat()) {
93 1
            $view->setFormat($request->getRequestFormat());
94
        }
95 16
96 16
        $response = $this->viewHandler->handle($view, $request);
97 16
98 4
        $event->setResponse($response);
99
    }
100
101 20
    public static function getSubscribedEvents()
102 20
    {
103 20
        // Must be executed before SensioFrameworkExtraBundle's listener
104
        return array(
105 20
            KernelEvents::VIEW => array('onKernelView', 30),
106 20
        );
107 20
    }
108
}
109