| Total Complexity | 45 | 
| Total Lines | 191 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like InstagramResponseException 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 InstagramResponseException, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 12 | class InstagramResponseException extends InstagramSDKException  | 
            ||
| 13 | { | 
            ||
| 14 | /**  | 
            ||
| 15 | * @var InstagramResponse The response that threw the exception.  | 
            ||
| 16 | */  | 
            ||
| 17 | protected $response;  | 
            ||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * @var array Decoded response.  | 
            ||
| 21 | */  | 
            ||
| 22 | protected $responseData;  | 
            ||
| 23 | |||
| 24 | /**  | 
            ||
| 25 | * Creates a InstagramResponseException.  | 
            ||
| 26 | *  | 
            ||
| 27 | * @param InstagramResponse $response The response that threw the exception.  | 
            ||
| 28 | * @param InstagramSDKException $previousException The more detailed exception.  | 
            ||
| 29 | */  | 
            ||
| 30 | public function __construct(InstagramResponse $response, InstagramSDKException $previousException = null)  | 
            ||
| 39 | }  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * A factory for creating the appropriate exception based on the response from Graph.  | 
            ||
| 43 | *  | 
            ||
| 44 | * @param InstagramResponse $response The response that threw the exception.  | 
            ||
| 45 | *  | 
            ||
| 46 | * @return InstagramResponseException  | 
            ||
| 47 | */  | 
            ||
| 48 | public static function create(InstagramResponse $response)  | 
            ||
| 49 |     { | 
            ||
| 50 | $data = $response->getDecodedBody();  | 
            ||
| 51 | |||
| 52 |         if (!isset($data['error']['code']) && isset($data['code'])) { | 
            ||
| 53 | $data = ['error' => $data];  | 
            ||
| 54 | }  | 
            ||
| 55 | |||
| 56 | $code = isset($data['error']['code']) ? $data['error']['code'] : null;  | 
            ||
| 57 | $message = isset($data['error']['message']) ? $data['error']['message'] : 'Unknown error from Graph.';  | 
            ||
| 58 | |||
| 59 |         if (isset($data['error']['error_subcode'])) { | 
            ||
| 60 |             switch ($data['error']['error_subcode']) { | 
            ||
| 61 | // Other authentication issues  | 
            ||
| 62 | case 458:  | 
            ||
| 63 | case 459:  | 
            ||
| 64 | case 460:  | 
            ||
| 65 | case 463:  | 
            ||
| 66 | case 464:  | 
            ||
| 67 | case 467:  | 
            ||
| 68 | return new static($response, new InstagramAuthenticationException($message, $code));  | 
            ||
| 69 | // Video upload resumable error  | 
            ||
| 70 | case 1363030:  | 
            ||
| 71 | case 1363019:  | 
            ||
| 72 | case 1363033:  | 
            ||
| 73 | case 1363021:  | 
            ||
| 74 | case 1363041:  | 
            ||
| 75 | return new static($response, new InstagramResumableUploadException($message, $code));  | 
            ||
| 76 | case 1363037:  | 
            ||
| 77 | $previousException = new InstagramResumableUploadException($message, $code);  | 
            ||
| 78 | |||
| 79 | $startOffset = isset($data['error']['error_data']['start_offset']) ? (int) $data['error']['error_data']['start_offset'] : null;  | 
            ||
| 80 | $previousException->setStartOffset($startOffset);  | 
            ||
| 81 | |||
| 82 | $endOffset = isset($data['error']['error_data']['end_offset']) ? (int) $data['error']['error_data']['end_offset'] : null;  | 
            ||
| 83 | $previousException->setEndOffset($endOffset);  | 
            ||
| 84 | |||
| 85 | return new static($response, $previousException);  | 
            ||
| 86 | }  | 
            ||
| 87 | }  | 
            ||
| 88 | |||
| 89 |         switch ($code) { | 
            ||
| 90 | // Login status or token expired, revoked, or invalid  | 
            ||
| 91 | case 100:  | 
            ||
| 92 | case 102:  | 
            ||
| 93 | case 190:  | 
            ||
| 94 | return new static($response, new InstagramAuthenticationException($message, $code));  | 
            ||
| 95 | |||
| 96 | // Server issue, possible downtime  | 
            ||
| 97 | case 1:  | 
            ||
| 98 | case 2:  | 
            ||
| 99 | return new static($response, new InstagramServerException($message, $code));  | 
            ||
| 100 | |||
| 101 | // API Throttling  | 
            ||
| 102 | case 4:  | 
            ||
| 103 | case 17:  | 
            ||
| 104 | case 32:  | 
            ||
| 105 | case 341:  | 
            ||
| 106 | case 613:  | 
            ||
| 107 | return new static($response, new InstagramThrottleException($message, $code));  | 
            ||
| 108 | |||
| 109 | // Duplicate Post  | 
            ||
| 110 | case 506:  | 
            ||
| 111 | return new static($response, new InstagramClientException($message, $code));  | 
            ||
| 112 | }  | 
            ||
| 113 | |||
| 114 | // Missing Permissions  | 
            ||
| 115 |         if ($code == 10 || ($code >= 200 && $code <= 299)) { | 
            ||
| 116 | return new static($response, new InstagramAuthorizationException($message, $code));  | 
            ||
| 117 | }  | 
            ||
| 118 | |||
| 119 | // OAuth authentication error  | 
            ||
| 120 |         if (isset($data['error']['type']) && $data['error']['type'] === 'OAuthException') { | 
            ||
| 121 | return new static($response, new InstagramAuthenticationException($message, $code));  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | // All others  | 
            ||
| 125 | return new static($response, new InstagramOtherException($message, $code));  | 
            ||
| 126 | }  | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Checks isset and returns that or a default value.  | 
            ||
| 130 | *  | 
            ||
| 131 | * @param string $key  | 
            ||
| 132 | * @param mixed $default  | 
            ||
| 133 | *  | 
            ||
| 134 | * @return mixed  | 
            ||
| 135 | */  | 
            ||
| 136 | private function get($key, $default = null)  | 
            ||
| 143 | }  | 
            ||
