|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Manager; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\ApiHttp\Error\Error; |
|
8
|
|
|
use Chubbyphp\ApiHttp\Error\ErrorInterface; |
|
9
|
|
|
use Chubbyphp\ApiHttp\Factory\ResponseFactoryInterface; |
|
10
|
|
|
use Chubbyphp\Deserialization\DeserializerInterface; |
|
11
|
|
|
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; |
|
12
|
|
|
use Chubbyphp\Serialization\SerializerInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
14
|
|
|
|
|
15
|
|
|
final class ResponseManager implements ResponseManagerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var DeserializerInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $deserializer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ResponseFactoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $responseFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var SerializerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $serializer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param DeserializerInterface $deserializer |
|
34
|
|
|
* @param ResponseFactoryInterface $responseFactory |
|
35
|
|
|
* @param SerializerInterface $serializer |
|
36
|
|
|
*/ |
|
37
|
17 |
|
public function __construct( |
|
38
|
|
|
DeserializerInterface $deserializer, |
|
39
|
|
|
ResponseFactoryInterface $responseFactory, |
|
40
|
|
|
SerializerInterface $serializer |
|
41
|
|
|
) { |
|
42
|
17 |
|
$this->deserializer = $deserializer; |
|
43
|
17 |
|
$this->responseFactory = $responseFactory; |
|
44
|
17 |
|
$this->serializer = $serializer; |
|
45
|
17 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param object $object |
|
49
|
|
|
* @param string $accept |
|
50
|
|
|
* @param int $status |
|
51
|
|
|
* @param NormalizerContextInterface|null $context |
|
52
|
|
|
* |
|
53
|
|
|
* @return Response |
|
54
|
|
|
*/ |
|
55
|
12 |
|
public function create( |
|
56
|
|
|
$object, |
|
57
|
|
|
string $accept, |
|
58
|
|
|
int $status = 200, |
|
59
|
|
|
NormalizerContextInterface $context = null |
|
60
|
|
|
): Response { |
|
61
|
12 |
|
$body = $this->serializer->serialize($object, $accept, $context); |
|
62
|
|
|
|
|
63
|
12 |
|
$response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept); |
|
64
|
12 |
|
$response->getBody()->write($body); |
|
65
|
|
|
|
|
66
|
12 |
|
return $response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $accept |
|
71
|
|
|
* @param int $status |
|
72
|
|
|
* |
|
73
|
|
|
* @return Response |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function createEmpty(string $accept, int $status = 204): Response |
|
76
|
|
|
{ |
|
77
|
2 |
|
return $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $location |
|
82
|
|
|
* @param int $status |
|
83
|
|
|
* |
|
84
|
|
|
* @return Response |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function createRedirect(string $location, int $status = 307): Response |
|
87
|
|
|
{ |
|
88
|
2 |
|
return $this->responseFactory->createResponse($status)->withHeader('Location', $location); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param ErrorInterface $error |
|
93
|
|
|
* @param string $accept |
|
94
|
|
|
* @param int $status |
|
95
|
|
|
* @param NormalizerContextInterface|null $context |
|
96
|
|
|
* |
|
97
|
|
|
* @return Response |
|
98
|
|
|
*/ |
|
99
|
10 |
|
public function createFromError( |
|
100
|
|
|
ErrorInterface $error, |
|
101
|
|
|
string $accept, |
|
102
|
|
|
int $status = 400, |
|
103
|
|
|
NormalizerContextInterface $context = null |
|
104
|
|
|
): Response { |
|
105
|
10 |
|
return $this->create($error, $accept, $status, $context); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $accept |
|
110
|
|
|
* @param NormalizerContextInterface|null $context |
|
111
|
|
|
* |
|
112
|
|
|
* @return Response |
|
113
|
|
|
*/ |
|
114
|
2 |
|
public function createNotAuthenticated(string $accept, NormalizerContextInterface $context = null): Response |
|
115
|
|
|
{ |
|
116
|
2 |
|
return $this->createFromError(new Error( |
|
117
|
2 |
|
Error::SCOPE_HEADER, |
|
118
|
2 |
|
'not_authenticated', |
|
119
|
2 |
|
'missing or invalid authentication token to perform the request', |
|
120
|
2 |
|
'authentication' |
|
121
|
2 |
|
), $accept, 401, $context); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $accept |
|
126
|
|
|
* @param NormalizerContextInterface|null $context |
|
127
|
|
|
* |
|
128
|
|
|
* @return Response |
|
129
|
|
|
*/ |
|
130
|
2 |
|
public function createNotAuthorized(string $accept, NormalizerContextInterface $context = null): Response |
|
131
|
|
|
{ |
|
132
|
2 |
|
return $this->createFromError(new Error( |
|
133
|
2 |
|
Error::SCOPE_HEADER, |
|
134
|
2 |
|
'permission_denied', |
|
135
|
2 |
|
'missing authorization to perform request', |
|
136
|
2 |
|
'authorization' |
|
137
|
2 |
|
), $accept, 403, $context); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $reference |
|
142
|
|
|
* @param array $arguments |
|
143
|
|
|
* @param string $accept |
|
144
|
|
|
* @param NormalizerContextInterface|null $context |
|
145
|
|
|
* |
|
146
|
|
|
* @return Response |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function createResourceNotFound( |
|
149
|
|
|
string $reference, |
|
150
|
|
|
array $arguments, |
|
151
|
|
|
string $accept, |
|
152
|
|
|
NormalizerContextInterface $context = null |
|
153
|
|
|
): Response { |
|
154
|
2 |
|
return $this->createFromError(new Error( |
|
155
|
2 |
|
Error::SCOPE_RESOURCE, |
|
156
|
2 |
|
'resource_not_found', |
|
157
|
2 |
|
'the requested resource cannot be found', |
|
158
|
2 |
|
$reference, |
|
159
|
2 |
|
$arguments |
|
160
|
2 |
|
), $accept, 404, $context); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string $accept |
|
165
|
|
|
* |
|
166
|
|
|
* @return Response |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function createAcceptNotSupported(string $accept): Response |
|
169
|
|
|
{ |
|
170
|
1 |
|
return $this->responseFactory->createResponse(406)->withHeader('X-Not-Acceptable', sprintf( |
|
171
|
1 |
|
'Accept "%s" is not supported, supported are %s', |
|
172
|
1 |
|
$accept, |
|
173
|
1 |
|
'"'.implode('", "', $this->serializer->getContentTypes()).'"' |
|
174
|
|
|
)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $contentType |
|
179
|
|
|
* @param string $accept |
|
180
|
|
|
* @param NormalizerContextInterface|null $context |
|
181
|
|
|
* |
|
182
|
|
|
* @return Response |
|
183
|
|
|
*/ |
|
184
|
2 |
|
public function createContentTypeNotSupported( |
|
185
|
|
|
string $contentType, |
|
186
|
|
|
string $accept, |
|
187
|
|
|
NormalizerContextInterface $context = null |
|
188
|
|
|
): Response { |
|
189
|
2 |
|
return $this->createFromError(new Error( |
|
190
|
2 |
|
Error::SCOPE_HEADER, |
|
191
|
2 |
|
'contentype_not_supported', |
|
192
|
2 |
|
'the given content type is not supported', |
|
193
|
2 |
|
'content-type', |
|
194
|
|
|
[ |
|
195
|
2 |
|
'contentType' => $contentType, |
|
196
|
2 |
|
'supportedContentTypes' => $this->deserializer->getContentTypes(), |
|
197
|
|
|
] |
|
198
|
2 |
|
), $accept, 415, $context); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|