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

ErrorParser   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 80
ccs 36
cts 36
cp 1
rs 10
wmc 29

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D parse() 0 30 20
B buildErrorSource() 0 15 8
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