Completed
Push — master ( 5644ae...21573f )
by Lukas Kahwe
04:43
created

ViewResponseListener::onKernelController()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4286
cc 3
eloc 6
nc 3
nop 1
crap 3
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\FOSRestBundle;
15
use FOS\RestBundle\View\View;
16
use FOS\RestBundle\View\ViewHandlerInterface;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\KernelEvents;
21
use Symfony\Component\Templating\TemplateReferenceInterface;
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 18
    public function __construct(ViewHandlerInterface $viewHandler, $forceView)
42
    {
43 18
        $this->viewHandler = $viewHandler;
44 18
        $this->forceView = $forceView;
45 18
    }
46
47
    /**
48
     * Renders the parameters and template and initializes a new response object with the
49
     * rendered content.
50
     *
51
     * @param GetResponseForControllerResultEvent $event
52
     */
53 18
    public function onKernelView(GetResponseForControllerResultEvent $event)
54
    {
55 18
        $request = $event->getRequest();
56
57 18
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
58
            return false;
59
        }
60
61
        /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
62 18
        $configuration = $request->attributes->get('_view');
63
64 18
        $view = $event->getControllerResult();
65 18
        $customViewDefined = true;
66 18
        if (!$view instanceof View) {
67 9
            if (!$configuration && !$this->forceView) {
68 1
                return;
69
            }
70
71 8
            $view = new View($view);
72 8
            $customViewDefined = false;
73 8
        }
74
75 17
        if ($configuration) {
76 11
            $context = $view->getSerializationContext();
77 11
            if ($configuration->getTemplateVar()) {
78
                $view->setTemplateVar($configuration->getTemplateVar());
79
            }
80 11
            if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
81 1
                $view->setStatusCode($configuration->getStatusCode());
82 1
            }
83 11
            if ($configuration->getSerializerGroups() && !$customViewDefined) {
84
                $context->addGroups($configuration->getSerializerGroups());
85
            }
86 11
            if ($configuration->getSerializerEnableMaxDepthChecks()) {
87 1
                $context->setMaxDepth(0);
88 1
            }
89 11
            $populateDefaultVars = $configuration->isPopulateDefaultVars();
90 11
        } else {
91 6
            $populateDefaultVars = true;
92
        }
93
94 17
        if (null === $view->getFormat()) {
95 17
            $view->setFormat($request->getRequestFormat());
96 17
        }
97
98 17
        $vars = $request->attributes->get('_template_vars');
99 17
        if (!$vars && $populateDefaultVars) {
100 16
            $vars = $request->attributes->get('_template_default_vars');
101 16
        }
102
103 17
        if ($this->viewHandler->isFormatTemplating($view->getFormat())) {
104 10
            if (!empty($vars)) {
105 3
                $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
106 3
                foreach ($vars as $var) {
107 3
                    if (!array_key_exists($var, $parameters)) {
108 3
                        $parameters[$var] = $request->attributes->get($var);
109 3
                    }
110 3
                }
111 3
                $view->setData($parameters);
112 3
            }
113
114 10
            $template = null !== $configuration && $configuration->getTemplate()
115 10
                ? $configuration->getTemplate()
116 10
                : $request->attributes->get('_template');
117 10
            if ($template) {
118 2
                if ($template instanceof TemplateReferenceInterface) {
119 1
                    $template->set('format', null);
120 1
                }
121
122 2
                $view->setTemplate($template);
123 2
            }
124 10
        }
125
126 17
        $response = $this->viewHandler->handle($view, $request);
127
128 17
        $event->setResponse($response);
129 17
    }
130
131 12
    public static function getSubscribedEvents()
132
    {
133
        return array(
134 12
            KernelEvents::VIEW => 'onKernelView',
135 12
        );
136
    }
137
}
138