Completed
Branch master (152d43)
by
unknown
06:03
created

ErrorCollection::addRelationshipIdError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 8
crap 1

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 namespace Neomerx\JsonApi\Exceptions;
2
3
/**
4
 * Copyright 2015-2017 [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 \Countable;
20
use \ArrayAccess;
21
use \Serializable;
22
use \ArrayIterator;
23
use \IteratorAggregate;
24
use \Neomerx\JsonApi\Document\Error;
25
use \Neomerx\JsonApi\Contracts\Document\LinkInterface;
26
use \Neomerx\JsonApi\Contracts\Document\ErrorInterface;
27
use \Neomerx\JsonApi\Contracts\Document\DocumentInterface;
28
29
/**
30
 * @package Neomerx\JsonApi
31
 *
32
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
33
 */
34
class ErrorCollection implements IteratorAggregate, ArrayAccess, Serializable, Countable
35
{
36
    /**
37
     * @var array
38
     */
39
    private $items = [];
40
41
    /**
42
     * @inheritdoc
43
     */
44 10
    public function getIterator()
45
    {
46 10
        return new ArrayIterator($this->items);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 27
    public function count()
53
    {
54 27
        return count($this->items);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 1
    public function serialize()
61
    {
62 1
        return serialize($this->items);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function unserialize($serialized)
69
    {
70 1
        $this->items = unserialize($serialized);
71 1
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 1
    public function offsetExists($offset)
77
    {
78 1
        return array_key_exists($offset, $this->items);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     *
84
     * @return ErrorInterface
85
     */
86 16
    public function offsetGet($offset)
87
    {
88 16
        return $this->items[$offset];
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 11
    public function offsetSet($offset, $value)
95
    {
96 11
        $offset === null ? $this->add($value) : $this->items[$offset] = $value;
97 11
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 1
    public function offsetUnset($offset)
103
    {
104 1
        unset($this->items[$offset]);
105 1
    }
106
107
    /**
108
     * @return ErrorInterface[]
109
     */
110 4
    public function getArrayCopy()
111
    {
112 4
        return $this->items;
113
    }
114
115
    /**
116
     * @param ErrorInterface $error
117
     *
118
     * @return $this
119
     */
120 23
    public function add(ErrorInterface $error)
121
    {
122 23
        $this->items[] =$error;
123
124 23
        return $this;
125
    }
126
127
    /**
128
     * @param string             $title
129
     * @param string|null        $detail
130
     * @param int|string|null    $status
131
     * @param int|string|null    $idx
132
     * @param LinkInterface|null $aboutLink
133
     * @param int|string|null    $code
134
     * @param mixed|null         $meta
135
     *
136
     * @return $this
137
     */
138 2 View Code Duplication
    public function addDataError(
139
        $title,
140
        $detail = null,
141
        $status = null,
142
        $idx = null,
143
        LinkInterface $aboutLink = null,
144
        $code = null,
145
        $meta = null
146
    ) {
147 2
        $pointer = $this->getPathToData();
148
149 2
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
150
    }
151
152
    /**
153
     * @param string             $title
154
     * @param string|null        $detail
155
     * @param int|string|null    $status
156
     * @param int|string|null    $idx
157
     * @param LinkInterface|null $aboutLink
158
     * @param int|string|null    $code
159
     * @param mixed|null         $meta
160
     *
161
     * @return $this
162
     */
163 1 View Code Duplication
    public function addDataTypeError(
164
        $title,
165
        $detail = null,
166
        $status = null,
167
        $idx = null,
168
        LinkInterface $aboutLink = null,
169
        $code = null,
170
        $meta = null
171
    ) {
172 1
        $pointer = $this->getPathToType();
173
174 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
175
    }
176
177
    /**
178
     * @param string             $title
179
     * @param string|null        $detail
180
     * @param int|string|null    $status
181
     * @param int|string|null    $idx
182
     * @param LinkInterface|null $aboutLink
183
     * @param int|string|null    $code
184
     * @param mixed|null         $meta
185
     *
186
     * @return $this
187
     */
188 1 View Code Duplication
    public function addDataIdError(
189
        $title,
190
        $detail = null,
191
        $status = null,
192
        $idx = null,
193
        LinkInterface $aboutLink = null,
194
        $code = null,
195
        $meta = null
196
    ) {
197 1
        $pointer = $this->getPathToId();
198
199 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
200
    }
201
202
    /**
203
     * @param string             $title
204
     * @param string|null        $detail
205
     * @param int|string|null    $status
206
     * @param int|string|null    $idx
207
     * @param LinkInterface|null $aboutLink
208
     * @param int|string|null    $code
209
     * @param mixed|null         $meta
210
     *
211
     * @return $this
212
     */
213 1 View Code Duplication
    public function addAttributesError(
214
        $title,
215
        $detail = null,
216
        $status = null,
217
        $idx = null,
218
        LinkInterface $aboutLink = null,
219
        $code = null,
220
        $meta = null
221
    ) {
222 1
        $pointer = $this->getPathToAttributes();
223
224 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
225
    }
226
227
    /**
228
     * @param string             $name
229
     * @param string             $title
230
     * @param string|null        $detail
231
     * @param int|string|null    $status
232
     * @param int|string|null    $idx
233
     * @param LinkInterface|null $aboutLink
234
     * @param int|string|null    $code
235
     * @param mixed|null         $meta
236
     *
237
     * @return $this
238
     */
239 1 View Code Duplication
    public function addDataAttributeError(
240
        $name,
241
        $title,
242
        $detail = null,
243
        $status = null,
244
        $idx = null,
245
        LinkInterface $aboutLink = null,
246
        $code = null,
247
        $meta = null
248
    ) {
249 1
        $pointer = $this->getPathToAttribute($name);
250
251 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
252
    }
253
254
    /**
255
     * @param string             $title
256
     * @param string|null        $detail
257
     * @param int|string|null    $status
258
     * @param int|string|null    $idx
259
     * @param LinkInterface|null $aboutLink
260
     * @param int|string|null    $code
261
     * @param mixed|null         $meta
262
     *
263
     * @return $this
264
     */
265 1 View Code Duplication
    public function addRelationshipsError(
266
        $title,
267
        $detail = null,
268
        $status = null,
269
        $idx = null,
270
        LinkInterface $aboutLink = null,
271
        $code = null,
272
        $meta = null
273
    ) {
274 1
        $pointer = $this->getPathToRelationships();
275
276 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
277
    }
278
279
    /**
280
     * @param string             $name
281
     * @param string             $title
282
     * @param string|null        $detail
283
     * @param int|string|null    $status
284
     * @param int|string|null    $idx
285
     * @param LinkInterface|null $aboutLink
286
     * @param int|string|null    $code
287
     * @param mixed|null         $meta
288
     *
289
     * @return $this
290
     */
291 1 View Code Duplication
    public function addRelationshipError(
292
        $name,
293
        $title,
294
        $detail = null,
295
        $status = null,
296
        $idx = null,
297
        LinkInterface $aboutLink = null,
298
        $code = null,
299
        $meta = null
300
    ) {
301 1
        $pointer = $this->getPathToRelationship($name);
302
303 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
304
    }
305
306
    /**
307
     * @param string             $name
308
     * @param string             $title
309
     * @param string|null        $detail
310
     * @param int|string|null    $status
311
     * @param int|string|null    $idx
312
     * @param LinkInterface|null $aboutLink
313
     * @param int|string|null    $code
314
     * @param mixed|null         $meta
315
     *
316
     * @return $this
317
     */
318 1 View Code Duplication
    public function addRelationshipTypeError(
319
        $name,
320
        $title,
321
        $detail = null,
322
        $status = null,
323
        $idx = null,
324
        LinkInterface $aboutLink = null,
325
        $code = null,
326
        $meta = null
327
    ) {
328 1
        $pointer = $this->getPathToRelationshipType($name);
329
330 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
331
    }
332
333
    /**
334
     * @param string             $name
335
     * @param string             $title
336
     * @param string|null        $detail
337
     * @param int|string|null    $status
338
     * @param int|string|null    $idx
339
     * @param LinkInterface|null $aboutLink
340
     * @param int|string|null    $code
341
     * @param mixed|null         $meta
342
     *
343
     * @return $this
344
     */
345 1 View Code Duplication
    public function addRelationshipIdError(
346
        $name,
347
        $title,
348
        $detail = null,
349
        $status = null,
350
        $idx = null,
351
        LinkInterface $aboutLink = null,
352
        $code = null,
353
        $meta = null
354
    ) {
355 1
        $pointer = $this->getPathToRelationshipId($name);
356
357 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
358
    }
359
360
    /**
361
     * @param string             $name
362
     * @param string             $title
363
     * @param string|null        $detail
364
     * @param int|string|null    $status
365
     * @param int|string|null    $idx
366
     * @param LinkInterface|null $aboutLink
367
     * @param int|string|null    $code
368
     * @param mixed|null         $meta
369
     *
370
     * @return $this
371
     */
372 8 View Code Duplication
    public function addQueryParameterError(
373
        $name,
374
        $title,
375
        $detail = null,
376
        $status = null,
377
        $idx = null,
378
        LinkInterface $aboutLink = null,
379
        $code = null,
380
        $meta = null
381
    ) {
382 8
        $source = [Error::SOURCE_PARAMETER => $name];
383 8
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
384
385 8
        $this->add($error);
386
387 8
        return $this;
388
    }
389
390
    /** @noinspection PhpTooManyParametersInspection
391
     * @param string             $title
392
     * @param string             $pointer
393
     * @param string|null        $detail
394
     * @param int|string|null    $status
395
     * @param int|string|null    $idx
396
     * @param LinkInterface|null $aboutLink
397
     * @param int|string|null    $code
398
     * @param mixed|null         $meta
399
     *
400
     * @return $this
401
     */
402 10 View Code Duplication
    protected function addResourceError(
403
        $title,
404
        $pointer,
405
        $detail = null,
406
        $status = null,
407
        $idx = null,
408
        LinkInterface $aboutLink = null,
409
        $code = null,
410
        $meta = null
411
    ) {
412 10
        $source = [Error::SOURCE_POINTER => $pointer];
413 10
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
414
415 10
        $this->add($error);
416
417 10
        return $this;
418
    }
419
420
    /**
421
     * @return string
422
     */
423 10
    protected function getPathToData()
424
    {
425 10
        return '/' . DocumentInterface::KEYWORD_DATA;
426
    }
427
428
    /**
429
     * @return string
430
     */
431 1
    protected function getPathToType()
432
    {
433 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_TYPE;
434
    }
435
436
    /**
437
     * @return string
438
     */
439 1
    protected function getPathToId()
440
    {
441 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ID;
442
    }
443
444
    /**
445
     * @return string
446
     */
447 1
    protected function getPathToAttributes()
448
    {
449 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES;
450
    }
451
452
    /**
453
     * @param string $name
454
     *
455
     * @return string
456
     */
457 1
    protected function getPathToAttribute($name)
458
    {
459 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES . '/' . $name;
460
    }
461
462
    /**
463
     * @return string
464
     */
465 4
    protected function getPathToRelationships()
466
    {
467 4
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_RELATIONSHIPS;
468
    }
469
470
    /**
471
     * @param string $name
472
     *
473
     * @return string
474
     */
475 3
    protected function getPathToRelationship($name)
476
    {
477 3
        return $this->getPathToRelationships() . '/' . $name;
478
    }
479
480
    /**
481
     * @param string $name
482
     *
483
     * @return string
484
     */
485 1
    protected function getPathToRelationshipType($name)
486
    {
487 1
        return $this->getPathToRelationship($name) . '/' .
488 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_TYPE;
489
    }
490
491
    /**
492
     * @param string $name
493
     *
494
     * @return string
495
     */
496 1
    protected function getPathToRelationshipId($name)
497
    {
498 1
        return $this->getPathToRelationship($name) . '/' .
499 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_ID;
500
    }
501
}
502