DogecoindResponse::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bouhnosaure\Dogecoin;
4
5
use ArrayAccess;
6
use Countable;
7
use JsonSerializable;
8
use Serializable;
9
10
use Psr\Http\Message\ResponseInterface;
11
12
class DogecoindResponse implements ResponseInterface, ArrayAccess, Countable, Serializable, JsonSerializable
13
{
14
    use MessageTrait, ResponseArrayTrait, ReadOnlyArrayTrait, SerializableContainerTrait;
15
16
    /**
17
     * Response instance.
18
     *
19
     * @var \Psr\Http\Message\ResponseInterface
20
     */
21
    protected $response;
22
23
    /**
24
     * Data container.
25
     *
26
     * @var array
27
     */
28
    protected $container = [];
29
30
    /**
31
     * Current key.
32
     *
33
     * @var string
34
     */
35
    protected $current;
36
37
    /**
38
     * Constructs new json response.
39
     *
40
     * @param \Psr\Http\Message\ResponseInterface $response
41
     *
42
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
43
     */
44 138
    public function __construct(ResponseInterface $response)
45
    {
46 138
        $this->response = $response;
47 138
        $this->container = json_decode($response->getBody(), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($response->getBody(), true) of type * is incompatible with the declared type array of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48 138
    }
49
50
    /**
51
     * Gets raw response.
52
     *
53
     * @return \Psr\Http\Message\ResponseInterface
54
     */
55 3
    public function response()
56
    {
57 3
        return $this->response;
58
    }
59
60
    /**
61
     * Sets response.
62
     *
63
     * @param \Psr\Http\Message\ResponseInterface $response
64
     *
65
     * @return static
66
     */
67 102
    public function setResponse(ResponseInterface $response)
68
    {
69 102
        $this->response = $response;
70
71 102
        return $this;
72
    }
73
74
    /**
75
     * Checks if response has error.
76
     *
77
     * @return bool
78
     */
79 45
    public function hasError()
80
    {
81 45
        return isset($this->container['error']);
82
    }
83
84
    /**
85
     * Gets error object.
86
     *
87
     * @return object|null
88
     */
89 24
    public function error()
90
    {
91 24
        if ($this->hasError()) {
92 21
            return $this->container['error'];
93
        }
94 3
    }
95
96
    /**
97
     * Checks if response has result.
98
     *
99
     * @return bool
100
     */
101 66
    public function hasResult()
102
    {
103 66
        return isset($this->container['result']);
104
    }
105
106
    /**
107
     * Gets result array.
108
     *
109
     * @return array|null
110
     */
111 63
    public function result()
112
    {
113 63
        if ($this->hasResult()) {
114 60
            return $this->container['result'];
115
        }
116 3
    }
117
118
    /**
119
     * Get response status code.
120
     *
121
     * @return int
122
     */
123 42
    public function getStatusCode()
124
    {
125 42
        return $this->response->getStatusCode();
126
    }
127
128
    /**
129
     * Return an instance with the specified status code and, optionally, reason phrase.
130
     *
131
     * @param int $code
132
     * @param string $reasonPhrase
133
     *
134
     * @return static
135
     */
136 3
    public function withStatus($code, $reasonPhrase = '')
137
    {
138 3
        $new = clone $this;
139
140 3
        return $new->setResponse(
141 3
            $this->response->withStatus($code, $reasonPhrase)
142 1
        );
143
    }
144
145
    /**
146
     * Gets the response reason phrase associated with the status code.
147
     *
148
     * @return string
149
     */
150 18
    public function getReasonPhrase()
151
    {
152 18
        return $this->response->getReasonPhrase();
153
    }
154
155
    /**
156
     * Creates new json response from response interface object.
157
     *
158
     * @param \Psr\Http\Message\ResponseInterface $response
159
     *
160
     * @return \Bouhnosaure\Dogecoin\DogecoindResponse
161
     */
162 138
    public static function createFrom(ResponseInterface $response)
163
    {
164 138
        return new self($response);
165
    }
166
}
167