InstagramResponse   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 374
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 374
rs 9.76
c 0
b 0
f 0
wmc 33

26 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequest() 0 3 1
A getGraphSessionInfo() 0 5 1
A getGraphGroup() 0 5 1
A getGraphList() 0 3 1
A __construct() 0 8 1
A getHttpStatusCode() 0 3 1
A throwException() 0 3 1
A getGraphUser() 0 5 1
A getDecodedBody() 0 3 1
A getGraphEvent() 0 5 1
A getBody() 0 3 1
A getGraphEdge() 0 5 1
A getThrownException() 0 3 1
A getGraphVersion() 0 3 2
A getAccessToken() 0 3 1
A getAppSecretProof() 0 3 1
A makeException() 0 3 1
A getGraphNode() 0 5 1
A decodeBody() 0 22 6
A getApp() 0 3 1
A getGraphAlbum() 0 5 1
A isError() 0 3 1
A getGraphPage() 0 5 1
A getHeaders() 0 3 1
A getETag() 0 3 2
A getGraphObject() 0 3 1
1
<?php
2
3
namespace Maztech;
4
5
use Maztech\GraphNodes\GraphNodeFactory;
6
use Maztech\Exceptions\InstagramResponseException;
7
use Maztech\Exceptions\InstagramSDKException;
8
9
/**
10
 * Class InstagramResponse
11
 *
12
 * @package Instagram
13
 */
