Completed
Push — master ( e8e573...d0b5f0 )
by Yassine
11s
created

Response::getGraphList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook;
24
25
use Facebook\GraphNode\GraphNodeFactory;
26
use Facebook\Exception\ResponseException;
27
use Facebook\Exception\SDKException;
28
29
/**
30
 * @package Facebook
31
 */
32
class Response
33
{
34
    /**
35
     * @var int the HTTP status code response from Graph
36
     */
37
    protected $httpStatusCode;
38
39
    /**
40
     * @var array the headers returned from Graph
41
     */
42
    protected $headers;
43
44
    /**
45
     * @var string the raw body of the response from Graph
46
     */
47
    protected $body;
48
49
    /**
50
     * @var array the decoded body of the Graph response
51
     */
52
    protected $decodedBody = [];
53
54
    /**
55
     * @var Request the original request that returned this response
56
     */
57
    protected $request;
58
59
    /**
60
     * @var SDKException the exception thrown by this request
61
     */
62
    protected $thrownException;
63
64
    /**
65
     * Creates a new Response entity.
66
     *
67
     * @param Request     $request
68
     * @param null|string $body
69
     * @param null|int    $httpStatusCode
70
     * @param null|array  $headers
71
     */
72
    public function __construct(Request $request, $body = null, $httpStatusCode = null, array $headers = [])
73
    {
74
        $this->request = $request;
75
        $this->body = $body;
76
        $this->httpStatusCode = $httpStatusCode;
77
        $this->headers = $headers;
78
79
        $this->decodeBody();
80
    }
81
82
    /**
83
     * Return the original request that returned this response.
84
     *
85
     * @return Request
86
     */
87
    public function getRequest()
88
    {
89
        return $this->request;
90
    }
91
92
    /**
93
     * Return the Application entity used for this response.
94
     *
95
     * @return Application
96
     */
97
    public function getApplication()
98
    {
99
        return $this->request->getApplication();
100
    }
101
102
    /**
103
     * Return the access token that was used for this response.
104
     *
105
     * @return null|string
106
     */
107
    public function getAccessToken()
108
    {
109
        return $this->request->getAccessToken();
110
    }
111
112
    /**
113
     * Return the HTTP status code for this response.
114
     *
115
     * @return int
116
     */
117
    public function getHttpStatusCode()
118
    {
119
        return $this->httpStatusCode;
120
    }
121
122
    /**
123
     * Return the HTTP headers for this response.
124
     *
125
     * @return array
126
     */
127
    public function getHeaders()
128
    {
129
        return $this->headers;
130
    }
131
132
    /**
133
     * Return the raw body response.
134
     *
135
     * @return string
136
     */
137
    public function getBody()
138
    {
139
        return $this->body;
140
    }
141
142
    /**
143
     * Return the decoded body response.
144
     *
145
     * @return array
146
     */
147
    public function getDecodedBody()
148
    {
149
        return $this->decodedBody;
150
    }
151
152
    /**
153
     * Get the app secret proof that was used for this response.
154
     *
155
     * @return null|string
156
     */
157
    public function getAppSecretProof()
158
    {
159
        return $this->request->getAppSecretProof();
160
    }
161
162
    /**
163
     * Get the ETag associated with the response.
164
     *
165
     * @return null|string
166
     */
167
    public function getETag()
168
    {
169
        return isset($this->headers['ETag']) ? $this->headers['ETag'] : null;
170
    }
171
172
    /**
173
     * Get the version of Graph that returned this response.
174
     *
175
     * @return null|string
176
     */
177
    public function getGraphVersion()
178
    {
179
        return isset($this->headers['Facebook-API-Version']) ? $this->headers['Facebook-API-Version'] : null;
180
    }
181
182
    /**
183
     * Returns true if Graph returned an error message.
184
     *
185
     * @return bool
186
     */
187
    public function isError()
188
    {
189
        return isset($this->decodedBody['error']);
190
    }
191
192
    /**
193
     * Throws the exception.
194
     *
195
     * @throws SDKException
196
     */
197
    public function throwException()
198
    {
199
        throw $this->thrownException;
200
    }
201
202
    /**
203
     * Instantiates an exception to be thrown later.
204
     */
205
    public function makeException()
206
    {
207
        $this->thrownException = ResponseException::create($this);
208
    }
209
210
    /**
211
     * Returns the exception that was thrown for this request.
212
     *
213
     * @return null|ResponseException
214
     */
215
    public function getThrownException()
216
    {
217
        return $this->thrownException;
218
    }
219
220
    /**
221
     * Convert the raw response into an array if possible.
222
     *
223
     * Graph will return 2 types of responses:
224
     * - JSON(P)
225
     *    Most responses from Graph are JSON(P)
226
     * - application/x-www-form-urlencoded key/value pairs
227
     *    Happens on the `/oauth/access_token` endpoint when exchanging
228
     *    a short-lived access token for a long-lived access token
229
     * - And sometimes nothing :/ but that'd be a bug.
230
     */
231
    public function decodeBody()
232
    {
233
        $this->decodedBody = json_decode($this->body, true);
234
235
        if ($this->decodedBody === null) {
236
            $this->decodedBody = [];
237
            parse_str($this->body, $this->decodedBody);
238
        } elseif (is_bool($this->decodedBody)) {
239
            // Backwards compatibility for Graph < 2.1.
240
            // Mimics 2.1 responses.
241
            // @TODO Remove this after Graph 2.0 is no longer supported
242
            $this->decodedBody = ['success' => $this->decodedBody];
243
        } elseif (is_numeric($this->decodedBody)) {
244
            $this->decodedBody = ['id' => $this->decodedBody];
245
        }
246
247
        if (!is_array($this->decodedBody)) {
248
            $this->decodedBody = [];
249
        }
250
251
        if ($this->isError()) {
252
            $this->makeException();
253
        }
254
    }
255
256
    /**
257
     * Instantiate a new GraphNode from response.
258
     *
259
     * @param null|string $subclassName the GraphNode subclass to cast to
260
     *
261
     * @throws SDKException
262
     *
263
     * @return \Facebook\GraphNode\GraphNode
264
     */
265
    public function getGraphNode($subclassName = null)
266
    {
267
        $factory = new GraphNodeFactory($this);
268
269
        return $factory->makeGraphNode($subclassName);
270
    }
271
272
    /**
273
     * Convenience method for creating a GraphAlbum collection.
274
     *
275
     * @throws SDKException
276
     *
277
     * @return \Facebook\GraphNode\GraphAlbum
278
     */
279
    public function getGraphAlbum()
280
    {
281
        $factory = new GraphNodeFactory($this);
282
283
        return $factory->makeGraphAlbum();
284
    }
285
286
    /**
287
     * Convenience method for creating a GraphPage collection.
288
     *
289
     * @throws SDKException
290
     *
291
     * @return \Facebook\GraphNode\GraphPage
292
     */
293
    public function getGraphPage()
294
    {
295
        $factory = new GraphNodeFactory($this);
296
297
        return $factory->makeGraphPage();
298
    }
299
300
    /**
301
     * Convenience method for creating a GraphSessionInfo collection.
302
     *
303
     * @throws SDKException
304
     *
305
     * @return \Facebook\GraphNode\GraphSessionInfo
306
     */
307
    public function getGraphSessionInfo()
308
    {
309
        $factory = new GraphNodeFactory($this);
310
311
        return $factory->makeGraphSessionInfo();
312
    }
313
314
    /**
315
     * Convenience method for creating a GraphUser collection.
316
     *
317
     * @throws SDKException
318
     *
319
     * @return \Facebook\GraphNode\GraphUser
320
     */
321
    public function getGraphUser()
322
    {
323
        $factory = new GraphNodeFactory($this);
324
325
        return $factory->makeGraphUser();
326
    }
327
328
    /**
329
     * Convenience method for creating a GraphEvent collection.
330
     *
331
     * @throws SDKException
332
     *
333
     * @return \Facebook\GraphNode\GraphEvent
334
     */
335
    public function getGraphEvent()
336
    {
337
        $factory = new GraphNodeFactory($this);
338
339
        return $factory->makeGraphEvent();
340
    }
341
342
    /**
343
     * Convenience method for creating a GraphGroup collection.
344
     *
345
     * @throws SDKException
346
     *
347
     * @return \Facebook\GraphNode\GraphGroup
348
     */
349
    public function getGraphGroup()
350
    {
351
        $factory = new GraphNodeFactory($this);
352
353
        return $factory->makeGraphGroup();
354
    }
355
356
    /**
357
     * Instantiate a new GraphEdge from response.
358
     *
359
     * @param null|string $subclassName the GraphNode subclass to cast list items to
360
     * @param bool        $auto_prefix  toggle to auto-prefix the subclass name
361
     *
362
     * @throws SDKException
363
     *
364
     * @return \Facebook\GraphNode\GraphEdge
365
     */
366
    public function getGraphEdge($subclassName = null, $auto_prefix = true)
367
    {
368
        $factory = new GraphNodeFactory($this);
369
370
        return $factory->makeGraphEdge($subclassName, $auto_prefix);
371
    }
372
}
373