Completed
Push — master ( 3a869f...beb3fd )
by John
12s
created

ResourceEndpoint::optionsAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace LunixREST\Server\Router\Endpoint;
3
4
use LunixREST\Server\APIRequest\APIRequest;
5
use LunixREST\Server\APIResponse\APIResponseData;
6
use LunixREST\Server\Router\Endpoint\Exceptions\ElementConflictException;
7
use LunixREST\Server\Router\Endpoint\Exceptions\ElementNotFoundException;
8
use LunixREST\Server\Router\Endpoint\Exceptions\EndpointExecutionException;
9
use LunixREST\Server\Router\Endpoint\Exceptions\InvalidRequestException;
10
use LunixREST\Server\Router\Endpoint\Exceptions\UnsupportedMethodException;
11
use LunixREST\Server\Router\Endpoint\ResourceEndpoint\Exceptions\UnableToCreateAPIResponseDataException;
12
use LunixREST\Server\Router\Endpoint\ResourceEndpoint\Exceptions\UnableToCreateResourceParametersException;
13
use LunixREST\Server\Router\Endpoint\ResourceEndpoint\ResourceAPIResponseDataFactory;
14
use LunixREST\Server\Router\Endpoint\ResourceEndpoint\ResourceParameters;
15
use LunixREST\Server\Router\Endpoint\ResourceEndpoint\ResourceParametersFactory;
16
use LunixREST\Server\Router\Endpoint\ResourceEndpoint\Resource;
17
18
/**
19
 * Class ResourceEndpoint
20
 * @package LunixREST\Server\Router\Endpoint
21
 */
