Completed
Branch next (8f844e)
by Neomerx
04:06
created

ErrorCollection::addQueryParameterError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 10
dl 0
loc 19
ccs 5
cts 5
cp 1
crap 1
rs 9.6333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Schema;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use ArrayAccess;
22
use ArrayIterator;
23
use Countable;
24
use IteratorAggregate;
25
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface;
26
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface;
27
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
28
use Serializable;
29
30
/**
31
 * @package Neomerx\JsonApi
32
 *
33
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
34
 */
35
class ErrorCollection implements IteratorAggregate, ArrayAccess, Serializable, Countable
36
{
37
    /**
38
     * @var array
39
     */
40
    private $items = [];
41
42
    /**
43
     * @inheritdoc
44
     */
45 2
    public function getIterator()
46
    {
47 2
        return new ArrayIterator($this->items);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 14
    public function count()
54
    {
55 14
        return count($this->items);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 1
    public function serialize()
62
    {
63 1
        return serialize($this->items);
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 1
    public function unserialize($serialized)
70
    {
71 1
        $this->items = unserialize($serialized);
72 1
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 1
    public function offsetExists($offset)
78
    {
79 1
        return array_key_exists($offset, $this->items);
80
    }
81
82
    /**
83
     * @inheritdoc
84
     *
85
     * @return ErrorInterface
86
     */
87 11
    public function offsetGet($offset)
88
    {
89 11
        return $this->items[$offset];
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 9
    public function offsetSet($offset, $value)
96
    {
97 9
        $offset === null ? $this->add($value) : $this->items[$offset] = $value;
98 9
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 1
    public function offsetUnset($offset)
104
    {
105 1
        unset($this->items[$offset]);
106 1
    }
107
108
    /**
109
     * @return ErrorInterface[]
110
     */
111 4
    public function getArrayCopy(): array
112
    {
113 4
        return $this->items;
114
    }
115
116
    /**
117
     * @param ErrorInterface $error
118
     *
119
     * @return self
120
     */
121 22
    public function add(ErrorInterface $error): self
122
    {
123 22
        $this->items[] = $error;
124
125 22
        return $this;
126
    }
127
128
    /**
129
     * @param string             $title
130
     * @param string|null        $detail
131
     * @param string|null        $status
132
     * @param int|string|null    $idx
133
     * @param LinkInterface|null $aboutLink
134
     * @param iterable|null      $typeLinks
135
     * @param string|null        $code
136
     * @param bool               $hasMeta
137
     * @param mixed              $meta
138
     *
139
     * @return self
140
     */
141 2
    public function addDataError(
142
        $title,
143
        $detail = null,
144
        $status = null,
145
        $idx = null,
146
        LinkInterface $aboutLink = null,
147
        ?iterable $typeLinks = null,
148
        $code = null,
149
        $hasMeta = false,
150
        $meta = null
151
    ): self {
152 2
        $pointer = $this->getPathToData();
153
154 2
        return $this->addResourceError(
155 2
            $title,
156 2
            $pointer,
157 2
            $detail,
158 2
            $status,
159 2
            $idx,
160 2
            $aboutLink,
161 2
            $typeLinks,
162 2
            $code,
163 2
            $hasMeta,
164 2
            $meta
165
        );
166
    }
167
168
    /**
169
     * @param string             $title
170
     * @param string|null        $detail
171
     * @param string|null        $status
172
     * @param int|string|null    $idx
173
     * @param LinkInterface|null $aboutLink
174
     * @param iterable|null      $typeLinks
175
     * @param string|null        $code
176
     * @param bool               $hasMeta
177
     * @param mixed              $meta
178
     *
179
     * @return self
180
     */
181 1
    public function addDataTypeError(
182
        $title,
183
        $detail = null,
184
        $status = null,
185
        $idx = null,
186
        LinkInterface $aboutLink = null,
187
        ?iterable $typeLinks = null,
188
        $code = null,
189
        $hasMeta = false,
190
        $meta = null
191
    ): self {
192 1
        $pointer = $this->getPathToType();
193
194 1
        return $this->addResourceError(
195 1
            $title,
196 1
            $pointer,
197 1
            $detail,
198 1
            $status,
199 1
            $idx,
200 1
            $aboutLink,
201 1
            $typeLinks,
202 1
            $code,
203 1
            $hasMeta,
204 1
            $meta
205
        );
206
    }
207
208
    /**
209
     * @param string             $title
210
     * @param string|null        $detail
211
     * @param string|null        $status
212
     * @param int|string|null    $idx
213
     * @param LinkInterface|null $aboutLink
214
     * @param iterable|null      $typeLinks
215
     * @param string|null        $code
216
     * @param bool               $hasMeta
217
     * @param mixed              $meta
218
     *
219
     * @return self
220
     */
221 1
    public function addDataIdError(
222
        $title,
223
        $detail = null,
224
        $status = null,
225
        $idx = null,
226
        LinkInterface $aboutLink = null,
227
        ?iterable $typeLinks = null,
228
        $code = null,
229
        $hasMeta = false,
230
        $meta = null
231
    ): self {
232 1
        $pointer = $this->getPathToId();
233
234 1
        return $this->addResourceError(
235 1
            $title,
236 1
            $pointer,
237 1
            $detail,
238 1
            $status,
239 1
            $idx,
240 1
            $aboutLink,
241 1
            $typeLinks,
242 1
            $code,
243 1
            $hasMeta,
244 1
            $meta
245
        );
246
    }
247
248
    /**
249
     * @param string             $title
250
     * @param string|null        $detail
251
     * @param string|null        $status
252
     * @param int|string|null    $idx
253
     * @param LinkInterface|null $aboutLink
254
     * @param iterable|null      $typeLinks
255
     * @param string|null        $code
256
     * @param bool               $hasMeta
257
     * @param mixed              $meta
258
     *
259
     * @return self
260
     */
261 1
    public function addAttributesError(
262
        $title,
263
        $detail = null,
264
        $status = null,
265
        $idx = null,
266
        LinkInterface $aboutLink = null,
267
        ?iterable $typeLinks = null,
268
        $code = null,
269
        $hasMeta = false,
270
        $meta = null
271
    ): self {
272 1
        $pointer = $this->getPathToAttributes();
273
274 1
        return $this->addResourceError(
275 1
            $title,
276 1
            $pointer,
277 1
            $detail,
278 1
            $status,
279 1
            $idx,
280 1
            $aboutLink,
281 1
            $typeLinks,
282 1
            $code,
283 1
            $hasMeta,
284 1
            $meta
285
        );
286
    }
287
288
    /**
289
     * @param string             $name
290
     * @param string             $title
291
     * @param string|null        $detail
292
     * @param string|null        $status
293
     * @param int|string|null    $idx
294
     * @param LinkInterface|null $aboutLink
295
     * @param iterable|null      $typeLinks
296
     * @param string|null        $code
297
     * @param bool               $hasMeta
298
     * @param mixed              $meta
299
     *
300
     * @return self
301
     */
302 1
    public function addDataAttributeError(
303
        $name,
304
        $title,
305
        $detail = null,
306
        $status = null,
307
        $idx = null,
308
        LinkInterface $aboutLink = null,
309
        ?iterable $typeLinks = null,
310
        $code = null,
311
        $hasMeta = false,
312
        $meta = null
313
    ): self {
314 1
        $pointer = $this->getPathToAttribute($name);
315
316 1
        return $this->addResourceError(
317 1
            $title,
318 1
            $pointer,
319 1
            $detail,
320 1
            $status,
321 1
            $idx,
322 1
            $aboutLink,
323 1
            $typeLinks,
324 1
            $code,
325 1
            $hasMeta,
326 1
            $meta
327
        );
328
    }
329
330
    /**
331
     * @param string             $title
332
     * @param string|null        $detail
333
     * @param string|null        $status
334
     * @param int|string|null    $idx
335
     * @param LinkInterface|null $aboutLink
336
     * @param iterable|null      $typeLinks
337
     * @param string|null        $code
338
     * @param bool               $hasMeta
339
     * @param mixed              $meta
340
     *
341
     * @return self
342
     */
343 1
    public function addRelationshipsError(
344
        $title,
345
        $detail = null,
346
        $status = null,
347
        $idx = null,
348
        LinkInterface $aboutLink = null,
349
        ?iterable $typeLinks = null,
350
        $code = null,
351
        $hasMeta = false,
352
        $meta = null
353
    ): self {
354 1
        $pointer = $this->getPathToRelationships();
355
356 1
        return $this->addResourceError(
357 1
            $title,
358 1
            $pointer,
359 1
            $detail,
360 1
            $status,
361 1
            $idx,
362 1
            $aboutLink,
363 1
            $typeLinks,
364 1
            $code,
365 1
            $hasMeta,
366 1
            $meta
367
        );
368
    }
369
370
    /**
371
     * @param string             $name
372
     * @param string             $title
373
     * @param string|null        $detail
374
     * @param string|null        $status
375
     * @param int|string|null    $idx
376
     * @param LinkInterface|null $aboutLink
377
     * @param iterable|null      $typeLinks
378
     * @param string|null        $code
379
     * @param bool               $hasMeta
380
     * @param mixed              $meta
381
     *
382
     * @return self
383
     */
384 1
    public function addRelationshipError(
385
        $name,
386
        $title,
387
        $detail = null,
388
        $status = null,
389
        $idx = null,
390
        LinkInterface $aboutLink = null,
391
        ?iterable $typeLinks = null,
392
        $code = null,
393
        $hasMeta = false,
394
        $meta = null
395
    ): self {
396 1
        $pointer = $this->getPathToRelationship($name);
397
398 1
        return $this->addResourceError(
399 1
            $title,
400 1
            $pointer,
401 1
            $detail,
402 1
            $status,
403 1
            $idx,
404 1
            $aboutLink,
405 1
            $typeLinks,
406 1
            $code,
407 1
            $hasMeta,
408 1
            $meta
409
        );
410
    }
411
412
    /**
413
     * @param string             $name
414
     * @param string             $title
415
     * @param string|null        $detail
416
     * @param string|null        $status
417
     * @param int|string|null    $idx
418
     * @param LinkInterface|null $aboutLink
419
     * @param iterable|null      $typeLinks
420
     * @param string|null        $code
421
     * @param bool               $hasMeta
422
     * @param mixed              $meta
423
     *
424
     * @return self
425
     */
426 1
    public function addRelationshipTypeError(
427
        $name,
428
        $title,
429
        $detail = null,
430
        $status = null,
431
        $idx = null,
432
        LinkInterface $aboutLink = null,
433
        ?iterable $typeLinks = null,
434
        $code = null,
435
        $hasMeta = false,
436
        $meta = null
437
    ): self {
438 1
        $pointer = $this->getPathToRelationshipType($name);
439
440 1
        return $this->addResourceError(
441 1
            $title,
442 1
            $pointer,
443 1
            $detail,
444 1
            $status,
445 1
            $idx,
446 1
            $aboutLink,
447 1
            $typeLinks,
448 1
            $code,
449 1
            $hasMeta,
450 1
            $meta
451
        );
452
    }
453
454
    /**
455
     * @param string             $name
456
     * @param string             $title
457
     * @param string|null        $detail
458
     * @param string|null        $status
459
     * @param int|string|null    $idx
460
     * @param LinkInterface|null $aboutLink
461
     * @param iterable|null      $typeLinks
462
     * @param string|null        $code
463
     * @param bool               $hasMeta
464
     * @param mixed              $meta
465
     *
466
     * @return self
467
     */
468 1
    public function addRelationshipIdError(
469
        $name,
470
        $title,
471
        $detail = null,
472
        $status = null,
473
        $idx = null,
474
        LinkInterface $aboutLink = null,
475
        ?iterable $typeLinks = null,
476
        $code = null,
477
        $hasMeta = false,
478
        $meta = null
479
    ): self {
480 1
        $pointer = $this->getPathToRelationshipId($name);
481
482 1
        return $this->addResourceError(
483 1
            $title,
484 1
            $pointer,
485 1
            $detail,
486 1
            $status,
487 1
            $idx,
488 1
            $aboutLink,
489 1
            $typeLinks,
490 1
            $code,
491 1
            $hasMeta,
492 1
            $meta
493
        );
494
    }
495
496
    /**
497
     * @param string             $name
498
     * @param string             $title
499
     * @param string|null        $detail
500
     * @param string|null        $status
501
     * @param int|string|null    $idx
502
     * @param LinkInterface|null $aboutLink
503
     * @param iterable|null      $typeLinks
504
     * @param string|null        $code
505
     * @param bool               $hasMeta
506
     * @param mixed              $meta
507
     *
508
     * @return self
509
     */
510 1
    public function addQueryParameterError(
511
        string $name,
512
        string $title,
513
        string $detail = null,
514
        string $status = null,
515
        $idx = null,
516
        LinkInterface $aboutLink = null,
517
        ?iterable $typeLinks = null,
518
        $code = null,
519
        $hasMeta = false,
520
        $meta = null
521
    ): self {
522 1
        $source = [ErrorInterface::SOURCE_PARAMETER => $name];
523 1
        $error  = new Error($idx, $aboutLink, $typeLinks, $status, $code, $title, $detail, $source, $hasMeta, $meta);
524
525 1
        $this->add($error);
526
527 1
        return $this;
528
    }
529
530
    /** @noinspection PhpTooManyParametersInspection
531
     * @param string             $title
532
     * @param string             $pointer
533
     * @param string|null        $detail
534
     * @param string|null        $status
535
     * @param null               $idx
536
     * @param LinkInterface|null $aboutLink
537
     * @param iterable|null      $typeLinks
538
     * @param string|null        $code
539
     * @param bool               $hasMeta
540
     * @param mixed              $meta
541
     *
542
     * @return self
543
     */
544 10
    protected function addResourceError(
545
        string $title,
546
        string $pointer,
547
        string $detail = null,
548
        string $status = null,
549
        $idx = null,
550
        LinkInterface $aboutLink = null,
551
        ?iterable $typeLinks = null,
552
        string $code = null,
553
        $hasMeta = false,
554
        $meta = null
555
    ): self {
556 10
        $source = [ErrorInterface::SOURCE_POINTER => $pointer];
557 10
        $error  = new Error($idx, $aboutLink, $typeLinks, $status, $code, $title, $detail, $source, $hasMeta, $meta);
558
559 10
        $this->add($error);
560
561 10
        return $this;
562
    }
563
564
    /**
565
     * @return string
566
     */
567 10
    protected function getPathToData(): string
568
    {
569 10
        return '/' . DocumentInterface::KEYWORD_DATA;
570
    }
571
572
    /**
573
     * @return string
574
     */
575 1
    protected function getPathToType(): string
576
    {
577 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_TYPE;
578
    }
579
580
    /**
581
     * @return string
582
     */
583 1
    protected function getPathToId(): string
584
    {
585 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ID;
586
    }
587
588
    /**
589
     * @return string
590
     */
591 1
    protected function getPathToAttributes(): string
592
    {
593 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES;
594
    }
595
596
    /**
597
     * @param string $name
598
     *
599
     * @return string
600
     */
601 1
    protected function getPathToAttribute(string $name): string
602
    {
603 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES . '/' . $name;
604
    }
605
606
    /**
607
     * @return string
608
     */
609 4
    protected function getPathToRelationships(): string
610
    {
611 4
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_RELATIONSHIPS;
612
    }
613
614
    /**
615
     * @param string $name
616
     *
617
     * @return string
618
     */
619 3
    protected function getPathToRelationship(string $name): string
620
    {
621 3
        return $this->getPathToRelationships() . '/' . $name;
622
    }
623
624
    /**
625
     * @param string $name
626
     *
627
     * @return string
628
     */
629 1
    protected function getPathToRelationshipType(string $name): string
630
    {
631 1
        return $this->getPathToRelationship($name) . '/' .
632 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_TYPE;
633
    }
634
635
    /**
636
     * @param string $name
637
     *
638
     * @return string
639
     */
640 1
    protected function getPathToRelationshipId(string $name): string
641
    {
642 1
        return $this->getPathToRelationship($name) . '/' .
643 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_ID;
644
    }
645
}
646