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