22
abstract class ResourceEndpoint implements Endpoint
23
{
24
    /**
25
     * @var ResourceParametersFactory
26
     */
27
    protected $resourceParametersFactory;
28
    /**
29
     * @var ResourceAPIResponseDataFactory
30
     */
31
    protected $resourceAPIResponseDataFactory;
32
33
    /**
34
     * ResourceEndpoint constructor.
35
     * @param ResourceParametersFactory $parametersFactory
36
     * @param ResourceAPIResponseDataFactory $APIResponseDataFactory
37
     */
38 80
    public function __construct(
39
        ResourceParametersFactory $parametersFactory,
40
        ResourceAPIResponseDataFactory $APIResponseDataFactory
41
    )
42
    {
43 80
        $this->resourceParametersFactory = $parametersFactory;
44 80
        $this->resourceAPIResponseDataFactory = $APIResponseDataFactory;
45 80
    }
46
47
    /**
48
     * @param APIRequest $request
49
     * @return APIResponseData
50
     * @throws EndpointExecutionException
51
     * @throws UnsupportedMethodException
52
     * @throws ElementNotFoundException
53
     * @throws InvalidRequestException
54
     * @throws UnableToCreateAPIResponseDataException
55
     * @throws UnableToCreateResourceParametersException
56
     */
57 6
    public function get(APIRequest $request): APIResponseData
58
    {
59 6
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
60
61 5
        $resource = $this->getResource($resourceParameters);
62
63 1
        return $this->resourceAPIResponseDataFactory->toAPIResponseData($resource);
1 ignored issue
show
Documentation introduced by
$resource is of type object<LunixREST\Server\...ourceEndpoint\Resource>, but the function expects a null|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
    }
65
66
    /**
67
     * @param APIRequest $request
68
     * @return APIResponseData
69
     * @throws EndpointExecutionException
70
     * @throws UnsupportedMethodException
71
     * @throws InvalidRequestException
72
     * @throws UnableToCreateAPIResponseDataException
73
     * @throws UnableToCreateResourceParametersException
74
     */
75 6
    public function getAll(APIRequest $request): APIResponseData
76
    {
77 6
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
78
79 5
        $resources = $this->getResources($resourceParameters);
80
81 1
        return $this->resourceAPIResponseDataFactory->multipleToAPIResponseData($resources);
1 ignored issue
show
Documentation introduced by
$resources is of type array<integer,object<Lun...urceEndpoint\Resource>>, but the function expects a null|array<integer,resource>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
    }
83
84
    /**
85
     * @param APIRequest $request
86
     * @return APIResponseData
87
     * @throws EndpointExecutionException
88
     * @throws UnsupportedMethodException
89
     * @throws ElementNotFoundException
90
     * @throws ElementConflictException
91
     * @throws InvalidRequestException
92
     * @throws UnableToCreateAPIResponseDataException
93
     * @throws UnableToCreateResourceParametersException
94
     */
95 7
    public function post(APIRequest $request): APIResponseData
96
    {
97 7
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
98
99 6
        $resource = $this->postResource($resourceParameters);
100
101 1
        return $this->resourceAPIResponseDataFactory->toAPIResponseData($resource);
1 ignored issue
show
Documentation introduced by
$resource is of type object<LunixREST\Server\...ourceEndpoint\Resource>, but the function expects a null|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
    }
103
104
    /**
105
     * @param APIRequest $request
106
     * @return APIResponseData
107
     * @throws EndpointExecutionException
108
     * @throws UnsupportedMethodException
109
     * @throws InvalidRequestException
110
     * @throws ElementConflictException
111
     * @throws UnableToCreateAPIResponseDataException
112
     * @throws UnableToCreateResourceParametersException
113
     */
114 7
    public function postAll(APIRequest $request): APIResponseData
115
    {
116 7
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
117
118 6
        $resource = $this->postResources($resourceParameters);
119
120 1
        return $this->resourceAPIResponseDataFactory->toAPIResponseData($resource);
1 ignored issue
show
Documentation introduced by
$resource is of type object<LunixREST\Server\...ourceEndpoint\Resource>, but the function expects a null|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
    }
122
123
    /**
124
     * @param APIRequest $request
125
     * @return APIResponseData
126
     * @throws EndpointExecutionException
127
     * @throws UnsupportedMethodException
128
     * @throws ElementNotFoundException
129
     * @throws InvalidRequestException
130
     * @throws ElementConflictException
131
     * @throws UnableToCreateAPIResponseDataException
132
     * @throws UnableToCreateResourceParametersException
133
     */
134 7
    public function put(APIRequest $request): APIResponseData
135
    {
136 7
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
137
138 6
        $resource = $this->putResource($resourceParameters);
139
140 1
        return $this->resourceAPIResponseDataFactory->toAPIResponseData($resource);
1 ignored issue
show
Documentation introduced by
$resource is of type object<LunixREST\Server\...ourceEndpoint\Resource>, but the function expects a null|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
    }
142
143
    /**
144
     * @param APIRequest $request
145
     * @return APIResponseData
146
     * @throws EndpointExecutionException
147
     * @throws UnsupportedMethodException
148
     * @throws InvalidRequestException
149
     * @throws ElementConflictException
150
     * @throws UnableToCreateAPIResponseDataException
151
     * @throws UnableToCreateResourceParametersException
152
     */
153 7
    public function putAll(APIRequest $request): APIResponseData
154
    {
155 7
        $resourceParameters = $this->resourceParametersFactory->createMultipleResourceParameters($request);
156
157 6
        $resources = $this->putResources($resourceParameters);
158
159 1
        return $this->resourceAPIResponseDataFactory->multipleToAPIResponseData($resources);
0 ignored issues
show
Documentation introduced by
$resources is of type array<integer,object<Lun...urceEndpoint\Resource>>, but the function expects a null|array<integer,resource>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
    }
161
162
    /**
163
     * @param APIRequest $request
164
     * @return APIResponseData
165
     * @throws EndpointExecutionException
166
     * @throws UnsupportedMethodException
167
     * @throws ElementNotFoundException
168
     * @throws InvalidRequestException
169
     * @throws ElementConflictException
170
     * @throws UnableToCreateAPIResponseDataException
171
     * @throws UnableToCreateResourceParametersException
172
     */
173 7
    public function patch(APIRequest $request): APIResponseData
174
    {
175 7
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
176
177 6
        $resource = $this->patchResource($resourceParameters);
178
179 1
        return $this->resourceAPIResponseDataFactory->toAPIResponseData($resource);
1 ignored issue
show
Documentation introduced by
$resource is of type object<LunixREST\Server\...ourceEndpoint\Resource>, but the function expects a null|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
    }
181
182
    /**
183
     * @param APIRequest $request
184
     * @return APIResponseData
185
     * @throws EndpointExecutionException
186
     * @throws UnsupportedMethodException
187
     * @throws InvalidRequestException
188
     * @throws ElementConflictException
189
     * @throws UnableToCreateAPIResponseDataException
190
     * @throws UnableToCreateResourceParametersException
191
     */
192 7
    public function patchAll(APIRequest $request): APIResponseData
193
    {
194 7
        $resourceParameters = $this->resourceParametersFactory->createMultipleResourceParameters($request);
195
196 6
        $resources = $this->patchResources($resourceParameters);
197
198 1
        return $this->resourceAPIResponseDataFactory->multipleToAPIResponseData($resources);
0 ignored issues
show
Documentation introduced by
$resources is of type array<integer,object<Lun...urceEndpoint\Resource>>, but the function expects a null|array<integer,resource>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
199
    }
200
201
    /**
202
     * @param APIRequest $request
203
     * @return APIResponseData
204
     * @throws EndpointExecutionException
205
     * @throws UnsupportedMethodException
206
     * @throws ElementNotFoundException
207
     * @throws InvalidRequestException
208
     * @throws UnableToCreateAPIResponseDataException
209
     * @throws UnableToCreateResourceParametersException
210
     */
211 7
    public function options(APIRequest $request): APIResponseData
212
    {
213 7
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
214
215 6
        $resource = $this->optionsResource($resourceParameters);
216
217 1
        return $this->resourceAPIResponseDataFactory->toAPIResponseData($resource);
1 ignored issue
show
Documentation introduced by
$resource is of type object<LunixREST\Server\...ourceEndpoint\Resource>, but the function expects a null|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
218
    }
219
220
    /**
221
     * @param APIRequest $request
222
     * @return APIResponseData
223
     * @throws EndpointExecutionException
224
     * @throws UnsupportedMethodException
225
     * @throws InvalidRequestException
226
     * @throws UnableToCreateAPIResponseDataException
227
     * @throws UnableToCreateResourceParametersException
228
     */
229 7
    public function optionsAll(APIRequest $request): APIResponseData
230
    {
231 7
        $resourceParameters = $this->resourceParametersFactory->createMultipleResourceParameters($request);
232
233 6
        $resources = $this->optionsResources($resourceParameters);
234
235 1
        return $this->resourceAPIResponseDataFactory->multipleToAPIResponseData($resources);
0 ignored issues
show
Documentation introduced by
$resources is of type array<integer,object<Lun...urceEndpoint\Resource>>, but the function expects a null|array<integer,resource>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
236
    }
237
238
    /**
239
     * @param APIRequest $request
240
     * @return APIResponseData
241
     * @throws EndpointExecutionException
242
     * @throws UnsupportedMethodException
243
     * @throws ElementNotFoundException
244
     * @throws InvalidRequestException
245
     * @throws UnableToCreateAPIResponseDataException
246
     * @throws UnableToCreateResourceParametersException
247
     */
248 6
    public function delete(APIRequest $request): APIResponseData
249
    {
250 6
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
251
252 5
        $resource = $this->deleteResource($resourceParameters);
253
254 1
        return $this->resourceAPIResponseDataFactory->toAPIResponseData($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource defined by $this->deleteResource($resourceParameters) on line 252 can also be of type object<LunixREST\Server\...ourceEndpoint\Resource>; however, LunixREST\Server\Router\...ry::toAPIResponseData() does only seem to accept null|resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
255
    }
256
257
    /**
258
     * @param APIRequest $request
259
     * @return APIResponseData
260
     * @throws EndpointExecutionException
261
     * @throws UnsupportedMethodException
262
     * @throws InvalidRequestException
263
     * @throws UnableToCreateAPIResponseDataException
264
     * @throws UnableToCreateResourceParametersException
265
     */
266 6
    public function deleteAll(APIRequest $request): APIResponseData
267
    {
268 6
        $resourceParameters = $this->resourceParametersFactory->createResourceParameters($request);
269
270 5
        $resources = $this->deleteResources($resourceParameters);
271
272 1
        return $this->resourceAPIResponseDataFactory->multipleToAPIResponseData($resources);
0 ignored issues
show
Bug introduced by
It seems like $resources defined by $this->deleteResources($resourceParameters) on line 270 can also be of type array<integer,object<Lun...urceEndpoint\Resource>>; however, LunixREST\Server\Router\...ipleToAPIResponseData() does only seem to accept null|array<integer,resource>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
273
    }
274
275
    /**
276
     * @param ResourceParameters $parameters
277
     * @return Resource
278
     * @throws EndpointExecutionException
279
     * @throws UnsupportedMethodException
280
     * @throws ElementNotFoundException
281
     * @throws InvalidRequestException
282
     */
283
    abstract protected function getResource(ResourceParameters $parameters): Resource;
284
285
    /**
286
     * @param ResourceParameters $parameters
287
     * @return Resource[]
288
     * @throws EndpointExecutionException
289
     * @throws UnsupportedMethodException
290
     * @throws ElementNotFoundException
291
     * @throws InvalidRequestException
292
     */
293
    abstract protected function getResources(ResourceParameters $parameters): array;
294
295
    /**
296
     * @param ResourceParameters $parameters
297
     * @return Resource
298
     * @throws EndpointExecutionException
299
     * @throws UnsupportedMethodException
300
     * @throws ElementNotFoundException
301
     * @throws ElementConflictException
302
     * @throws InvalidRequestException
303
     */
304
    abstract protected function postResource(ResourceParameters $parameters): Resource;
305
306
    /**
307
     * @param ResourceParameters $parameters
308
     * @return Resource
309
     * @throws EndpointExecutionException
310
     * @throws UnsupportedMethodException
311
     * @throws ElementConflictException
312
     * @throws InvalidRequestException
313
     */
314
    abstract protected function postResources(ResourceParameters $parameters): Resource;
315
316
    /**
317
     * @param ResourceParameters $parameters
318
     * @return Resource
319
     * @throws EndpointExecutionException
320
     * @throws UnsupportedMethodException
321
     * @throws ElementNotFoundException
322
     * @throws ElementConflictException
323
     * @throws InvalidRequestException
324
     */
325
    abstract protected function putResource(ResourceParameters $parameters): Resource;
326
327
    /**
328
     * @param ResourceParameters[] $parameters
329
     * @return Resource[]
330
     * @throws EndpointExecutionException
331
     * @throws UnsupportedMethodException
332
     * @throws ElementConflictException
333
     * @throws InvalidRequestException
334
     */
335
    abstract protected function putResources(array $parameters): array;
336
337
    /**
338
     * @param ResourceParameters $parameters
339
     * @return Resource
340
     * @throws EndpointExecutionException
341
     * @throws UnsupportedMethodException
342
     * @throws ElementNotFoundException
343
     * @throws ElementConflictException
344
     * @throws InvalidRequestException
345
     */
346
    abstract protected function patchResource(ResourceParameters $parameters): Resource;
347
348
    /**
349
     * @param ResourceParameters[] $parameters
350
     * @return Resource[]
351
     * @throws EndpointExecutionException
352
     * @throws UnsupportedMethodException
353
     * @throws ElementNotFoundException
354
     * @throws ElementConflictException
355
     * @throws InvalidRequestException
356
     */
357
    abstract protected function patchResources(array $parameters): array;
358
359
    /**
360
     * @param ResourceParameters $parameters
361
     * @return Resource
362
     * @throws EndpointExecutionException
363
     * @throws UnsupportedMethodException
364
     * @throws InvalidRequestException
365
     */
366
    abstract protected function optionsResource(ResourceParameters $parameters): Resource;
367
368
    /**
369
     * @param ResourceParameters[] $parameters
370
     * @return Resource[]
371
     * @throws EndpointExecutionException
372
     * @throws UnsupportedMethodException
373
     * @throws InvalidRequestException
374
     */
375
    abstract protected function optionsResources(array $parameters): array;
376
377
    /**
378
     * @param ResourceParameters $parameters
379
     * @return Resource|null
380
     * @throws EndpointExecutionException
381
     * @throws UnsupportedMethodException
382
     * @throws ElementNotFoundException
383
     * @throws InvalidRequestException
384
     */
385
    abstract protected function deleteResource(ResourceParameters $parameters): ?Resource;
386
387
    /**
388
     * @param ResourceParameters $parameters
389
     * @return Resource[]|null
390
     * @throws EndpointExecutionException
391
     * @throws UnsupportedMethodException
392
     * @throws ElementNotFoundException
393
     * @throws InvalidRequestException
394
     */
395
    abstract protected function deleteResources(ResourceParameters $parameters): ?array;
396
397
}
398