FacebookResponseException::create()   F
last analyzed

Complexity

Conditions 36
Paths 344

Size

Total Lines 78
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 36
eloc 48
c 1
b 0
f 1
nc 344
nop 1
dl 0
loc 78
rs 1.5333

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Exceptions;
25
26
use Facebook\FacebookResponse;
27
28
/**
29
 * Class FacebookResponseException
30
 *
31
 * @package Facebook
32
 */
33
class FacebookResponseException extends FacebookSDKException
34
{
35
    /**
36
     * @var FacebookResponse The response that threw the exception.
37
     */
38
    protected $response;
39
40
    /**
41
     * @var array Decoded response.
42
     */
43
    protected $responseData;
44
45
    /**
46
     * Creates a FacebookResponseException.
47
     *
48
     * @param FacebookResponse     $response          The response that threw the exception.
49
     * @param FacebookSDKException $previousException The more detailed exception.
50
     */
51
    public function __construct(FacebookResponse $response, FacebookSDKException $previousException = null)
52
    {
53
        $this->response = $response;
54
        $this->responseData = $response->getDecodedBody();
55
56
        $errorMessage = $this->get('message', 'Unknown error from Graph.');
57
        $errorCode = $this->get('code', -1);
58
59
        parent::__construct($errorMessage, $errorCode, $previousException);
60
    }
61
62
    /**
63
     * A factory for creating the appropriate exception based on the response from Graph.
64
     *
65
     * @param FacebookResponse $response The response that threw the exception.
66
     *
67
     * @return FacebookResponseException
68
     */
69
    public static function create(FacebookResponse $response)
70
    {
71
        $data = $response->getDecodedBody();
72
73
        if (!isset($data['error']['code']) && isset($data['code'])) {
74
            $data = ['error' => $data];
75
        }
76
77
        $code = isset($data['error']['code']) ? $data['error']['code'] : null;
78
        $message = isset($data['error']['message']) ? $data['error']['message'] : 'Unknown error from Graph.';
79
80
        if (isset($data['error']['error_subcode'])) {
81
            switch ($data['error']['error_subcode']) {
82
                // Other authentication issues
83
                case 458:
84
                case 459:
85
                case 460:
86
                case 463:
87
                case 464:
88
                case 467:
89
                    return new static($response, new FacebookAuthenticationException($message, $code));
90
                // Video upload resumable error
91
                case 1363030:
92
                case 1363019:
93
                case 1363033:
94
                case 1363021:
95
                case 1363041:
96
                    return new static($response, new FacebookResumableUploadException($message, $code));
97
                case 1363037:
98
                    $previousException = new FacebookResumableUploadException($message, $code);
99
100
                    $startOffset = isset($data['error']['error_data']['start_offset']) ? (int) $data['error']['error_data']['start_offset'] : null;
101
                    $previousException->setStartOffset($startOffset);
102
103
                    $endOffset = isset($data['error']['error_data']['end_offset']) ? (int) $data['error']['error_data']['end_offset'] : null;
104
                    $previousException->setEndOffset($endOffset);
105
106
                    return new static($response, $previousException);
107
            }
108
        }
109
110
        switch ($code) {
111
            // Login status or token expired, revoked, or invalid
112
            case 100:
113
            case 102:
114
            case 190:
115
                return new static($response, new FacebookAuthenticationException($message, $code));
116
117
            // Server issue, possible downtime
118
            case 1:
119
            case 2:
120
                return new static($response, new FacebookServerException($message, $code));
121
122
            // API Throttling
123
            case 4:
124
            case 17:
125
            case 32:
126
            case 341:
127
            case 613:
128
                return new static($response, new FacebookThrottleException($message, $code));
129
130
            // Duplicate Post
131
            case 506:
132
                return new static($response, new FacebookClientException($message, $code));
133
        }
134
135
        // Missing Permissions
136
        if ($code == 10 || ($code >= 200 && $code <= 299)) {
137
            return new static($response, new FacebookAuthorizationException($message, $code));
138
        }
139
140
        // OAuth authentication error
141
        if (isset($data['error']['type']) && $data['error']['type'] === 'OAuthException') {
142
            return new static($response, new FacebookAuthenticationException($message, $code));
143
        }
144
145
        // All others
146
        return new static($response, new FacebookOtherException($message, $code));
147
    }
148
149
    /**
150
     * Checks isset and returns that or a default value.
151
     *
152
     * @param string $key
153
     * @param mixed  $default
154
     *
155
     * @return mixed
156
     */
157
    private function get($key, $default = null)
158
    {
159
        if (isset($this->responseData['error'][$key])) {
160
            return $this->responseData['error'][$key];
161
        }
162
163
        return $default;
164
    }
165
166
    /**
167
     * Returns the HTTP status code
168
     *
169
     * @return int
170
     */
171
    public function getHttpStatusCode()
172
    {
173
        return $this->response->getHttpStatusCode();
174
    }
175
176
    /**
177
     * Returns the sub-error code
178
     *
179
     * @return int
180
     */
181
    public function getSubErrorCode()
182
    {
183
        return $this->get('error_subcode', -1);
184
    }
185
186
    /**
187
     * Returns the error type
188
     *
189
     * @return string
190
     */
191
    public function getErrorType()
192
    {
193
        return $this->get('type', '');
194
    }
195
196
    /**
197
     * Returns the raw response used to create the exception.
198
     *
199
     * @return string
200
     */
201
    public function getRawResponse()
202
    {
203
        return $this->response->getBody();
204
    }
205
206
    /**
207
     * Returns the decoded response used to create the exception.
208
     *
209
     * @return array
210
     */
211
    public function getResponseData()
212
    {
213
        return $this->responseData;
214
    }
215
216
    /**
217
     * Returns the response entity used to create the exception.
218
     *
219
     * @return FacebookResponse
220
     */
221
    public function getResponse()
222
    {
223
        return $this->response;
224
    }
225
}
226