Completed
Pull Request — master (#19)
by
unknown
05:55
created

Response   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 53.66%

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 2
cbo 1
dl 0
loc 206
ccs 22
cts 41
cp 0.5366
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A ok() 0 7 3
A getError() 0 4 2
A rateLimited() 0 4 1
A parseJson() 0 9 2
A result() 0 4 1
A setResult() 0 6 1
A getRequest() 0 4 1
A getStatusCode() 0 4 1
A getHeaders() 0 4 1
A getHeader() 0 6 2
A getRateLimit() 0 6 2
A getRemainingRequests() 0 6 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 22
    public function __construct(Request $request, $statusCode, $rawBody, array $headers)
77
    {
78 22
        $this->request = $request;
79 22
        $this->statusCode = (int) $statusCode;
80 22
        $this->rawBody = (string) $rawBody;
81 22
        $this->body = $this->parseJson($this->rawBody);
82 22
        $this->headers = $headers;
83 22
    }
84
85
    /**
86
     * Checks if the response is okay.
87
     *
88
     * @return bool Whether the response is okay.
89
     */
90 22
    public function ok()
91
    {
92
        return
93 22
            !isset($this->body->error)
94 22
            && $this->statusCode >= 200
95 22
            && $this->statusCode < 300;
96
    }
97
98
    /**
99
     * Get the HTTP status code.
100
     *
101
     * @return int
102
     */
103
    public function getStatusCode()
104
    {
105
        return $this->statusCode;
106
    }
107
108
    /**
109
     * Get the error message.
110
     *
111
     * @return null|string
112
     */
113
    public function getError()
114
    {
115
        return !empty($this->body->message) ? (string) $this->body->message : null;
116
    }
117
118
    /**
119
     * Checks if the response is rate-limited.
120
     *
121
     * @return bool Whether the response is rate-limited.
122
     */
123 22
    public function rateLimited()
124
    {
125 22
        return $this->statusCode === 429;
126
    }
127
128
    /**
129
     * Parse the response json.
130
     *
131
     * @param string $rawBody The raw response body.
132
     * @param bool   $toArray Return as array?
133
     *
134
     * @throws MalformedJson
135
     *
136
     * @return mixed The parsed json.
137
     */
138 22
    private function parseJson($rawBody, $toArray = false)
139
    {
140 22
        $json = json_decode($rawBody, $toArray);
141 22
        if (json_last_error() === JSON_ERROR_NONE) {
142 22
            return $json;
143
        } else {
144
            throw new MalformedJson();
145
        }
146
    }
147
148
    /**
149
     * Get the processed result.
150
     *
151
     * @return mixed The processed result.
152
     */
153 20
    public function result()
154
    {
155 20
        return $this->result;
156
    }
157
158
    /**
159
     * Set the processed result.
160
     *
161
     * @param mixed $result The result.
162
     *
163
     * @return $this
164
     */
165 20
    public function setResult($result)
166
    {
167 20
        $this->result = $result;
168
169 20
        return $this;
170
    }
171
172
    /**
173
     * Get the request object.
174
     *
175
     * @return Request
176
     */
177
    public function getRequest()
178
    {
179
        return $this->request;
180
    }
181
182
    /**
183
     * Get the HTTP Headers.
184
     *
185
     * @return array
186
     */
187
    public function getHeaders()
188
    {
189
        return $this->headers;
190
    }
191
192
    /**
193
     * Get a specific HTTP header.
194
     *
195
     * @param string $header
196
     *
197
     * @return string|null The header value.
198
     */
199
    public function getHeader($header)
200
    {
201
        return isset($this->headers[$header])
202
            ? $this->headers[$header]
203
            : null;
204
    }
205
206
    /**
207
     * Get the rate-limit.
208
     *
209
     * @return int|null The rate-limit.
210
     */
211
    public function getRateLimit()
212
    {
213
        $limit = $this->getHeader('X-Ratelimit-Limit');
214
215
        return is_numeric($limit) ? (int) $limit : null;
216
    }
217
218
    /**
219
     * Get the remaining requests before reaching the rate-limit.
220
     *
221
     * @return int|null The remaining requests.
222
     */
223
    public function getRemainingRequests()
224
    {
225
        $remaining = $this->getHeader('X-Ratelimit-Remaining');
226
227
        return is_numeric($remaining) ? (int) $remaining : null;
228
    }
229
}
230