1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Saikootau\ApiBundle\Event\Listener; |
6
|
|
|
|
7
|
|
|
use Saikootau\ApiBundle\Http\ResourceResponse; |
8
|
|
|
use Saikootau\ApiBundle\MediaType\Exception\NonNegotiableMediaTypeException; |
9
|
|
|
use Saikootau\ApiBundle\MediaType\MediaTypeNegotiator; |
10
|
|
|
use Saikootau\ApiBundle\MediaType\MediaTypes; |
11
|
|
|
use Saikootau\ApiBundle\Serializer\Serializer; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; |
16
|
|
|
|
17
|
|
|
class ResponseListener |
18
|
|
|
{ |
19
|
|
|
private $defaultContentType; |
20
|
|
|
private $mediaTypeNegotiator; |
21
|
|
|
|
22
|
|
|
private static $noContentMethods = [ |
23
|
|
|
'HEAD', |
24
|
|
|
]; |
25
|
|
|
|
26
|
5 |
|
public function __construct(MediaTypeNegotiator $mediaTypeNegotiator, string $defaultContentType) |
27
|
|
|
{ |
28
|
5 |
|
$this->mediaTypeNegotiator = $mediaTypeNegotiator; |
29
|
5 |
|
$this->defaultContentType = $defaultContentType; |
30
|
5 |
|
} |
31
|
|
|
|
32
|
5 |
|
public function onKernelView(GetResponseForControllerResultEvent $event): void |
33
|
|
|
{ |
34
|
5 |
|
$controllerResult = $event->getControllerResult(); |
35
|
|
|
|
36
|
5 |
|
if ($controllerResult instanceof Response) { |
37
|
1 |
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
$serializer = $this->getSerializer($event->getRequest()); |
41
|
3 |
|
$body = ''; |
42
|
3 |
|
if ($this->isContentAllowed($event->getRequest()->getMethod()) && null !== $controllerResult) { |
43
|
3 |
|
$body = $serializer->serialize($this->getResource($controllerResult)); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
$event->setResponse(new Response( |
47
|
3 |
|
$body, |
48
|
3 |
|
$this->getStatusCode($event->getRequest(), $controllerResult), |
49
|
3 |
|
$this->getHeaders($controllerResult, $serializer->getContentType()) |
50
|
|
|
)); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the actual resource from the controller result. |
55
|
|
|
* |
56
|
|
|
* @param object $controllerResult |
57
|
|
|
* |
58
|
|
|
* @return object |
59
|
|
|
*/ |
60
|
3 |
|
private function getResource(object $controllerResult): object |
61
|
|
|
{ |
62
|
3 |
|
if ($controllerResult instanceof ResourceResponse) { |
63
|
1 |
|
return $controllerResult->getResource(); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
return $controllerResult; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the status code to return from the controller result. |
71
|
|
|
* Defaults to 202 for POST, PUT and 200 for every other method. |
72
|
|
|
* |
73
|
|
|
* @param Request $request |
74
|
|
|
* @param object|null $controllerResult |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
3 |
|
private function getStatusCode(Request $request, ?object $controllerResult): int |
79
|
|
|
{ |
80
|
3 |
|
if ($controllerResult instanceof ResourceResponse) { |
81
|
1 |
|
return $controllerResult->getStatusCode(); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return in_array($request->getMethod(), ['POST', 'PUT']) ? Response::HTTP_ACCEPTED : Response::HTTP_OK; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Generates an array of headers for the given controller result. |
89
|
|
|
* |
90
|
|
|
* @param object|null $controllerResult |
91
|
|
|
* @param string $contentType |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
3 |
|
private function getHeaders(?object $controllerResult, string $contentType): array |
96
|
|
|
{ |
97
|
3 |
|
$headers = ['Content-Type' => $contentType]; |
98
|
3 |
|
if ($controllerResult instanceof ResourceResponse) { |
99
|
1 |
|
$headers = array_merge($controllerResult->getHeaders(), $headers); |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
return $headers; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks if the response should contain any content. |
107
|
|
|
* |
108
|
|
|
* @param string $method |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
3 |
|
private function isContentAllowed(string $method): bool |
113
|
|
|
{ |
114
|
3 |
|
return !in_array($method, self::$noContentMethods); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get a matching serializer for the given request's acceptable content types. |
119
|
|
|
* |
120
|
|
|
* @param Request $request |
121
|
|
|
* |
122
|
|
|
* @return Serializer |
123
|
|
|
* |
124
|
|
|
* @throws NotAcceptableHttpException |
125
|
|
|
*/ |
126
|
4 |
|
protected function getSerializer(Request $request): Serializer |
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
/** @var Serializer $serializer */ |
130
|
4 |
|
$serializer = $this->mediaTypeNegotiator->negotiate(...$this->getRequestAcceptableContentTypes($request)); |
131
|
1 |
|
} catch (NonNegotiableMediaTypeException $exception) { |
132
|
1 |
|
throw new NotAcceptableHttpException( |
133
|
1 |
|
sprintf( |
134
|
1 |
|
'Deliverable content types are: %s', |
135
|
1 |
|
implode(', ', $this->mediaTypeNegotiator->getSupportedMediaTypes()) |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
return $serializer; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns an array of acceptable content types for the given request. |
145
|
|
|
* |
146
|
|
|
* @param Request $request |
147
|
|
|
* |
148
|
|
|
* @return string[] |
149
|
|
|
*/ |
150
|
4 |
|
private function getRequestAcceptableContentTypes(Request $request): array |
151
|
|
|
{ |
152
|
4 |
|
$acceptableContentTypes = $request->getAcceptableContentTypes(); |
153
|
|
|
|
154
|
4 |
|
if (false !== ($anyIndex = array_search(MediaTypes::TYPE_APPLICATION_ANY, $acceptableContentTypes))) { |
155
|
1 |
|
$acceptableContentTypes[$anyIndex] = $this->defaultContentType; |
156
|
|
|
} |
157
|
|
|
|
158
|
4 |
|
return $acceptableContentTypes; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|