Response   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 368
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 368
rs 10
wmc 24
lcom 2
cbo 2

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 4 1
A getDataFromResponse() 0 6 2
A getData() 0 4 1
A toArray() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 6 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getProtocolVersion() 0 4 1
A withProtocolVersion() 0 4 1
A getHeaders() 0 4 1
A hasHeader() 0 4 1
A getHeader() 0 4 1
A getHeaderLine() 0 4 1
A withHeader() 0 4 1
A withAddedHeader() 0 4 1
A withoutHeader() 0 4 1
A getBody() 0 4 1
A withBody() 0 4 1
A getStatusCode() 0 4 1
A withStatus() 0 4 1
A getReasonPhrase() 0 4 1
1
<?php
2
3
namespace SevenShores\Hubspot\Http;
4
5
use ArrayAccess;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\StreamInterface;
8
9
class Response implements ResponseInterface, ArrayAccess
10
{
11
    /**
12
     * @var \Psr\Http\Message\ResponseInterface
13
     */
14
    protected $response;
15
16
    /**
17
     * @var mixed
18
     */
19
    public $data;
20
21
    /**
22
     * @param \Psr\Http\Message\ResponseInterface $response
23
     */
24
    function __construct(ResponseInterface $response)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
    {
26
        $this->response = $response;
27
        $this->data = $this->getDataFromResponse($response);
28
    }
29
30
    /**
31
     * Get the api data from the response as usual.
32
     *
33
     * @param  string  $name
34
     * @return mixed
35
     */
36
    function __get($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
    {
38
        return $this->data->$name;
39
    }
40
41
    /**
42
     * @param  ResponseInterface $response
43
     * @return mixed
44
     */
45
    private function getDataFromResponse(ResponseInterface $response)
46
    {
47
        $contents = $response->getBody()->getContents();
48
49
        return $contents ? json_decode($contents) : null;
50
    }
51
52
    /**
53
     * Get the underlying data.
54
     *
55
     * @return mixed
56
     */
57
    function getData()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
    {
59
        return $this->data;
60
    }
61
62
    /**
63
     * Return an array of the data.
64
     *
65
     * @return array
66
     */
67
    function toArray()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
    {
69
        return json_decode(json_encode($this->data), true);
70
    }
71
72
    /**
73
     * Whether a offset exists
74
     *
75
     * @param  mixed  $offset
76
     * @return boolean
77
     */
78
    function offsetExists($offset)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
    {
80
        return isset($this->data->$offset);
81
    }
82
83
    /**
84
     * Offset to retrieve
85
     *
86
     * @param  mixed  $offset
87
     * @return mixed
88
     */
89
    function offsetGet($offset)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
90
    {
91
        $data = $this->toArray();
92
93
        return $data[$offset];
94
    }
95
96
    /**
97
     * Offset to set
98
     *
99
     * @param  mixed  $offset
100
     * @param  mixed  $value
101
     * @return void
102
     */
103
    function offsetSet($offset, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
104
    {
105
        $this->data->$offset = $value;
106
    }
107
108
    /**
109
     * Offset to unset
110
     *
111
     * @param  mixed  $offset
112
     * @return void
113
     */
114
    function offsetUnset($offset)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
115
    {
116
        unset($this->data->$offset);
117
    }
118
119
    /**
120
     * Retrieves the HTTP protocol version as a string.
121
     *
122
     * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
123
     *
124
     * @return string HTTP protocol version.
125
     */
126
    function getProtocolVersion()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
127
    {
128
        return $this->response->getProtocolVersion();
129
    }
130
131
    /**
132
     * Return an instance with the specified HTTP protocol version.
133
     *
134
     * The version string MUST contain only the HTTP version number (e.g.,
135
     * "1.1", "1.0").
136
     *
137
     * This method MUST be implemented in such a way as to retain the
138
     * immutability of the message, and MUST return an instance that has the
139
     * new protocol version.
140
     *
141
     * @param string $version HTTP protocol version
142
     * @return self
143
     */
144
    function withProtocolVersion($version)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
145
    {
146
        return $this->response->withProtocolVersion($version);
147
    }
148
149
    /**
150
     * Retrieves all message header values.
151
     *
152
     * The keys represent the header name as it will be sent over the wire, and
153
     * each value is an array of strings associated with the header.
154
     *
155
     *     // Represent the headers as a string
156
     *     foreach ($message->getHeaders() as $name => $values) {
157
     *         echo $name . ": " . implode(", ", $values);
158
     *     }
159
     *
160
     *     // Emit headers iteratively:
161
     *     foreach ($message->getHeaders() as $name => $values) {
162
     *         foreach ($values as $value) {
163
     *             header(sprintf('%s: %s', $name, $value), false);
164
     *         }
165
     *     }
166
     *
167
     * While header names are not case-sensitive, getHeaders() will preserve the
168
     * exact case in which headers were originally specified.
169
     *
170
     * @return array Returns an associative array of the message's headers. Each
171
     *     key MUST be a header name, and each value MUST be an array of strings
172
     *     for that header.
173
     */
174
    function getHeaders()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
175
    {
176
        return $this->response->getHeaders();
177
    }
178
179
    /**
180
     * Checks if a header exists by the given case-insensitive name.
181
     *
182
     * @param string $name Case-insensitive header field name.
183
     * @return bool Returns true if any header names match the given header
184
     *     name using a case-insensitive string comparison. Returns false if
185
     *     no matching header name is found in the message.
186
     */
187
    function hasHeader($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
188
    {
189
        return $this->response->hasHeader($name);
190
    }
191
192
    /**
193
     * Retrieves a message header value by the given case-insensitive name.
194
     *
195
     * This method returns an array of all the header values of the given
196
     * case-insensitive header name.
197
     *
198
     * If the header does not appear in the message, this method MUST return an
199
     * empty array.
200
     *
201
     * @param string $name Case-insensitive header field name.
202
     * @return string[] An array of string values as provided for the given
203
     *    header. If the header does not appear in the message, this method MUST
204
     *    return an empty array.
205
     */
206
    function getHeader($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
207
    {
208
        return $this->response->getHeader($name);
209
    }
210
211
    /**
212
     * Retrieves a comma-separated string of the values for a single header.
213
     *
214
     * This method returns all of the header values of the given
215
     * case-insensitive header name as a string concatenated together using
216
     * a comma.
217
     *
218
     * NOTE: Not all header values may be appropriately represented using
219
     * comma concatenation. For such headers, use getHeader() instead
220
     * and supply your own delimiter when concatenating.
221
     *
222
     * If the header does not appear in the message, this method MUST return
223
     * an empty string.
224
     *
225
     * @param string $name Case-insensitive header field name.
226
     * @return string A string of values as provided for the given header
227
     *    concatenated together using a comma. If the header does not appear in
228
     *    the message, this method MUST return an empty string.
229
     */
230
    function getHeaderLine($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
231
    {
232
        return $this->response->getHeaderLine($name);
233
    }
234
235
    /**
236
     * Return an instance with the provided value replacing the specified header.
237
     *
238
     * While header names are case-insensitive, the casing of the header will
239
     * be preserved by this function, and returned from getHeaders().
240
     *
241
     * This method MUST be implemented in such a way as to retain the
242
     * immutability of the message, and MUST return an instance that has the
243
     * new and/or updated header and value.
244
     *
245
     * @param string $name Case-insensitive header field name.
246
     * @param string|string[] $value Header value(s).
247
     * @return self
248
     * @throws \InvalidArgumentException for invalid header names or values.
249
     */
250
    function withHeader($name, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
251
    {
252
        return $this->response->withHeader($name, $value);
253
    }
254
255
    /**
256
     * Return an instance with the specified header appended with the given value.
257
     *
258
     * Existing values for the specified header will be maintained. The new
259
     * value(s) will be appended to the existing list. If the header did not
260
     * exist previously, it will be added.
261
     *
262
     * This method MUST be implemented in such a way as to retain the
263
     * immutability of the message, and MUST return an instance that has the
264
     * new header and/or value.
265
     *
266
     * @param string $name Case-insensitive header field name to add.
267
     * @param string|string[] $value Header value(s).
268
     * @return self
269
     * @throws \InvalidArgumentException for invalid header names or values.
270
     */
271
    function withAddedHeader($name, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
272
    {
273
        return $this->response->withAddedHeader($name, $value);
274
    }
275
276
    /**
277
     * Return an instance without the specified header.
278
     *
279
     * Header resolution MUST be done without case-sensitivity.
280
     *
281
     * This method MUST be implemented in such a way as to retain the
282
     * immutability of the message, and MUST return an instance that removes
283
     * the named header.
284
     *
285
     * @param string $name Case-insensitive header field name to remove.
286
     * @return self
287
     */
288
    function withoutHeader($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
289
    {
290
        return $this->response->withoutHeader($name);
291
    }
292
293
    /**
294
     * Gets the body of the message.
295
     *
296
     * @return StreamInterface Returns the body as a stream.
297
     */
298
    function getBody()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
299
    {
300
        return $this->response->getBody();
301
    }
302
303
    /**
304
     * Return an instance with the specified message body.
305
     *
306
     * The body MUST be a StreamInterface object.
307
     *
308
     * This method MUST be implemented in such a way as to retain the
309
     * immutability of the message, and MUST return a new instance that has the
310
     * new body stream.
311
     *
312
     * @param StreamInterface $body Body.
313
     * @return self
314
     * @throws \InvalidArgumentException When the body is not valid.
315
     */
316
    function withBody(StreamInterface $body)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
317
    {
318
        return $this->response->withBody($body);
319
    }
320
321
    /**
322
     * Gets the response status code.
323
     *
324
     * The status code is a 3-digit integer result code of the server's attempt
325
     * to understand and satisfy the request.
326
     *
327
     * @return int Status code.
328
     */
329
    function getStatusCode()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
330
    {
331
        return $this->response->getStatusCode();
332
    }
333
334
    /**
335
     * Return an instance with the specified status code and, optionally, reason phrase.
336
     *
337
     * If no reason phrase is specified, implementations MAY choose to default
338
     * to the RFC 7231 or IANA recommended reason phrase for the response's
339
     * status code.
340
     *
341
     * This method MUST be implemented in such a way as to retain the
342
     * immutability of the message, and MUST return an instance that has the
343
     * updated status and reason phrase.
344
     *
345
     * @link http://tools.ietf.org/html/rfc7231#section-6
346
     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
347
     * @param int $code The 3-digit integer result code to set.
348
     * @param string $reasonPhrase The reason phrase to use with the
349
     *     provided status code; if none is provided, implementations MAY
350
     *     use the defaults as suggested in the HTTP specification.
351
     * @return self
352
     * @throws \InvalidArgumentException For invalid status code arguments.
353
     */
354
    function withStatus($code, $reasonPhrase = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
355
    {
356
        return $this->response->withStatus($code, $reasonPhrase);
357
    }
358
359
    /**
360
     * Gets the response reason phrase associated with the status code.
361
     *
362
     * Because a reason phrase is not a required element in a response
363
     * status line, the reason phrase value MAY be null. Implementations MAY
364
     * choose to return the default RFC 7231 recommended reason phrase (or those
365
     * listed in the IANA HTTP Status Code Registry) for the response's
366
     * status code.
367
     *
368
     * @link http://tools.ietf.org/html/rfc7231#section-6
369
     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
370
     * @return string Reason phrase; must return an empty string if none present.
371
     */
372
    function getReasonPhrase()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
373
    {
374
        return $this->response->getReasonPhrase();
375
    }
376
}
377