|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Common Component. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Component\Common\EventListener; |
|
16
|
|
|
|
|
17
|
|
|
use FOS\RestBundle\Context\Context; |
|
18
|
|
|
use FOS\RestBundle\View\View; |
|
19
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
|
20
|
|
|
use SWP\Component\Common\Factory\KnpPaginatorRepresentationFactory; |
|
21
|
|
|
use SWP\Component\Common\Response\ResourcesListResponseInterface; |
|
22
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
|
23
|
|
|
use SWP\Component\Common\Response\ResponseContextInterface; |
|
24
|
|
|
use SWP\Component\Common\Response\SingleResourceResponseInterface; |
|
25
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
|
26
|
|
|
|
|
27
|
|
|
final class ResourceResponseListener |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ViewHandlerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $viewHandler; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* ResourcesListResponseListener constructor. |
|
36
|
|
|
* |
|
37
|
79 |
|
* @param ViewHandlerInterface $viewHandler |
|
38
|
|
|
*/ |
|
39
|
79 |
|
public function __construct(ViewHandlerInterface $viewHandler) |
|
40
|
79 |
|
{ |
|
41
|
|
|
$this->viewHandler = $viewHandler; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
78 |
|
* @param GetResponseForControllerResultEvent $event |
|
46
|
|
|
*/ |
|
47
|
78 |
|
public function onKernelView(GetResponseForControllerResultEvent $event) |
|
48
|
|
|
{ |
|
49
|
78 |
|
$controllerResult = $event->getControllerResult(); |
|
50
|
20 |
|
if (null === $controllerResult) { |
|
51
|
|
|
return; |
|
52
|
20 |
|
} |
|
53
|
20 |
|
|
|
54
|
20 |
|
/** @var ResponseContext $responseContext */ |
|
55
|
20 |
|
$responseContext = $controllerResult->getResponseContext(); |
|
56
|
20 |
|
if ($controllerResult instanceof ResourcesListResponseInterface) { |
|
57
|
|
|
if (ResponseContextInterface::INTENTION_API === $responseContext->getIntention()) { |
|
58
|
|
|
$factory = new KnpPaginatorRepresentationFactory(); |
|
59
|
|
|
$representation = $factory->createRepresentation($controllerResult->getResources(), $event->getRequest()); |
|
60
|
72 |
|
|
|
61
|
72 |
|
$view = View::create($representation, $responseContext->getStatusCode()); |
|
62
|
72 |
|
$view = $this->setSerializationGroups($view); |
|
63
|
72 |
|
$event->setResponse($this->viewHandler->handle( |
|
64
|
|
|
$view |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
78 |
|
} elseif ($controllerResult instanceof SingleResourceResponseInterface) { |
|
68
|
|
|
if (ResponseContextInterface::INTENTION_API === $responseContext->getIntention()) { |
|
69
|
|
|
$view = View::create($controllerResult->getResource(), $responseContext->getStatusCode()); |
|
70
|
|
|
$view = $this->setSerializationGroups($view); |
|
71
|
|
|
|
|
72
|
|
|
$event->setResponse($this->viewHandler->handle( |
|
73
|
|
|
$view |
|
74
|
|
|
)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->setHeaders($event, $responseContext); |
|
79
|
|
|
$this->clearCookies($event, $responseContext); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param GetResponseForControllerResultEvent $event |
|
84
|
|
|
* @param ResponseContext $responseContext |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
private function setHeaders(GetResponseForControllerResultEvent $event, ResponseContext $responseContext) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
if (count($responseContext->getHeaders()) > 0) { |
|
89
|
|
|
$response = $event->getResponse(); |
|
90
|
|
|
foreach ($responseContext->getHeaders() as $key => $value) { |
|
91
|
|
|
$response->headers->set($key, $value); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$event->setResponse($response); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param GetResponseForControllerResultEvent $event |
|
100
|
|
|
* @param ResponseContext $responseContext |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
private function clearCookies(GetResponseForControllerResultEvent $event, ResponseContext $responseContext) |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
if (count($responseContext->getClearedCookies()) > 0) { |
|
105
|
|
|
$response = $event->getResponse(); |
|
106
|
|
|
foreach ($responseContext->getClearedCookies() as $key) { |
|
107
|
|
|
$response->headers->clearCookie($key); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$event->setResponse($response); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function setSerializationGroups(View $view): View |
|
115
|
|
|
{ |
|
116
|
|
|
$context = new Context(); |
|
117
|
|
|
$context->setGroups(['Default', 'api']); |
|
118
|
|
|
$view->setContext($context); |
|
119
|
|
|
|
|
120
|
|
|
return $view; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.