Passed
Pull Request — master (#888)
by Tobias
01:50
created

Response::getApp()   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 0
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
 */
24
namespace Facebook;
25
26
use Facebook\GraphNode\GraphNodeFactory;
27
use Facebook\Exception\ResponseException;
28
use Facebook\Exception\SDKException;
29
30
/**
31
 *
32
 * @package Facebook
33
 */
34
class Response
35
{
36
    /**
37
     * @var int The HTTP status code response from Graph.
38
     */
39
    protected $httpStatusCode;
40
41
    /**
42
     * @var array The headers returned from Graph.
43
     */
44
    protected $headers;
45
46
    /**
47
     * @var string The raw body of the response from Graph.
48
     */
49
    protected $body;
50
51
    /**
52
     * @var array The decoded body of the Graph response.
53
     */
54
    protected $decodedBody = [];
55
56
    /**
57
     * @var Request The original request that returned this response.
58
     */
59
    protected $request;
60
61
    /**
62
     * @var SDKException The exception thrown by this request.
63
     */
64
    protected $thrownException;
65
66
    /**
67
     * Creates a new Response entity.
68
     *
69
     * @param Request $request
70
     * @param string|null     $body
71
     * @param int|null        $httpStatusCode
72
     * @param array|null      $headers
73
     */
74
    public function __construct(Request $request, $body = null, $httpStatusCode = null, array $headers = [])
75
    {
76
        $this->request = $request;
77
        $this->body = $body;
78
        $this->httpStatusCode = $httpStatusCode;
79
        $this->headers = $headers;
80
81
        $this->decodeBody();
82
    }
83
84
    /**
85
     * Return the original request that returned this response.
86
     *
87
     * @return Request
88
     */
89
    public function getRequest()
90
    {
91
        return $this->request;
92
    }
93
94
    /**
95
     * Return the FacebookApp entity used for this response.
96
     *
97
     * @return Application
98
     */
99
    public function getApp()
100
    {
101
        return $this->request->getApp();
102
    }
103
104
    /**
105
     * Return the access token that was used for this response.
106
     *
107
     * @return string|null
108
     */
109
    public function getAccessToken()
110
    {
111
        return $this->request->getAccessToken();
112
    }
113
114
    /**
115
     * Return the HTTP status code for this response.
116
     *
117
     * @return int
118
     */
119
    public function getHttpStatusCode()
120
    {
121
        return $this->httpStatusCode;
122
    }
123
124
    /**
125
     * Return the HTTP headers for this response.
126
     *
127
     * @return array
128
     */
129
    public function getHeaders()
130
    {
131
        return $this->headers;
132
    }
133
134
    /**
135
     * Return the raw body response.
136
     *
137
     * @return string
138
     */
139
    public function getBody()
140
    {
141
        return $this->body;
142
    }
143
144
    /**
145
     * Return the decoded body response.
146
     *
147
     * @return array
148
     */
149
    public function getDecodedBody()
150
    {
151
        return $this->decodedBody;
152
    }
153
154
    /**
155
     * Get the app secret proof that was used for this response.
156
     *
157
     * @return string|null
158
     */
159
    public function getAppSecretProof()
160
    {
161
        return $this->request->getAppSecretProof();
162
    }
163
164
    /**
165
     * Get the ETag associated with the response.
166
     *
167
     * @return string|null
168
     */
169
    public function getETag()
170
    {
171
        return isset($this->headers['ETag']) ? $this->headers['ETag'] : null;
172
    }
173
174
    /**
175
     * Get the version of Graph that returned this response.
176
     *
177
     * @return string|null
178
     */
179
    public function getGraphVersion()
180
    {
181
        return isset($this->headers['Facebook-API-Version']) ? $this->headers['Facebook-API-Version'] : null;
182
    }
183
184
    /**
185
     * Returns true if Graph returned an error message.
186
     *
187
     * @return boolean
188
     */
189
    public function isError()
190
    {
191
        return isset($this->decodedBody['error']);
192
    }
193
194
    /**
195
     * Throws the exception.
196
     *
197
     * @throws SDKException
198
     */
199
    public function throwException()
200
    {
201
        throw $this->thrownException;
202
    }
203
204
    /**
205
     * Instantiates an exception to be thrown later.
206
     */
207
    public function makeException()
208
    {
209
        $this->thrownException = ResponseException::create($this);
210
    }
211
212
    /**
213
     * Returns the exception that was thrown for this request.
214
     *
215
     * @return ResponseException|null
216
     */
217
    public function getThrownException()
218
    {
219
        return $this->thrownException;
220
    }
221
222
    /**
223
     * Convert the raw response into an array if possible.
224
     *
225
     * Graph will return 2 types of responses:
226
     * - JSON(P)
227
     *    Most responses from Graph are JSON(P)
228
     * - application/x-www-form-urlencoded key/value pairs
229
     *    Happens on the `/oauth/access_token` endpoint when exchanging
230
     *    a short-lived access token for a long-lived access token
231
     * - And sometimes nothing :/ but that'd be a bug.
232
     */
233
    public function decodeBody()
234
    {
235
        $this->decodedBody = json_decode($this->body, true);
236
237
        if ($this->decodedBody === null) {
238
            $this->decodedBody = [];
239
            parse_str($this->body, $this->decodedBody);
240
        } elseif (is_bool($this->decodedBody)) {
241
            // Backwards compatibility for Graph < 2.1.
242
            // Mimics 2.1 responses.
243
            // @TODO Remove this after Graph 2.0 is no longer supported
244
            $this->decodedBody = ['success' => $this->decodedBody];
245
        } elseif (is_numeric($this->decodedBody)) {
246
            $this->decodedBody = ['id' => $this->decodedBody];
247
        }
248
249
        if (!is_array($this->decodedBody)) {
250
            $this->decodedBody = [];
251
        }
252
253
        if ($this->isError()) {
254
            $this->makeException();
255
        }
256
    }
257
258
    /**
259
     * Instantiate a new GraphObject from response.
260
     *
261
     * @param string|null $subclassName The GraphNode subclass to cast to.
262
     *
263
     * @return \Facebook\GraphNode\GraphObject
264
     *
265
     * @throws SDKException
266
     *
267
     * @deprecated 5.0.0 getGraphObject() has been renamed to getGraphNode()
268
     * @todo v6: Remove this method
269
     */
270
    public function getGraphObject($subclassName = null)
271
    {
272
        return $this->getGraphNode($subclassName);
273
    }
274
275
    /**
276
     * Instantiate a new GraphNode from response.
277
     *
278
     * @param string|null $subclassName The GraphNode subclass to cast to.
279
     *
280
     * @return \Facebook\GraphNode\GraphNode
281
     *
282
     * @throws SDKException
283
     */
284
    public function getGraphNode($subclassName = null)
285
    {
286
        $factory = new GraphNodeFactory($this);
287
288
        return $factory->makeGraphNode($subclassName);
289
    }
290
291
    /**
292
     * Convenience method for creating a GraphAlbum collection.
293
     *
294
     * @return \Facebook\GraphNode\GraphAlbum
295
     *
296
     * @throws SDKException
297
     */
298
    public function getGraphAlbum()
299
    {
300
        $factory = new GraphNodeFactory($this);
301
302
        return $factory->makeGraphAlbum();
303
    }
304
305
    /**
306
     * Convenience method for creating a GraphPage collection.
307
     *
308
     * @return \Facebook\GraphNode\GraphPage
309
     *
310
     * @throws SDKException
311
     */
312
    public function getGraphPage()
313
    {
314
        $factory = new GraphNodeFactory($this);
315
316
        return $factory->makeGraphPage();
317
    }
318
319
    /**
320
     * Convenience method for creating a GraphSessionInfo collection.
321
     *
322
     * @return \Facebook\GraphNode\GraphSessionInfo
323
     *
324
     * @throws SDKException
325
     */
326
    public function getGraphSessionInfo()
327
    {
328
        $factory = new GraphNodeFactory($this);
329
330
        return $factory->makeGraphSessionInfo();
331
    }
332
333
    /**
334
     * Convenience method for creating a GraphUser collection.
335
     *
336
     * @return \Facebook\GraphNode\GraphUser
337
     *
338
     * @throws SDKException
339
     */
340
    public function getGraphUser()
341
    {
342
        $factory = new GraphNodeFactory($this);
343
344
        return $factory->makeGraphUser();
345
    }
346
347
    /**
348
     * Convenience method for creating a GraphEvent collection.
349
     *
350
     * @return \Facebook\GraphNode\GraphEvent
351
     *
352
     * @throws SDKException
353
     */
354
    public function getGraphEvent()
355
    {
356
        $factory = new GraphNodeFactory($this);
357
358
        return $factory->makeGraphEvent();
359
    }
360
361
    /**
362
     * Convenience method for creating a GraphGroup collection.
363
     *
364
     * @return \Facebook\GraphNode\GraphGroup
365
     *
366
     * @throws SDKException
367
     */
368
    public function getGraphGroup()
369
    {
370
        $factory = new GraphNodeFactory($this);
371
372
        return $factory->makeGraphGroup();
373
    }
374
375
    /**
376
     * Instantiate a new GraphList from response.
377
     *
378
     * @param string|null $subclassName The GraphNode subclass to cast list items to.
379
     * @param boolean     $auto_prefix  Toggle to auto-prefix the subclass name.
380
     *
381
     * @return \Facebook\GraphNode\GraphList
382
     *
383
     * @throws SDKException
384
     *
385
     * @deprecated 5.0.0 getGraphList() has been renamed to getGraphEdge()
386
     * @todo v6: Remove this method
387
     */
388
    public function getGraphList($subclassName = null, $auto_prefix = true)
389
    {
390
        return $this->getGraphEdge($subclassName, $auto_prefix);
391
    }
392
393
    /**
394
     * Instantiate a new GraphEdge from response.
395
     *
396
     * @param string|null $subclassName The GraphNode subclass to cast list items to.
397
     * @param boolean     $auto_prefix  Toggle to auto-prefix the subclass name.
398
     *
399
     * @return \Facebook\GraphNode\GraphEdge
400
     *
401
     * @throws SDKException
402
     */
403
    public function getGraphEdge($subclassName = null, $auto_prefix = true)
404
    {
405
        $factory = new GraphNodeFactory($this);
406
407
        return $factory->makeGraphEdge($subclassName, $auto_prefix);
408
    }
409
}
410