Completed
Push — master ( 7113c4...8875d3 )
by Yassine
14s
created

ResponseException::create()   D

Complexity

Conditions 34
Paths 320

Size

Total Lines 69
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 34.0155

Importance

Changes 0
Metric Value
cc 34
eloc 42
nc 320
nop 1
dl 0
loc 69
ccs 41
cts 42
cp 0.9762
crap 34.0155
rs 4.2089
c 0
b 0
f 0

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