Completed
Branch master (85eed3)
by
unknown
02:32
created

ErrorCollection::addDataError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 7
crap 1
1
<?php namespace Neomerx\JsonApi\Exceptions;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
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 \ArrayObject;
22
use \Serializable;
23
use \IteratorAggregate;
24
use \Neomerx\JsonApi\Document\Error;
25
use \Neomerx\JsonApi\Contracts\Schema\LinkInterface;
26
use \Neomerx\JsonApi\Contracts\Document\DocumentInterface;
27
28
/**
29
 * @package Neomerx\JsonApi
30
 */
31
class ErrorCollection implements IteratorAggregate, ArrayAccess, Serializable, Countable
32
{
33
    /**
34
     * @var ArrayObject
35
     */
36
    private $items;
37
38
    /**
39
     * ErrorCollection constructor.
40
     */
41 30
    public function __construct()
42
    {
43 30
        $this->items = new ArrayObject();
44 30
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 3
    public function getIterator()
50
    {
51 3
        return $this->items->getIterator();
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 12
    public function count()
58
    {
59 12
        return $this->items->count();
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    public function serialize()
66
    {
67 1
        return $this->items->serialize();
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 1
    public function unserialize($serialized)
74
    {
75 1
        $this->items->unserialize($serialized);
76 1
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 1
    public function offsetExists($offset)
82
    {
83 1
        return $this->items->offsetExists($offset);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     *
89
     * @return Error
90
     */
91 9
    public function offsetGet($offset)
92
    {
93 9
        return $this->items->offsetGet($offset);
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 4
    public function offsetSet($offset, $value)
100
    {
101 4
        $this->items->offsetSet($offset, $value);
102 4
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 1
    public function offsetUnset($offset)
108
    {
109 1
        $this->items->offsetUnset($offset);
110 1
    }
111
112
    /**
113
     * @return Error[]
114
     */
115 4
    public function getArrayCopy()
116
    {
117 4
        return $this->items->getArrayCopy();
118
    }
119
120
    /**
121
     * @param Error $error
122
     *
123
     * @return $this
124
     */
125 12
    public function add(Error $error)
126
    {
127 12
        $this->items->append($error);
128
129 12
        return $this;
130
    }
131
132
    /**
133
     * @param string             $title
134
     * @param string|null        $detail
135
     * @param int|string|null    $status
136
     * @param int|string|null    $idx
137
     * @param LinkInterface|null $aboutLink
138
     * @param int|string|null    $code
139
     * @param mixed|null         $meta
140
     *
141
     * @return $this
142
     */
143 2 View Code Duplication
    public function addDataError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
144
        $title,
145
        $detail = null,
146
        $status = null,
147
        $idx = null,
148
        LinkInterface $aboutLink = null,
149
        $code = null,
150
        $meta = null
151
    ) {
152 2
        $pointer = $this->getPathToData();
153
154 2
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
155
    }
156
157
    /**
158
     * @param string             $title
159
     * @param string|null        $detail
160
     * @param int|string|null    $status
161
     * @param int|string|null    $idx
162
     * @param LinkInterface|null $aboutLink
163
     * @param int|string|null    $code
164
     * @param mixed|null         $meta
165
     *
166
     * @return $this
167
     */
168 1 View Code Duplication
    public function addDataTypeError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
169
        $title,
170
        $detail = null,
171
        $status = null,
172
        $idx = null,
173
        LinkInterface $aboutLink = null,
174
        $code = null,
175
        $meta = null
176
    ) {
177 1
        $pointer = $this->getPathToType();
178
179 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
180
    }
181
182
    /**
183
     * @param string             $title
184
     * @param string|null        $detail
185
     * @param int|string|null    $status
186
     * @param int|string|null    $idx
187
     * @param LinkInterface|null $aboutLink
188
     * @param int|string|null    $code
189
     * @param mixed|null         $meta
190
     *
191
     * @return $this
192
     */
193 1 View Code Duplication
    public function addDataIdError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
194
        $title,
195
        $detail = null,
196
        $status = null,
197
        $idx = null,
198
        LinkInterface $aboutLink = null,
199
        $code = null,
200
        $meta = null
201
    ) {
202 1
        $pointer = $this->getPathToId();
203
204 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
205
    }
206
207
    /**
208
     * @param string             $name
209
     * @param string             $title
210
     * @param string|null        $detail
211
     * @param int|string|null    $status
212
     * @param int|string|null    $idx
213
     * @param LinkInterface|null $aboutLink
214
     * @param int|string|null    $code
215
     * @param mixed|null         $meta
216
     *
217
     * @return $this
218
     */
219 1 View Code Duplication
    public function addDataAttributeError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
220
        $name,
221
        $title,
222
        $detail = null,
223
        $status = null,
224
        $idx = null,
225
        LinkInterface $aboutLink = null,
226
        $code = null,
227
        $meta = null
228
    ) {
229 1
        $pointer = $this->getPathToAttribute($name);
230
231 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
232
    }
233
234
    /**
235
     * @param string             $name
236
     * @param string             $title
237
     * @param string|null        $detail
238
     * @param int|string|null    $status
239
     * @param int|string|null    $idx
240
     * @param LinkInterface|null $aboutLink
241
     * @param int|string|null    $code
242
     * @param mixed|null         $meta
243
     *
244
     * @return $this
245
     */
246 1 View Code Duplication
    public function addRelationshipError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
247
        $name,
248
        $title,
249
        $detail = null,
250
        $status = null,
251
        $idx = null,
252
        LinkInterface $aboutLink = null,
253
        $code = null,
254
        $meta = null
255
    ) {
256 1
        $pointer = $this->getPathToRelationship($name);
257
258 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
259
    }
260
261
    /**
262
     * @param string             $name
263
     * @param string             $title
264
     * @param string|null        $detail
265
     * @param int|string|null    $status
266
     * @param int|string|null    $idx
267
     * @param LinkInterface|null $aboutLink
268
     * @param int|string|null    $code
269
     * @param mixed|null         $meta
270
     *
271
     * @return $this
272
     */
273 1 View Code Duplication
    public function addRelationshipTypeError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
274
        $name,
275
        $title,
276
        $detail = null,
277
        $status = null,
278
        $idx = null,
279
        LinkInterface $aboutLink = null,
280
        $code = null,
281
        $meta = null
282
    ) {
283 1
        $pointer = $this->getPathToRelationshipType($name);
284
285 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
286
    }
287
288
    /**
289
     * @param string             $name
290
     * @param string             $title
291
     * @param string|null        $detail
292
     * @param int|string|null    $status
293
     * @param int|string|null    $idx
294
     * @param LinkInterface|null $aboutLink
295
     * @param int|string|null    $code
296
     * @param mixed|null         $meta
297
     *
298
     * @return $this
299
     */
300 1 View Code Duplication
    public function addRelationshipIdError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
301
        $name,
302
        $title,
303
        $detail = null,
304
        $status = null,
305
        $idx = null,
306
        LinkInterface $aboutLink = null,
307
        $code = null,
308
        $meta = null
309
    ) {
310 1
        $pointer = $this->getPathToRelationshipId($name);
311
312 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
313
    }
314
315
    /**
316
     * @param string             $name
317
     * @param string             $title
318
     * @param string|null        $detail
319
     * @param int|string|null    $status
320
     * @param int|string|null    $idx
321
     * @param LinkInterface|null $aboutLink
322
     * @param int|string|null    $code
323
     * @param mixed|null         $meta
324
     *
325
     * @return $this
326
     */
327 1 View Code Duplication
    public function addQueryParameterError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
328
        $name,
329
        $title,
330
        $detail = null,
331
        $status = null,
332
        $idx = null,
333
        LinkInterface $aboutLink = null,
334
        $code = null,
335
        $meta = null
336
    ) {
337 1
        $source = [Error::SOURCE_PARAMETER => $name];
338 1
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
339
340 1
        $this->add($error);
341
342 1
        return $this;
343
    }
344
345
    /** @noinspection PhpTooManyParametersInspection
346
     * @param string             $title
347
     * @param string             $pointer
348
     * @param string|null        $detail
349
     * @param int|string|null    $status
350
     * @param int|string|null    $idx
351
     * @param LinkInterface|null $aboutLink
352
     * @param int|string|null    $code
353
     * @param mixed|null         $meta
354
     *
355
     * @return $this
356
     */
357 8 View Code Duplication
    protected function addResourceError(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
358
        $title,
359
        $pointer,
360
        $detail = null,
361
        $status = null,
362
        $idx = null,
363
        LinkInterface $aboutLink = null,
364
        $code = null,
365
        $meta = null
366
    ) {
367 8
        $source = [Error::SOURCE_POINTER => $pointer];
368 8
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
369
370 8
        $this->add($error);
371
372 8
        return $this;
373
    }
374
375
    /**
376
     * @return string
377
     */
378 8
    protected function getPathToData()
379
    {
380 8
        return '/' . DocumentInterface::KEYWORD_DATA;
381
    }
382
383
    /**
384
     * @return string
385
     */
386 1
    protected function getPathToType()
387
    {
388 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_TYPE;
389
    }
390
391
    /**
392
     * @return string
393
     */
394 1
    protected function getPathToId()
395
    {
396 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ID;
397
    }
398
399
    /**
400
     * @param string $name
401
     *
402
     * @return string
403
     */
404 1
    protected function getPathToAttribute($name)
405
    {
406 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES . '/' . $name;
407
    }
408
409
    /**
410
     * @param string $name
411
     *
412
     * @return string
413
     */
414 3
    protected function getPathToRelationship($name)
415
    {
416 3
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_RELATIONSHIPS . '/' . $name;
417
    }
418
419
    /**
420
     * @param string $name
421
     *
422
     * @return string
423
     */
424 1
    protected function getPathToRelationshipType($name)
425
    {
426 1
        return $this->getPathToRelationship($name) . '/' .
427 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_TYPE;
428
    }
429
430
    /**
431
     * @param string $name
432
     *
433
     * @return string
434
     */
435 1
    protected function getPathToRelationshipId($name)
436
    {
437 1
        return $this->getPathToRelationship($name) . '/' .
438 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_ID;
439
    }
440
}
441