Total Complexity | 46 |
Total Lines | 244 |
Duplicated Lines | 0 % |
Changes | 32 | ||
Bugs | 0 | Features | 0 |
Complex classes like Api 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 Api, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Api implements ApiInterface { |
||
28 | |||
29 | protected $_api_key_var = 'Authorization: Bearer '; |
||
30 | protected $_api_key; |
||
31 | protected $_timeout = 30; |
||
32 | protected $_verify_ssl = true; |
||
33 | protected $_verify_host = 2; |
||
34 | protected $curlErrno = false; |
||
35 | protected $curlError = false; |
||
36 | protected $curlProxy = false; |
||
37 | protected $response; |
||
38 | protected $request = array(); |
||
39 | protected $json = true; |
||
40 | protected $accept; |
||
41 | protected $curlInfo; |
||
42 | protected $_curl_callback; |
||
43 | |||
44 | public function __construct($api_key = "", $basic = "") |
||
45 | { |
||
46 | if (!function_exists('curl_init')) |
||
47 | { |
||
48 | throw new CurlException("cURL is not available. This API wrapper cannot be used."); |
||
49 | } |
||
50 | |||
51 | if (isset($api_key)) |
||
52 | { |
||
53 | $this->setApiKey($api_key); |
||
54 | } |
||
55 | |||
56 | if ($basic === true) |
||
57 | { |
||
58 | $this->_api_key_var = 'Authorization: Basic '; |
||
59 | $this->json = false; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public function setOAuth() |
||
64 | { |
||
65 | $this->_api_key_var = ''; |
||
66 | $this->json = false; |
||
67 | $this->accept = true; |
||
68 | } |
||
69 | |||
70 | public function enableJson() |
||
71 | { |
||
72 | $this->_api_key_var = 'Authorization: Bearer '; |
||
73 | $this->json = true; |
||
74 | } |
||
75 | |||
76 | public function setApiKey($api_key) |
||
77 | { |
||
78 | $this->_api_key = $api_key; |
||
79 | } |
||
80 | |||
81 | public function enableSslVerification() |
||
85 | } |
||
86 | |||
87 | public function disableSslVerification() |
||
88 | { |
||
89 | $this->_verify_ssl = false; |
||
90 | $this->_verify_host = 0; |
||
91 | } |
||
92 | |||
93 | public function setTimeout($timeout) |
||
94 | { |
||
95 | $this->_timeout = $timeout; |
||
96 | } |
||
97 | |||
98 | public function setCurlProxy($proxy) |
||
99 | { |
||
100 | $this->curlProxy = $proxy; |
||
101 | } |
||
102 | |||
103 | public function setCurlCallback($callback) |
||
106 | } |
||
107 | |||
108 | private function _call($url, $params = null, $headers = null, $method = "GET") |
||
109 | { |
||
110 | $ch = curl_init(); |
||
111 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url); |
||
112 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_FOLLOWLOCATION, false); |
||
113 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_TIMEOUT, $this->_timeout); |
||
114 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true); |
||
115 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_SSL_VERIFYPEER, $this->_verify_ssl); |
||
116 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_SSL_VERIFYHOST, $this->_verify_host); |
||
117 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HEADER, true); |
||
118 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLINFO_HEADER_OUT, true); |
||
119 | if ($this->curlProxy) { |
||
120 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_PROXY, $this->curlProxy); |
||
121 | } |
||
122 | if ($this->_curl_callback) { |
||
123 | call_user_func($this->_curl_callback, $ch, $params, $headers, $method); |
||
124 | } |
||
125 | switch (strtoupper($method)) { |
||
126 | case 'PATCH': |
||
127 | if (!empty($params)) { |
||
128 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
||
129 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_POSTFIELDS, json_encode($params)); |
||
130 | } |
||
131 | break; |
||
132 | case 'DELETE': |
||
133 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
||
134 | break; |
||
135 | case 'POST': |
||
136 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_POST, true); |
||
137 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_POSTFIELDS, $params); |
||
138 | break; |
||
139 | case 'GET': |
||
140 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HTTPGET, true); |
||
141 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url); |
||
142 | break; |
||
143 | } |
||
144 | |||
145 | curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HTTPHEADER, $this->_createHeaders($headers)); |
||
146 | |||
147 | $this->request['method'] = strtoupper($method); |
||
148 | $this->request['headers'] = $this->_createHeaders($headers); |
||
149 | $this->request['params'] = $params; |
||
150 | |||
151 | $this->response = curl_exec(/** @scrutinizer ignore-type */ $ch); |
||
152 | |||
153 | if (curl_errno(/** @scrutinizer ignore-type */ $ch)) { |
||
154 | $this->curlErrno = curl_errno(/** @scrutinizer ignore-type */ $ch); |
||
155 | $this->curlError = curl_error(/** @scrutinizer ignore-type */ $ch); |
||
156 | curl_close(/** @scrutinizer ignore-type */ $ch); |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | $this->curlInfo = curl_getinfo(/** @scrutinizer ignore-type */ $ch); |
||
161 | curl_close(/** @scrutinizer ignore-type */ $ch); |
||
162 | return new Result($this->_getBody(), $this->_getHeaders(), $this->curlInfo); |
||
163 | } |
||
164 | |||
165 | private function _createHeaders($headers = null) |
||
166 | { |
||
167 | $headers[] = $this->_api_key_var.$this->_api_key; |
||
168 | if ($this->json === true) { |
||
169 | $headers[] = 'Content-Type: application/json'; |
||
170 | } |
||
171 | if ($this->accept === true) { |
||
172 | $headers[] = 'Accept: application/json'; |
||
173 | } |
||
174 | return $headers; |
||
175 | } |
||
176 | |||
177 | private function _parseHeaders($raw_headers) |
||
178 | { |
||
179 | if (!function_exists('http_parse_headers')) { |
||
180 | $headers = array(); |
||
181 | $key = ''; |
||
182 | foreach (explode("\n", $raw_headers) as $i => $h) { |
||
183 | $h = explode(':', $h, 2); |
||
184 | if (isset($h[1])) { |
||
185 | if (!isset($headers[$h[0]])) { |
||
186 | $headers[$h[0]] = trim($h[1]); |
||
187 | } elseif (is_array($headers[$h[0]])) { |
||
188 | $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); |
||
189 | } else { |
||
190 | $headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); |
||
191 | } |
||
192 | $key = $h[0]; |
||
193 | } else { |
||
194 | if (substr($h[0], 0, 1) == "\t") { |
||
195 | $headers[$key] .= "\r\n\t".trim($h[0]); |
||
196 | } elseif (!$key) { |
||
197 | $headers[0] = trim($h[0]); |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | return $headers; |
||
202 | } else { |
||
203 | return http_parse_headers($raw_headers); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | private function _getHeaders() |
||
208 | { |
||
209 | return $this->_parseHeaders(substr($this->response, 0, $this->curlInfo['header_size'])); |
||
210 | } |
||
211 | |||
212 | private function _getBody() |
||
213 | { |
||
214 | return substr($this->response, $this->curlInfo['header_size']); |
||
215 | } |
||
216 | |||
217 | public function getResponse() |
||
218 | { |
||
219 | return $this->response; |
||
220 | } |
||
221 | |||
222 | public function getRequest() |
||
223 | { |
||
224 | return $this->request; |
||
225 | } |
||
226 | |||
227 | public function get($url, $params = null, $headers = null) |
||
228 | { |
||
229 | if (!empty($params)) { |
||
230 | $url .= '?'.http_build_query($params); |
||
231 | } |
||
232 | return $this->_call($url, $params, $headers, $method = "GET"); |
||
233 | } |
||
234 | |||
235 | public function post($url, $params = null, $headers = null) |
||
236 | { |
||
237 | if ($this->json === true) { |
||
238 | $params = json_encode($params); |
||
239 | } |
||
240 | return $this->_call($url, $params, $headers, $method = "POST"); |
||
241 | } |
||
242 | |||
243 | public function delete($url, $params = null, $headers = null) |
||
244 | { |
||
245 | return $this->_call($url, $params, $headers, $method = "DELETE"); |
||
246 | } |
||
247 | |||
248 | public function patch($url, $params = null, $headers = null) |
||
251 | } |
||
252 | |||
253 | public function getCurlInfo() |
||
254 | { |
||
255 | return $this->curlInfo; |
||
256 | } |
||
257 | |||
258 | public function isCurlError() |
||
259 | { |
||
260 | return (bool) $this->curlErrno; |
||
261 | } |
||
262 | |||
263 | public function getCurlErrno() |
||
266 | } |
||
267 | |||
268 | public function getCurlError() |
||
274 |