FacebookResponseException   B
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 182
rs 8.2769
c 3
b 0
f 1
wmc 41
lcom 2
cbo 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
D create() 0 68 32
A __construct() 0 10 1
A get() 0 8 2
A getHttpStatusCode() 0 4 1
A getSubErrorCode() 0 4 1
A getErrorType() 0 4 1
A getRawResponse() 0 4 1
A getResponseData() 0 4 1
A getResponse() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like FacebookResponseException often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FacebookResponseException, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Copyright 2016 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 1363037:
94
                case 1363033:
95
                case 1363021:
96
                case 1363041:
97
                    return new static($response, new FacebookResumableUploadException($message, $code));
98
            }
99
        }
100
101
        switch ($code) {
102
            // Login status or token expired, revoked, or invalid
103
            case 100:
104
            case 102:
105
            case 190:
106
                return new static($response, new FacebookAuthenticationException($message, $code));
107
108
            // Server issue, possible downtime
109
            case 1:
110
            case 2:
111
                return new static($response, new FacebookServerException($message, $code));
112
113
            // API Throttling
114
            case 4:
115
            case 17:
116
            case 341:
117
                return new static($response, new FacebookThrottleException($message, $code));
118
119
            // Duplicate Post
120
            case 506:
121
                return new static($response, new FacebookClientException($message, $code));
122
        }
123
124
        // Missing Permissions
125
        if ($code == 10 || ($code >= 200 && $code <= 299)) {
126
            return new static($response, new FacebookAuthorizationException($message, $code));
127
        }
128
129
        // OAuth authentication error
130
        if (isset($data['error']['type']) && $data['error']['type'] === 'OAuthException') {
131
            return new static($response, new FacebookAuthenticationException($message, $code));
132
        }
133
134
        // All others
135
        return new static($response, new FacebookOtherException($message, $code));
136
    }
137
138
    /**
139
     * Checks isset and returns that or a default value.
140
     *
141
     * @param string $key
142
     * @param mixed  $default
143
     *
144
     * @return mixed
145
     */
146
    private function get($key, $default = null)
147
    {
148
        if (isset($this->responseData['error'][$key])) {
149
            return $this->responseData['error'][$key];
150
        }
151
152
        return $default;
153
    }
154
155
    /**
156
     * Returns the HTTP status code
157
     *
158
     * @return int
159
     */
160
    public function getHttpStatusCode()
161
    {
162
        return $this->response->getHttpStatusCode();
163
    }
164
165
    /**
166
     * Returns the sub-error code
167
     *
168
     * @return int
169
     */
170
    public function getSubErrorCode()
171
    {
172
        return $this->get('error_subcode', -1);
173
    }
174
175
    /**
176
     * Returns the error type
177
     *
178
     * @return string
179
     */
180
    public function getErrorType()
181
    {
182
        return $this->get('type', '');
183
    }
184
185
    /**
186
     * Returns the raw response used to create the exception.
187
     *
188
     * @return string
189
     */
190
    public function getRawResponse()
191
    {
192
        return $this->response->getBody();
193
    }
194
195
    /**
196
     * Returns the decoded response used to create the exception.
197
     *
198
     * @return array
199
     */
200
    public function getResponseData()
201
    {
202
        return $this->responseData;
203
    }
204
205
    /**
206
     * Returns the response entity used to create the exception.
207
     *
208
     * @return FacebookResponse
209
     */
210
    public function getResponse()
211
    {
212
        return $this->response;
213
    }
214
}
215