| Total Complexity | 45 |
| Total Lines | 191 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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.
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 |
||
| 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() |
||
| 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() |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the response entity used to create the exception. |
||
| 218 | * |
||
| 219 | * @return FacebookResponse |
||
| 220 | */ |
||
| 221 | public function getResponse() |
||
| 226 |