Total Complexity | 41 |
Total Lines | 180 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ResponseException 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 ResponseException, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ResponseException extends SDKException |
||
33 | { |
||
34 | /** |
||
35 | * @var Response The response that threw the exception. |
||
36 | */ |
||
37 | protected $response; |
||
38 | |||
39 | /** |
||
40 | * @var array Decoded response. |
||
41 | */ |
||
42 | protected $responseData; |
||
43 | |||
44 | /** |
||
45 | * Creates a FacebookResponseException. |
||
46 | * |
||
47 | * @param Response $response The response that threw the exception. |
||
48 | * @param SDKException $previousException The more detailed exception. |
||
49 | */ |
||
50 | public function __construct(Response $response, SDKException $previousException = null) |
||
51 | { |
||
52 | $this->response = $response; |
||
53 | $this->responseData = $response->getDecodedBody(); |
||
54 | |||
55 | $errorMessage = $this->get('message', 'Unknown error from Graph.'); |
||
56 | $errorCode = $this->get('code', -1); |
||
57 | |||
58 | parent::__construct($errorMessage, $errorCode, $previousException); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * A factory for creating the appropriate exception based on the response from Graph. |
||
63 | * |
||
64 | * @param Response $response The response that threw the exception. |
||
65 | * |
||
66 | * @return ResponseException |
||
67 | */ |
||
68 | public static function create(Response $response) |
||
69 | { |
||
70 | $data = $response->getDecodedBody(); |
||
71 | |||
72 | if (!isset($data['error']['code']) && isset($data['code'])) { |
||
73 | $data = ['error' => $data]; |
||
74 | } |
||
75 | |||
76 | $code = isset($data['error']['code']) ? $data['error']['code'] : null; |
||
77 | $message = isset($data['error']['message']) ? $data['error']['message'] : 'Unknown error from Graph.'; |
||
78 | |||
79 | if (isset($data['error']['error_subcode'])) { |
||
80 | switch ($data['error']['error_subcode']) { |
||
81 | // Other authentication issues |
||
82 | case 458: |
||
83 | case 459: |
||
84 | case 460: |
||
85 | case 463: |
||
86 | case 464: |
||
87 | case 467: |
||
88 | return new static($response, new AuthenticationException($message, $code)); |
||
89 | // Video upload resumable error |
||
90 | case 1363030: |
||
91 | case 1363019: |
||
92 | case 1363037: |
||
93 | case 1363033: |
||
94 | case 1363021: |
||
95 | case 1363041: |
||
96 | return new static($response, new ResumableUploadException($message, $code)); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | switch ($code) { |
||
101 | // Login status or token expired, revoked, or invalid |
||
102 | case 100: |
||
103 | case 102: |
||
104 | case 190: |
||
105 | return new static($response, new AuthenticationException($message, $code)); |
||
106 | |||
107 | // Server issue, possible downtime |
||
108 | case 1: |
||
109 | case 2: |
||
110 | return new static($response, new ServerException($message, $code)); |
||
111 | |||
112 | // API Throttling |
||
113 | case 4: |
||
114 | case 17: |
||
115 | case 341: |
||
116 | return new static($response, new ThrottleException($message, $code)); |
||
117 | |||
118 | // Duplicate Post |
||
119 | case 506: |
||
120 | return new static($response, new ClientException($message, $code)); |
||
121 | } |
||
122 | |||
123 | // Missing Permissions |
||
124 | if ($code == 10 || ($code >= 200 && $code <= 299)) { |
||
125 | return new static($response, new AuthorizationException($message, $code)); |
||
126 | } |
||
127 | |||
128 | // OAuth authentication error |
||
129 | if (isset($data['error']['type']) && $data['error']['type'] === 'OAuthException') { |
||
130 | return new static($response, new AuthenticationException($message, $code)); |
||
131 | } |
||
132 | |||
133 | // All others |
||
134 | 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 | private function get($key, $default = null) |
||
146 | { |
||
147 | if (isset($this->responseData['error'][$key])) { |
||
148 | 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 | public function getHttpStatusCode() |
||
160 | { |
||
161 | return $this->response->getHttpStatusCode(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Returns the sub-error code |
||
166 | * |
||
167 | * @return int |
||
168 | */ |
||
169 | public function getSubErrorCode() |
||
170 | { |
||
171 | return $this->get('error_subcode', -1); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns the error type |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getErrorType() |
||
180 | { |
||
181 | return $this->get('type', ''); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Returns the raw response used to create the exception. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getRawResponse() |
||
190 | { |
||
191 | 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() |
||
212 | } |
||
213 | } |
||
214 |