| 144 | |||
| 145 | /**  | 
            ||
| 146 | * Returns the HTTP status code  | 
            ||
| 147 | *  | 
            ||
| 148 | * @return int  | 
            ||
| 149 | */  | 
            ||
| 150 | public function getHttpStatusCode()  | 
            ||
| 151 |     { | 
            ||
| 152 | return $this->response->getHttpStatusCode();  | 
            ||
| 153 | }  | 
            ||
| 154 | |||
| 155 | /**  | 
            ||
| 156 | * Returns the sub-error code  | 
            ||
| 157 | *  | 
            ||
| 158 | * @return int  | 
            ||
| 159 | */  | 
            ||
| 160 | public function getSubErrorCode()  | 
            ||
| 161 |     { | 
            ||
| 162 |         return $this->get('error_subcode', -1); | 
            ||
| 163 | }  | 
            ||
| 164 | |||
| 165 | /**  | 
            ||
| 166 | * Returns the error type  | 
            ||
| 167 | *  | 
            ||
| 168 | * @return string  | 
            ||
| 169 | */  | 
            ||
| 170 | public function getErrorType()  | 
            ||
| 171 |     { | 
            ||
| 172 |         return $this->get('type', ''); | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Returns the raw response used to create the exception.  | 
            ||
| 177 | *  | 
            ||
| 178 | * @return string  | 
            ||
| 179 | */  | 
            ||
| 180 | public function getRawResponse()  | 
            ||
| 181 |     { | 
            ||
| 182 | return $this->response->getBody();  | 
            ||
| 183 | }  | 
            ||
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * Returns the decoded response used to create the exception.  | 
            ||
| 187 | *  | 
            ||
| 188 | * @return array  | 
            ||
| 189 | */  | 
            ||
| 190 | public function getResponseData()  | 
            ||
| 191 |     { | 
            ||
| 192 | return $this->responseData;  | 
            ||
| 193 | }  | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * Returns the response entity used to create the exception.  | 
            ||
| 197 | *  | 
            ||
| 198 | * @return InstagramResponse  | 
            ||
| 199 | */  | 
            ||
| 200 | public function getResponse()  | 
            ||
| 203 | }  | 
            ||
| 204 | }  | 
            ||
| 205 |