Completed
Push — master ( dbab92...26d863 )
by Hans
19:26 queued 18:02
created

Response::getStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Pinterest PHP library.
5
 *
6
 * (c) Hans Ott <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 *
11
 * Source: https://github.com/hansott/pinterest-php
12
 */
13
14
namespace Pinterest\Http;
15
16
use Pinterest\Http\Exceptions\MalformedJson;
17
18
/**
19
 * The response class.
20
 *
21
 * @author Hans Ott <[email protected]>
22
 * @author Toon Daelman <[email protected]>
23
 */
24
final class Response
25
{
26
    /**
27
     * The parsed response body.
28
     *
29
     * @var mixed
30
     */
31
    public $body;
32
33
    /**
34
     * The response status code.
35
     *
36
     * @var int
37
     */
38
    private $statusCode;
39
40
    /**
41
     * The raw response body.
42
     *
43
     * @var string
44
     */
45
    private $rawBody;
46
47
    /**
48
     * The HTTP headers.
49
     *
50
     * @var array
51
     */
52
    private $headers;
53
54
    /**
55
     * The processed result.
56
     *
57
     * @var mixed
58
     */
59
    private $result;
60
61
    /**
62
     * The request object.
63
     *
64
     * @var Request
65
     */
66
    private $request;
67
68
    /**
69
     * The constructor.
70
     *
71
     * @param Request $request The request object.
72
     * @param int $statusCode The status code.
73
     * @param string $rawBody The raw response body.
74
     * @param array $headers A key => value representation of response headers.
75
     *
76
     * @throws MalformedJson
77
     */
78 34
    public function __construct(Request $request, $statusCode, $rawBody, array $headers)
79
    {
80 34
        $this->request = $request;
81 34
        $this->statusCode = (int) $statusCode;
82 34
        $this->rawBody = (string) $rawBody;
83 34
        $this->body = $this->parseJson($this->rawBody);
84 34
        $this->headers = $headers;
85 34
    }
86
87
    /**
88
     * Checks if the response is okay.
89
     *
90
     * @return bool Whether the response is okay.
91
     */
92 34
    public function ok()
93
    {
94
        return (
95 34
            !isset($this->body->error)
96 34
            && $this->statusCode >= 200
97 34
            && $this->statusCode < 300
98 17
        );
99
    }
100
101
    /**
102
     * Get the HTTP status code.
103
     *
104
     * @return int
105
     */
106
    public function getStatusCode()
107
    {
108
        return $this->statusCode;
109
    }
110
111
    /**
112
     * Get the error message.
113
     *
114
     * @return null|string
115
     */
116
    public function getError()
117
    {
118
        return !empty($this->body->message) ? (string) $this->body->message : null;
119
    }
120
121
    /**
122
     * Checks if the response is rate-limited.
123
     *
124
     * @return bool Whether the response is rate-limited.
125
     */
126 34
    public function rateLimited()
127
    {
128 34
        return $this->statusCode === 429;
129
    }
130
131
    /**
132
     * Parse the response json.
133
     *
134
     * @param string $rawBody The raw response body.
135
     * @param bool   $toArray Return as array?
136
     *
137
     * @throws MalformedJson
138
     *
139
     * @return mixed The parsed json.
140
     */
141 34
    private function parseJson($rawBody, $toArray = false)
142
    {
143 34
        $json = json_decode($rawBody, $toArray);
144 34
        if (json_last_error() === JSON_ERROR_NONE) {
145 34
            return $json;
146
        } else {
147
            throw new MalformedJson();
148
        }
149
    }
150
151
    /**
152
     * Get the processed result.
153
     *
154
     * @return mixed The processed result.
155
     */
156 28
    public function result()
157
    {
158 28
        return $this->result;
159
    }
160
161
    /**
162
     * Set the processed result.
163
     *
164
     * @param mixed $result The result.
165
     *
166
     * @return $this
167
     */
168 28
    public function setResult($result)
169
    {
170 28
        $this->result = $result;
171
172 28
        return $this;
173
    }
174
175
    /**
176
     * Get the request object.
177
     *
178
     * @return Request
179
     */
180
    public function getRequest()
181
    {
182
        return $this->request;
183
    }
184
185
    /**
186
     * Get the HTTP Headers.
187
     *
188
     * @return array
189
     */
190 4
    public function getHeaders()
191
    {
192 4
        return $this->headers;
193
    }
194
195
    /**
196
     * Get a specific HTTP header.
197
     *
198
     * @param string $header
199
     *
200
     * @return string|null The header value.
201
     */
202 4
    public function getHeader($header)
203
    {
204 4
        return isset($this->headers[$header])
205 4
            ? $this->headers[$header]
206 4
            : null;
207
    }
208
209
    /**
210
     * Get the rate-limit.
211
     *
212
     * @return int|null The rate-limit.
213
     */
214 4
    public function getRateLimit()
215
    {
216 4
        $limit = $this->getHeader('X-Ratelimit-Limit');
217
218 4
        return is_numeric($limit) ? (int) $limit : null;
219
    }
220
221
    /**
222
     * Get the remaining requests before reaching the rate-limit.
223
     *
224
     * @return int|null The remaining requests.
225
     */
226 4
    public function getRemainingRequests()
227
    {
228 4
        $remaining = $this->getHeader('X-Ratelimit-Remaining');
229
230 4
        return is_numeric($remaining) ? (int) $remaining : null;
231
    }
232
}
233