1
|
|
|
<?php namespace Limoncello\Flute\Http; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2018 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Contracts\Data\ModelSchemaInfoInterface; |
20
|
|
|
use Limoncello\Contracts\L10n\FormatterFactoryInterface; |
21
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
22
|
|
|
use Limoncello\Flute\Contracts\Encoder\EncoderInterface; |
23
|
|
|
use Limoncello\Flute\Contracts\FactoryInterface; |
24
|
|
|
use Limoncello\Flute\Contracts\Http\JsonApiControllerInterface; |
25
|
|
|
use Limoncello\Flute\Contracts\Schema\JsonSchemasInterface; |
26
|
|
|
use Limoncello\Flute\Http\Traits\DefaultControllerMethodsTrait; |
27
|
|
|
use Psr\Container\ContainerExceptionInterface; |
28
|
|
|
use Psr\Container\ContainerInterface; |
29
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
30
|
|
|
use Psr\Http\Message\ResponseInterface; |
31
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @package Limoncello\Flute |
35
|
|
|
*/ |
36
|
|
|
abstract class JsonApiBaseController implements JsonApiControllerInterface |
37
|
|
|
{ |
38
|
|
|
use DefaultControllerMethodsTrait; |
39
|
|
|
|
40
|
|
|
/** API class name */ |
41
|
|
|
const API_CLASS = null; |
42
|
|
|
|
43
|
|
|
/** JSON API Schema class name */ |
44
|
|
|
const SCHEMA_CLASS = null; |
45
|
|
|
|
46
|
|
|
/** JSON API query validation rules class */ |
47
|
|
|
const ON_INDEX_QUERY_VALIDATION_RULES_CLASS = null; |
48
|
|
|
|
49
|
|
|
/** JSON API query validation rules class */ |
50
|
|
|
const ON_READ_QUERY_VALIDATION_RULES_CLASS = null; |
51
|
|
|
|
52
|
|
|
/** JSON API data validation rules class */ |
53
|
|
|
const ON_CREATE_DATA_VALIDATION_RULES_CLASS = null; |
54
|
|
|
|
55
|
|
|
/** JSON API data validation rules class */ |
56
|
|
|
const ON_UPDATE_DATA_VALIDATION_RULES_CLASS = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
12 |
View Code Duplication |
public static function index( |
|
|
|
|
62
|
|
|
array $routeParams, |
63
|
|
|
ContainerInterface $container, |
64
|
|
|
ServerRequestInterface $request |
65
|
|
|
): ResponseInterface { |
66
|
12 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
67
|
12 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
68
|
12 |
|
static::assertClassValueDefined(static::ON_INDEX_QUERY_VALIDATION_RULES_CLASS); |
|
|
|
|
69
|
|
|
|
70
|
12 |
|
return static::defaultIndexHandler( |
71
|
12 |
|
$request->getQueryParams(), |
72
|
12 |
|
$request->getUri(), |
73
|
12 |
|
static::defaultCreateQueryParser($container, static::ON_INDEX_QUERY_VALIDATION_RULES_CLASS), |
74
|
12 |
|
static::defaultCreateParameterMapper($container, static::SCHEMA_CLASS), |
75
|
12 |
|
static::defaultCreateApi($container, static::API_CLASS), |
76
|
12 |
|
$container->get(SettingsProviderInterface::class), |
77
|
12 |
|
$container->get(JsonSchemasInterface::class), |
78
|
12 |
|
$container->get(EncoderInterface::class) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
1 |
View Code Duplication |
public static function create( |
|
|
|
|
86
|
|
|
array $routeParams, |
87
|
|
|
ContainerInterface $container, |
88
|
|
|
ServerRequestInterface $request |
89
|
|
|
): ResponseInterface { |
90
|
1 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
91
|
1 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
92
|
1 |
|
static::assertClassValueDefined(static::ON_CREATE_DATA_VALIDATION_RULES_CLASS); |
|
|
|
|
93
|
|
|
|
94
|
1 |
|
$response = static::defaultCreateHandler( |
95
|
1 |
|
$request->getUri(), |
96
|
1 |
|
$request->getBody(), |
97
|
1 |
|
static::SCHEMA_CLASS, |
98
|
1 |
|
$container->get(ModelSchemaInfoInterface::class), |
99
|
1 |
|
static::defaultCreateDataParser($container, static::ON_CREATE_DATA_VALIDATION_RULES_CLASS), |
100
|
1 |
|
static::defaultCreateApi($container, static::API_CLASS), |
101
|
1 |
|
$container->get(SettingsProviderInterface::class), |
102
|
1 |
|
$container->get(JsonSchemasInterface::class), |
103
|
1 |
|
$container->get(EncoderInterface::class), |
104
|
1 |
|
$container->get(FactoryInterface::class), |
105
|
1 |
|
$container->get(FormatterFactoryInterface::class) |
106
|
|
|
); |
107
|
|
|
|
108
|
1 |
|
return $response; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
1 |
View Code Duplication |
public static function read( |
|
|
|
|
115
|
|
|
array $routeParams, |
116
|
|
|
ContainerInterface $container, |
117
|
|
|
ServerRequestInterface $request |
118
|
|
|
): ResponseInterface { |
119
|
1 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
120
|
1 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
121
|
1 |
|
static::assertClassValueDefined(static::ON_READ_QUERY_VALIDATION_RULES_CLASS); |
|
|
|
|
122
|
|
|
|
123
|
1 |
|
return static::defaultReadHandler( |
124
|
1 |
|
$routeParams[static::ROUTE_KEY_INDEX], |
125
|
1 |
|
$request->getQueryParams(), |
126
|
1 |
|
$request->getUri(), |
127
|
1 |
|
static::defaultCreateQueryParser($container, static::ON_READ_QUERY_VALIDATION_RULES_CLASS), |
128
|
1 |
|
static::defaultCreateParameterMapper($container, static::SCHEMA_CLASS), |
129
|
1 |
|
static::defaultCreateApi($container, static::API_CLASS), |
130
|
1 |
|
$container->get(SettingsProviderInterface::class), |
131
|
1 |
|
$container->get(JsonSchemasInterface::class), |
132
|
1 |
|
$container->get(EncoderInterface::class) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritdoc |
138
|
|
|
*/ |
139
|
5 |
View Code Duplication |
public static function update( |
|
|
|
|
140
|
|
|
array $routeParams, |
141
|
|
|
ContainerInterface $container, |
142
|
|
|
ServerRequestInterface $request |
143
|
|
|
): ResponseInterface { |
144
|
5 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
145
|
5 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
146
|
5 |
|
static::assertClassValueDefined(static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS); |
|
|
|
|
147
|
|
|
|
148
|
5 |
|
$response = static::defaultUpdateHandler( |
149
|
5 |
|
$routeParams[static::ROUTE_KEY_INDEX], |
150
|
5 |
|
$request->getUri(), |
151
|
5 |
|
$request->getBody(), |
152
|
5 |
|
static::SCHEMA_CLASS, |
153
|
5 |
|
$container->get(ModelSchemaInfoInterface::class), |
154
|
5 |
|
static::defaultCreateDataParser($container, static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS), |
155
|
5 |
|
static::defaultCreateApi($container, static::API_CLASS), |
156
|
5 |
|
$container->get(SettingsProviderInterface::class), |
157
|
5 |
|
$container->get(JsonSchemasInterface::class), |
158
|
5 |
|
$container->get(EncoderInterface::class), |
159
|
5 |
|
$container->get(FactoryInterface::class), |
160
|
5 |
|
$container->get(FormatterFactoryInterface::class) |
161
|
|
|
); |
162
|
|
|
|
163
|
2 |
|
return $response; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritdoc |
168
|
|
|
*/ |
169
|
1 |
|
public static function delete( |
170
|
|
|
array $routeParams, |
171
|
|
|
ContainerInterface $container, |
172
|
|
|
ServerRequestInterface $request |
173
|
|
|
): ResponseInterface { |
174
|
1 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
175
|
|
|
|
176
|
1 |
|
$response = static::defaultDeleteHandler( |
177
|
1 |
|
$routeParams[static::ROUTE_KEY_INDEX], |
178
|
1 |
|
$request->getUri(), |
179
|
1 |
|
static::defaultCreateQueryParser($container, static::ON_READ_QUERY_VALIDATION_RULES_CLASS), |
180
|
1 |
|
static::defaultCreateApi($container, static::API_CLASS), |
181
|
1 |
|
$container->get(SettingsProviderInterface::class), |
182
|
1 |
|
$container->get(JsonSchemasInterface::class), |
183
|
1 |
|
$container->get(EncoderInterface::class) |
184
|
|
|
); |
185
|
|
|
|
186
|
1 |
|
return $response; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $index |
191
|
|
|
* @param string $modelRelName |
192
|
|
|
* @param string $queryValRulesClass |
193
|
|
|
* @param ContainerInterface $container |
194
|
|
|
* @param ServerRequestInterface $request |
195
|
|
|
* |
196
|
|
|
* @return ResponseInterface |
197
|
|
|
* |
198
|
|
|
* @throws ContainerExceptionInterface |
199
|
|
|
* @throws NotFoundExceptionInterface |
200
|
|
|
*/ |
201
|
2 |
View Code Duplication |
protected static function readRelationship( |
|
|
|
|
202
|
|
|
string $index, |
203
|
|
|
string $modelRelName, |
204
|
|
|
string $queryValRulesClass, |
205
|
|
|
ContainerInterface $container, |
206
|
|
|
ServerRequestInterface $request |
207
|
|
|
): ResponseInterface { |
208
|
2 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
209
|
2 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
210
|
|
|
|
211
|
2 |
|
$api = static::defaultCreateApi($container, static::API_CLASS); |
212
|
|
|
$handler = function () use ($api, $index, $modelRelName) { |
213
|
2 |
|
return $api->readRelationship($index, $modelRelName); |
214
|
2 |
|
}; |
215
|
|
|
|
216
|
2 |
|
return static::defaultReadRelationshipWithClosureHandler( |
217
|
2 |
|
$index, |
218
|
2 |
|
$handler, |
219
|
2 |
|
$request->getQueryParams(), |
220
|
2 |
|
$request->getUri(), |
221
|
2 |
|
static::defaultCreateQueryParser($container, $queryValRulesClass), |
222
|
2 |
|
static::defaultCreateParameterMapper($container, static::SCHEMA_CLASS), |
223
|
2 |
|
$api, |
224
|
2 |
|
$container->get(SettingsProviderInterface::class), |
225
|
2 |
|
$container->get(JsonSchemasInterface::class), |
226
|
2 |
|
$container->get(EncoderInterface::class) |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $index |
232
|
|
|
* @param string $modelRelName |
233
|
|
|
* @param string $queryValRulesClass |
234
|
|
|
* @param ContainerInterface $container |
235
|
|
|
* @param ServerRequestInterface $request |
236
|
|
|
* |
237
|
|
|
* @return ResponseInterface |
238
|
|
|
* |
239
|
|
|
* @throws ContainerExceptionInterface |
240
|
|
|
* @throws NotFoundExceptionInterface |
241
|
|
|
*/ |
242
|
1 |
View Code Duplication |
protected static function readRelationshipIdentifiers( |
|
|
|
|
243
|
|
|
string $index, |
244
|
|
|
string $modelRelName, |
245
|
|
|
string $queryValRulesClass, |
246
|
|
|
ContainerInterface $container, |
247
|
|
|
ServerRequestInterface $request |
248
|
|
|
): ResponseInterface { |
249
|
1 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
250
|
1 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
251
|
|
|
|
252
|
1 |
|
$api = static::defaultCreateApi($container, static::API_CLASS); |
253
|
|
|
$handler = function () use ($api, $index, $modelRelName) { |
254
|
1 |
|
return $api->readRelationship($index, $modelRelName); |
255
|
1 |
|
}; |
256
|
|
|
|
257
|
1 |
|
return static::defaultReadRelationshipIdentifiersWithClosureHandler( |
258
|
1 |
|
$index, |
259
|
1 |
|
$handler, |
260
|
1 |
|
$request->getQueryParams(), |
261
|
1 |
|
$request->getUri(), |
262
|
1 |
|
static::defaultCreateQueryParser($container, $queryValRulesClass), |
263
|
1 |
|
static::defaultCreateParameterMapper($container, static::SCHEMA_CLASS), |
264
|
1 |
|
$api, |
265
|
1 |
|
$container->get(SettingsProviderInterface::class), |
266
|
1 |
|
$container->get(JsonSchemasInterface::class), |
267
|
1 |
|
$container->get(EncoderInterface::class) |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param string $parentIndex |
273
|
|
|
* @param string $jsonRelName |
274
|
|
|
* @param string $modelRelName |
275
|
|
|
* @param ContainerInterface $container |
276
|
|
|
* @param ServerRequestInterface $request |
277
|
|
|
* |
278
|
|
|
* @return ResponseInterface |
279
|
|
|
*/ |
280
|
1 |
View Code Duplication |
protected static function addInRelationship( |
|
|
|
|
281
|
|
|
string $parentIndex, |
282
|
|
|
string $jsonRelName, |
283
|
|
|
string $modelRelName, |
284
|
|
|
ContainerInterface $container, |
285
|
|
|
ServerRequestInterface $request |
286
|
|
|
): ResponseInterface { |
287
|
1 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
288
|
1 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
289
|
1 |
|
static::assertClassValueDefined(static::ON_READ_QUERY_VALIDATION_RULES_CLASS); |
|
|
|
|
290
|
1 |
|
static::assertClassValueDefined(static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS); |
|
|
|
|
291
|
|
|
|
292
|
1 |
|
return static::defaultAddInRelationshipHandler( |
293
|
1 |
|
$parentIndex, |
294
|
1 |
|
$jsonRelName, |
295
|
1 |
|
$modelRelName, |
296
|
1 |
|
$request->getUri(), |
297
|
1 |
|
$request->getBody(), |
298
|
1 |
|
static::SCHEMA_CLASS, |
299
|
1 |
|
$container->get(ModelSchemaInfoInterface::class), |
300
|
1 |
|
static::defaultCreateQueryParser($container, static::ON_READ_QUERY_VALIDATION_RULES_CLASS), |
301
|
1 |
|
static::defaultCreateDataParser($container, static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS), |
302
|
1 |
|
static::defaultCreateApi($container, static::API_CLASS), |
303
|
1 |
|
$container->get(SettingsProviderInterface::class), |
304
|
1 |
|
$container->get(JsonSchemasInterface::class), |
305
|
1 |
|
$container->get(EncoderInterface::class), |
306
|
1 |
|
$container->get(FactoryInterface::class), |
307
|
1 |
|
$container->get(FormatterFactoryInterface::class) |
308
|
|
|
); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param string $parentIndex |
313
|
|
|
* @param string $jsonRelName |
314
|
|
|
* @param string $modelRelName |
315
|
|
|
* @param ContainerInterface $container |
316
|
|
|
* @param ServerRequestInterface $request |
317
|
|
|
* |
318
|
|
|
* @return ResponseInterface |
319
|
|
|
*/ |
320
|
1 |
View Code Duplication |
protected static function deleteInRelationship( |
|
|
|
|
321
|
|
|
string $parentIndex, |
322
|
|
|
string $jsonRelName, |
323
|
|
|
string $modelRelName, |
324
|
|
|
ContainerInterface $container, |
325
|
|
|
ServerRequestInterface $request |
326
|
|
|
): ResponseInterface { |
327
|
1 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
328
|
1 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
329
|
1 |
|
static::assertClassValueDefined(static::ON_READ_QUERY_VALIDATION_RULES_CLASS); |
|
|
|
|
330
|
1 |
|
static::assertClassValueDefined(static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS); |
|
|
|
|
331
|
|
|
|
332
|
1 |
|
return static::defaultDeleteInRelationshipHandler( |
333
|
1 |
|
$parentIndex, |
334
|
1 |
|
$jsonRelName, |
335
|
1 |
|
$modelRelName, |
336
|
1 |
|
$request->getUri(), |
337
|
1 |
|
$request->getBody(), |
338
|
1 |
|
static::SCHEMA_CLASS, |
339
|
1 |
|
$container->get(ModelSchemaInfoInterface::class), |
340
|
1 |
|
static::defaultCreateQueryParser($container, static::ON_READ_QUERY_VALIDATION_RULES_CLASS), |
341
|
1 |
|
static::defaultCreateDataParser($container, static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS), |
342
|
1 |
|
static::defaultCreateApi($container, static::API_CLASS), |
343
|
1 |
|
$container->get(SettingsProviderInterface::class), |
344
|
1 |
|
$container->get(JsonSchemasInterface::class), |
345
|
1 |
|
$container->get(EncoderInterface::class), |
346
|
1 |
|
$container->get(FactoryInterface::class), |
347
|
1 |
|
$container->get(FormatterFactoryInterface::class) |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string $parentIndex |
353
|
|
|
* @param string $jsonRelName |
354
|
|
|
* @param string $modelRelName |
355
|
|
|
* @param ContainerInterface $container |
356
|
|
|
* @param ServerRequestInterface $request |
357
|
|
|
* |
358
|
|
|
* @return ResponseInterface |
359
|
|
|
*/ |
360
|
1 |
View Code Duplication |
protected static function replaceInRelationship( |
|
|
|
|
361
|
|
|
string $parentIndex, |
362
|
|
|
string $jsonRelName, |
363
|
|
|
string $modelRelName, |
364
|
|
|
ContainerInterface $container, |
365
|
|
|
ServerRequestInterface $request |
366
|
|
|
): ResponseInterface { |
367
|
1 |
|
static::assertClassValueDefined(static::API_CLASS); |
|
|
|
|
368
|
1 |
|
static::assertClassValueDefined(static::SCHEMA_CLASS); |
|
|
|
|
369
|
1 |
|
static::assertClassValueDefined(static::ON_READ_QUERY_VALIDATION_RULES_CLASS); |
|
|
|
|
370
|
1 |
|
static::assertClassValueDefined(static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS); |
|
|
|
|
371
|
|
|
|
372
|
1 |
|
return static::defaultReplaceInRelationship( |
373
|
1 |
|
$parentIndex, |
374
|
1 |
|
$jsonRelName, |
375
|
1 |
|
$modelRelName, |
376
|
1 |
|
$request->getUri(), |
377
|
1 |
|
$request->getBody(), |
378
|
1 |
|
static::SCHEMA_CLASS, |
379
|
1 |
|
$container->get(ModelSchemaInfoInterface::class), |
380
|
1 |
|
static::defaultCreateQueryParser($container, static::ON_READ_QUERY_VALIDATION_RULES_CLASS), |
381
|
1 |
|
static::defaultCreateDataParser($container, static::ON_UPDATE_DATA_VALIDATION_RULES_CLASS), |
382
|
1 |
|
static::defaultCreateApi($container, static::API_CLASS), |
383
|
1 |
|
$container->get(SettingsProviderInterface::class), |
384
|
1 |
|
$container->get(JsonSchemasInterface::class), |
385
|
1 |
|
$container->get(EncoderInterface::class), |
386
|
1 |
|
$container->get(FactoryInterface::class), |
387
|
1 |
|
$container->get(FormatterFactoryInterface::class) |
388
|
|
|
); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
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.