Response::isError()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Postpay\Http;
4
5
use Postpay\Exceptions\GraphQLException;
6
use Postpay\Exceptions\RESTfulException;
7
8
class Response
9
{
10
    /**
11
     * @var \Postpay\Http\Request The original request.
12
     */
13
    protected $request;
14
15
    /**
16
     * @var int The HTTP response status code.
17
     */
18
    protected $statusCode;
19
20
    /**
21
     * @var array The headers returned from API.
22
     */
23
    protected $headers;
24
25
    /**
26
     * @var string The raw body of the response.
27
     */
28
    protected $body;
29
30
    /**
31
     * @var array The decoded body.
32
     */
33
    protected $decodedBody = [];
34
35
    /**
36
     * @var string The exception type to be thrown.
37
     */
38
    protected $exc;
39
40
    /**
41
     * @var \Postpay\Exceptions\PostpayException The exception thrown.
42
     */
43
    protected $thrownException;
44
45
    /**
46
     * Creates a new Response entity.
47
     *
48
     * @param \Postpay\Http\Request $request
49
     * @param int|null              $statusCode
50
     * @param array|null            $headers
51
     * @param string|null           $body
52
     */
53 21
    public function __construct(
54
        Request $request,
55
        $statusCode = null,
56
        array $headers = [],
57
        $body = null
58
    ) {
59 21
        $this->request = $request;
60 21
        $this->statusCode = $statusCode;
61 21
        $this->headers = $headers;
62 21
        $this->body = $body;
63
64 21
        if ($this->request->isGraphQL()) {
65 1
            $this->exc = GraphQLException::class;
66
        } else {
67 20
            $this->exc = RESTfulException::class;
68
        }
69 21
        $this->decodeBody();
70 21
    }
71
72
    /**
73
     * Return the original request that returned this response.
74
     *
75
     * @return \Postpay\Http\Request
76
     */
77 7
    public function getRequest()
78
    {
79 7
        return $this->request;
80
    }
81
82
    /**
83
     * Return the HTTP status code.
84
     *
85
     * @return int
86
     */
87 5
    public function getStatusCode()
88
    {
89 5
        return $this->statusCode;
90
    }
91
92
    /**
93
     * Return the HTTP headers.
94
     *
95
     * @return array
96
     */
97 1
    public function getHeaders()
98
    {
99 1
        return $this->headers;
100
    }
101
102
    /**
103
     * Return the JSON response.
104
     *
105
     * @return array
106
     */
107 7
    public function json()
108
    {
109 7
        return $this->decodedBody;
110
    }
111
112
    /**
113
     * Returns true if API returned an error message.
114
     *
115
     * @return bool
116
     */
117 21
    public function isError()
118
    {
119 21
        $excClassName = $this->exc;
120
        return (
121 21
            $this->statusCode >= 400 or
122 21
            isset($this->decodedBody[$excClassName::ERROR_KEY])
123
        );
124
    }
125
126
    /**
127
     * Convert the raw response into an array if possible.
128
     */
129 21
    public function decodeBody()
130
    {
131 21
        $this->decodedBody = json_decode($this->body, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($this->body, true) of type * is incompatible with the declared type array of property $decodedBody.

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...
132
133 21
        if (!is_array($this->decodedBody)) {
134 15
            $this->decodedBody = [];
135
        }
136
137 21
        if ($this->isError()) {
138 4
            $this->thrownException = new $this->exc($this);
139
        }
140 21
    }
141
142
    /**
143
     * Returns the exception that was thrown.
144
     *
145
     * @return \Postpay\Exceptions\ApiException|null
146
     */
147 2
    public function getThrownException()
148
    {
149 2
        return $this->thrownException;
150
    }
151
}
152