14
class InstagramResponse
15
{
16
    /**
17
     * @var int The HTTP status code response from Graph.
18
     */
19
    protected $httpStatusCode;
20
21
    /**
22
     * @var array The headers returned from Graph.
23
     */
24
    protected $headers;
25
26
    /**
27
     * @var string The raw body of the response from Graph.
28
     */
29
    protected $body;
30
31
    /**
32
     * @var array The decoded body of the Graph response.
33
     */
34
    protected $decodedBody = [];
35
36
    /**
37
     * @var InstagramRequest The original request that returned this response.
38
     */
39
    protected $request;
40
41
    /**
42
     * @var InstagramSDKException The exception thrown by this request.
43
     */
44
    protected $thrownException;
45
46
    /**
47
     * Creates a new Response entity.
48
     *
49
     * @param InstagramRequest $request
50
     * @param string|null     $body
51
     * @param int|null        $httpStatusCode
52
     * @param array|null      $headers
53
     */
54
    public function __construct(InstagramRequest $request, $body = null, $httpStatusCode = null, array $headers = [])
55
    {
56
        $this->request = $request;
57
        $this->body = $body;
58
        $this->httpStatusCode = $httpStatusCode;
59
        $this->headers = $headers;
60
61
        $this->decodeBody();
62
    }
63
64
    /**
65
     * Return the original request that returned this response.
66
     *
67
     * @return InstagramRequest
68
     */
69
    public function getRequest()
70
    {
71
        return $this->request;
72
    }
73
74
    /**
75
     * Return the InstagramApp entity used for this response.
76
     *
77
     * @return InstagramApp
78
     */
79
    public function getApp()
80
    {
81
        return $this->request->getApp();
82
    }
83
84
    /**
85
     * Return the access token that was used for this response.
86
     *
87
     * @return string|null
88
     */
89
    public function getAccessToken()
90
    {
91
        return $this->request->getAccessToken();
92
    }
93
94
    /**
95
     * Return the HTTP status code for this response.
96
     *
97
     * @return int
98
     */
99
    public function getHttpStatusCode()
100
    {
101
        return $this->httpStatusCode;
102
    }
103
104
    /**
105
     * Return the HTTP headers for this response.
106
     *
107
     * @return array
108
     */
109
    public function getHeaders()
110
    {
111
        return $this->headers;
112
    }
113
114
    /**
115
     * Return the raw body response.
116
     *
117
     * @return string
118
     */
119
    public function getBody()
120
    {
121
        return $this->body;
122
    }
123
124
    /**
125
     * Return the decoded body response.
126
     *
127
     * @return array
128
     */
129
    public function getDecodedBody()
130
    {
131
        return $this->decodedBody;
132
    }
133
134
    /**
135
     * Get the app secret proof that was used for this response.
136
     *
137
     * @return string|null
138
     */
139
    public function getAppSecretProof()
140
    {
141
        return $this->request->getAppSecretProof();
142
    }
143
144
    /**
145
     * Get the ETag associated with the response.
146
     *
147
     * @return string|null
148
     */
149
    public function getETag()
150
    {
151
        return isset($this->headers['ETag']) ? $this->headers['ETag'] : null;
152
    }
153
154
    /**
155
     * Get the version of Graph that returned this response.
156
     *
157
     * @return string|null
158
     */
159
    public function getGraphVersion()
160
    {
161
        return isset($this->headers['Instagram-API-Version']) ? $this->headers['Instagram-API-Version'] : null;
162
    }
163
164
    /**
165
     * Returns true if Graph returned an error message.
166
     *
167
     * @return boolean
168
     */
169
    public function isError()
170
    {
171
        return isset($this->decodedBody['error']);
172
    }
173
174
    /**
175
     * Throws the exception.
176
     *
177
     * @throws InstagramSDKException
178
     */
179
    public function throwException()
180
    {
181
        throw $this->thrownException;
182
    }
183
184
    /**
185
     * Instantiates an exception to be thrown later.
186
     */
187
    public function makeException()
188
    {
189
        $this->thrownException = InstagramResponseException::create($this);
190
    }
191
192
    /**
193
     * Returns the exception that was thrown for this request.
194
     *
195
     * @return InstagramResponseException|null
196
     */
197
    public function getThrownException()
198
    {
199
        return $this->thrownException;
200
    }
201
202
    /**
203
     * Convert the raw response into an array if possible.
204
     *
205
     * Graph will return 2 types of responses:
206
     * - JSON(P)
207
     *    Most responses from Graph are JSON(P)
208
     * - application/x-www-form-urlencoded key/value pairs
209
     *    Happens on the `/oauth/access_token` endpoint when exchanging
210
     *    a short-lived access token for a long-lived access token
211
     * - And sometimes nothing :/ but that'd be a bug.
212
     */
213
    public function decodeBody()
214
    {
215
        $this->decodedBody = json_decode($this->body, true);
216
217
        if ($this->decodedBody === null) {
218
            $this->decodedBody = [];
219
            parse_str($this->body, $this->decodedBody);
220
        } elseif (is_bool($this->decodedBody)) {
221
            // Backwards compatibility for Graph < 2.1.
222
            // Mimics 2.1 responses.
223
            // @TODO Remove this after Graph 2.0 is no longer supported
224
            $this->decodedBody = ['success' => $this->decodedBody];
225
        } elseif (is_numeric($this->decodedBody)) {
226
            $this->decodedBody = ['id' => $this->decodedBody];
227
        }
228
229
        if (!is_array($this->decodedBody)) {
230
            $this->decodedBody = [];
231
        }
232
233
        if ($this->isError()) {
234
            $this->makeException();
235
        }
236
    }
237
238
    /**
239
     * Instantiate a new GraphObject from response.
240
     *
241
     * @param string|null $subclassName The GraphNode subclass to cast to.
242
     *
243
     * @return \Instagram\GraphNodes\GraphObject
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
244
     *
245
     * @throws InstagramSDKException
246
     *
247
     * @deprecated 5.0.0 getGraphObject() has been renamed to getGraphNode()
248
     * @todo v6: Remove this method
249
     */
250
    public function getGraphObject($subclassName = null)
251
    {
252
        return $this->getGraphNode($subclassName);
253
    }
254
255
    /**
256
     * Instantiate a new GraphNode from response.
257
     *
258
     * @param string|null $subclassName The GraphNode subclass to cast to.
259
     *
260
     * @return \Instagram\GraphNodes\GraphNode
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
261
     *
262
     * @throws InstagramSDKException
263
     */
264
    public function getGraphNode($subclassName = null)
265
    {
266
        $factory = new GraphNodeFactory($this);
267
268
        return $factory->makeGraphNode($subclassName);
269
    }
270
271
    /**
272
     * Convenience method for creating a GraphAlbum collection.
273
     *
274
     * @return \Instagram\GraphNodes\GraphAlbum
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphAlbum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
275
     *
276
     * @throws InstagramSDKException
277
     */
278
    public function getGraphAlbum()
279
    {
280
        $factory = new GraphNodeFactory($this);
281
282
        return $factory->makeGraphAlbum();
283
    }
284
285
    /**
286
     * Convenience method for creating a GraphPage collection.
287
     *
288
     * @return \Instagram\GraphNodes\GraphPage
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphPage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
289
     *
290
     * @throws InstagramSDKException
291
     */
292
    public function getGraphPage()
293
    {
294
        $factory = new GraphNodeFactory($this);
295
296
        return $factory->makeGraphPage();
297
    }
298
299
    /**
300
     * Convenience method for creating a GraphSessionInfo collection.
301
     *
302
     * @return \Instagram\GraphNodes\GraphSessionInfo
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphSessionInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
303
     *
304
     * @throws InstagramSDKException
305
     */
306
    public function getGraphSessionInfo()
307
    {
308
        $factory = new GraphNodeFactory($this);
309
310
        return $factory->makeGraphSessionInfo();
311
    }
312
313
    /**
314
     * Convenience method for creating a GraphUser collection.
315
     *
316
     * @return \Instagram\GraphNodes\GraphUser
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
317
     *
318
     * @throws InstagramSDKException
319
     */
320
    public function getGraphUser()
321
    {
322
        $factory = new GraphNodeFactory($this);
323
324
        return $factory->makeGraphUser();
325
    }
326
327
    /**
328
     * Convenience method for creating a GraphEvent collection.
329
     *
330
     * @return \Instagram\GraphNodes\GraphEvent
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
331
     *
332
     * @throws InstagramSDKException
333
     */
334
    public function getGraphEvent()
335
    {
336
        $factory = new GraphNodeFactory($this);
337
338
        return $factory->makeGraphEvent();
339
    }
340
341
    /**
342
     * Convenience method for creating a GraphGroup collection.
343
     *
344
     * @return \Instagram\GraphNodes\GraphGroup
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphGroup was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
345
     *
346
     * @throws InstagramSDKException
347
     */
348
    public function getGraphGroup()
349
    {
350
        $factory = new GraphNodeFactory($this);
351
352
        return $factory->makeGraphGroup();
353
    }
354
355
    /**
356
     * Instantiate a new GraphList from response.
357
     *
358
     * @param string|null $subclassName The GraphNode subclass to cast list items to.
359
     * @param boolean     $auto_prefix  Toggle to auto-prefix the subclass name.
360
     *
361
     * @return \Instagram\GraphNodes\GraphList
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
362
     *
363
     * @throws InstagramSDKException
364
     *
365
     * @deprecated 5.0.0 getGraphList() has been renamed to getGraphEdge()
366
     * @todo v6: Remove this method
367
     */
368
    public function getGraphList($subclassName = null, $auto_prefix = true)
369
    {
370
        return $this->getGraphEdge($subclassName, $auto_prefix);
371
    }
372
373
    /**
374
     * Instantiate a new GraphEdge from response.
375
     *
376
     * @param string|null $subclassName The GraphNode subclass to cast list items to.
377
     * @param boolean     $auto_prefix  Toggle to auto-prefix the subclass name.
378
     *
379
     * @return \Instagram\GraphNodes\GraphEdge
0 ignored issues
show
Bug introduced by
The type Instagram\GraphNodes\GraphEdge was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
380
     *
381
     * @throws InstagramSDKException
382
     */
383
    public function getGraphEdge($subclassName = null, $auto_prefix = true)
384
    {
385
        $factory = new GraphNodeFactory($this);
386
387
        return $factory->makeGraphEdge($subclassName, $auto_prefix);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $factory->makeGra...lassName, $auto_prefix) returns the type Maztech\GraphNodes\GraphEdge which is incompatible with the documented return type Instagram\GraphNodes\GraphEdge.
Loading history...
388
    }
389
}
390