ViewResponseListener   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 87.88%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 67
ccs 29
cts 33
cp 0.8788
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C onKernelView() 0 47 14
A getSubscribedEvents() 0 7 1
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 18
    public function __construct(ViewHandlerInterface $viewHandler, bool $forceView)
36
    {
37 18
        $this->viewHandler = $viewHandler;
38 18
        $this->forceView = $forceView;
39 18
    }
40
41 18
    public function onKernelView(ViewEvent $event): void
42
    {
43 18
        $request = $event->getRequest();
44
45 18
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
46
            return;
47
        }
48
49 18
        $configuration = $request->attributes->get('_template');
50
51 18
        $view = $event->getControllerResult();
52 18
        if (!$view instanceof View) {
53 12
            if (!$configuration instanceof ViewAnnotation && !$this->forceView) {
54 1
                return;
55
            }
56
57 11
            $view = new View($view);
58
        }
59
60 17
        if ($configuration instanceof ViewAnnotation) {
61 13
            if (null !== $configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
62 1
                $view->setStatusCode($configuration->getStatusCode());
63
            }
64
65 13
            $context = $view->getContext();
66 13
            if ($configuration->getSerializerGroups()) {
67
                if (null === $context->getGroups()) {
68
                    $context->setGroups($configuration->getSerializerGroups());
69
                } else {
70
                    $context->setGroups(array_merge($context->getGroups(), $configuration->getSerializerGroups()));
71
                }
72
            }
73 13
            if (true === $configuration->getSerializerEnableMaxDepthChecks()) {
74 1
                $context->enableMaxDepth();
75 12
            } elseif (false === $configuration->getSerializerEnableMaxDepthChecks()) {
76 1
                $context->disableMaxDepth();
77
            }
78
        }
79
80 17
        if (null === $view->getFormat()) {
81 17
            $view->setFormat($request->getRequestFormat());
82
        }
83
84 17
        $response = $this->viewHandler->handle($view, $request);
85
86 17
        $event->setResponse($response);
87 17
    }
88
89 3
    public static function getSubscribedEvents(): array
90
    {
91
        // Must be executed before SensioFrameworkExtraBundle's listener
92
        return array(
93 3
            KernelEvents::VIEW => array('onKernelView', 30),
94
        );
95
    }
96
}
97