Passed
Pull Request — master (#17)
by Pavel
04:41
created

SyncResponse::getError()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 0
crap 4
1
<?php
2
3
namespace ScayTrase\Api\JsonRpc;
4
5
use ScayTrase\Api\JsonRpc\Exception\ResponseParseException;
6
7
final class SyncResponse implements JsonRpcResponseInterface
8
{
9
    /** @var  \stdClass */
10
    private $response;
11
    /** @var  JsonRpcError */
12
    private $error;
13
14
    /**
15
     * SyncResponse constructor.
16
     * @param \stdClass $response
17
     * @throws ResponseParseException on creating response for notification
18
     */
19 21
    public function __construct(\stdClass $response)
20
    {
21 21
        $this->response = $response;
22
23
        /** @noinspection PhpInternalEntityUsedInspection */
24 21
        $validator = new ResponseBodyValidator();
25 21
        $validator->validate($this->response);
26 8
    }
27
28
29
    /** {@inheritdoc} */
30 5
    public function getError()
31
    {
32 5
        if ($this->isSuccessful()) {
33 1
            return null;
34
        }
35
36 4
        if (null !== $this->error) {
37 2
            return $this->error;
38
        }
39
40 4
        $rawError = $this->response->error;
41
42 4
        $data = null;
43 4
        if (property_exists($rawError, JsonRpcErrorInterface::ERROR_DATA_FIELD)) {
44 1
            $data = $rawError->data;
45 1
        }
46
47 4
        $this->error = new JsonRpcError(
48 4
            $rawError->{JsonRpcErrorInterface::ERROR_CODE_FIELD},
49 4
            $rawError->{JsonRpcErrorInterface::ERROR_MESSAGE_FIELD},
50
            $data
51 4
        );
52
53 4
        return $this->error;
54
    }
55
56
    /** {@inheritdoc} */
57 7
    public function isSuccessful()
58
    {
59 7
        return property_exists($this->response, JsonRpcResponseInterface::RESULT_FIELD) && !property_exists($this->response, JsonRpcResponseInterface::ERROR_FIELD);
60
    }
61
62
    /** {@inheritdoc} */
63 5
    public function getBody()
64
    {
65 5
        if (!$this->isSuccessful()) {
66 1
            return null;
67
        }
68
69 4
        return $this->response->result;
70
    }
71
72
    /** {@inheritdoc} */
73 6
    public function getVersion()
74
    {
75 6
        return $this->response->jsonrpc;
76
    }
77
78
    /** {@inheritdoc} */
79 8
    public function getId()
80
    {
81 8
        return $this->response->id;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 4
    public function jsonSerialize()
0 ignored issues
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...
88
    {
89
        $result = [
90 4
            self::VERSION_FIELD => JsonRpcClient::VERSION,
91 4
            self::ID_FIELD => $this->getId(),
92 4
        ];
93
94 4
        if ($this->isSuccessful()) {
95 2
            $result[self::RESULT_FIELD] = $this->getBody();
96 2
        }
97
98 4
        if (!$this->isSuccessful()) {
99 2
            $result[self::ERROR_FIELD] = $this->getError();
100 2
        }
101
102 4
        return $result;
103
    }
104
}
105