Completed
Push — master ( 52b2ea...d5cfd4 )
by Jasper
12s queued 10s
created

ErrorParser::parse()   D

Complexity

Conditions 20
Paths 7

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 21
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 30
ccs 22
cts 22
cp 1
crap 20
rs 4.1666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Swis\JsonApi\Client\Parsers;
4
5
use Swis\JsonApi\Client\Error;
6
use Swis\JsonApi\Client\ErrorSource;
7
use Swis\JsonApi\Client\Exceptions\ValidationException;
8
9
/**
10
 * @internal
11
 */
12
class ErrorParser
13
{
14
    /**
15
     * @var \Swis\JsonApi\Client\Parsers\LinksParser
16
     */
17
    private $linksParser;
18
19
    /**
20
     * @var \Swis\JsonApi\Client\Parsers\MetaParser
21
     */
22
    private $metaParser;
23
24
    /**
25
     * @param \Swis\JsonApi\Client\Parsers\LinksParser $linksParser
26
     * @param \Swis\JsonApi\Client\Parsers\MetaParser  $metaParser
27
     */
28 486
    public function __construct(LinksParser $linksParser, MetaParser $metaParser)
29
    {
30 486
        $this->linksParser = $linksParser;
31 486
        $this->metaParser = $metaParser;
32 486
    }
33
34
    /**
35
     * @param mixed $data
36
     *
37
     * @return \Swis\JsonApi\Client\Error
38
     */
39 336
    public function parse($data): Error
40
    {
41 336
        if (!is_object($data)) {
42 36
            throw new ValidationException(sprintf('Error has to be an object, "%s" given.', gettype($data)));
43
        }
44 300
        if (property_exists($data, 'id') && !is_string($data->id)) {
45 36
            throw new ValidationException(sprintf('Error property "id" has to be a string, "%s" given.', gettype($data->id)));
46
        }
47 264
        if (property_exists($data, 'status') && !is_string($data->status)) {
48 36
            throw new ValidationException(sprintf('Error property "status" has to be a string, "%s" given.', gettype($data->status)));
49
        }
50 228
        if (property_exists($data, 'code') && !is_string($data->code)) {
51 36
            throw new ValidationException(sprintf('Error property "code" has to be a string, "%s" given.', gettype($data->code)));
52
        }
53 192
        if (property_exists($data, 'title') && !is_string($data->title)) {
54 36
            throw new ValidationException(sprintf('Error property "title" has to be a string, "%s" given.', gettype($data->title)));
55
        }
56 156
        if (property_exists($data, 'detail') && !is_string($data->detail)) {
57 36
            throw new ValidationException(sprintf('Error property "detail" has to be a string, "%s" given.', gettype($data->detail)));
58
        }
59
60 120
        return new Error(
61 120
            property_exists($data, 'id') ? $data->id : null,
62 120
            property_exists($data, 'links') ? $this->linksParser->parse($data->links) : null,
63 120
            property_exists($data, 'status') ? $data->status : null,
64 120
            property_exists($data, 'code') ? $data->code : null,
65 120
            property_exists($data, 'title') ? $data->title : null,
66 120
            property_exists($data, 'detail') ? $data->detail : null,
67 120
            property_exists($data, 'source') ? $this->buildErrorSource($data->source) : null,
68 12
            property_exists($data, 'meta') ? $this->metaParser->parse($data->meta) : null
69
        );
70
    }
71
72
    /**
73
     * @param mixed $data
74
     *
75
     * @return \Swis\JsonApi\Client\ErrorSource
76
     */
77 114
    private function buildErrorSource($data): ErrorSource
78
    {
79 114
        if (!is_object($data)) {
80 36
            throw new ValidationException(sprintf('ErrorSource has to be an object, "%s" given.', gettype($data)));
81
        }
82 78
        if (property_exists($data, 'pointer') && !is_string($data->pointer)) {
83 36
            throw new ValidationException(sprintf('ErrorSource property "pointer" has to be a string, "%s" given.', gettype($data->pointer)));
84
        }
85 42
        if (property_exists($data, 'parameter') && !is_string($data->parameter)) {
86 36
            throw new ValidationException(sprintf('ErrorSource property "parameter" has to be a string, "%s" given.', gettype($data->parameter)));
87
        }
88
89 6
        return new ErrorSource(
90 6
            property_exists($data, 'pointer') ? $data->pointer : null,
91 6
            property_exists($data, 'parameter') ? $data->parameter : null
92
        );
93
    }
94
}
95