Complex classes like HttpClient 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 HttpClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\Stripe\Http\Curl; |
||
| 14 | class HttpClient implements HttpClientInterface |
||
| 15 | { |
||
| 16 | /* ------------------------------------------------------------------------------------------------ |
||
| 17 | | Constants |
||
| 18 | | ------------------------------------------------------------------------------------------------ |
||
| 19 | */ |
||
| 20 | const DEFAULT_TIMEOUT = 80; |
||
| 21 | const DEFAULT_CONNECT_TIMEOUT = 30; |
||
| 22 | |||
| 23 | /* ------------------------------------------------------------------------------------------------ |
||
| 24 | | Properties |
||
| 25 | | ------------------------------------------------------------------------------------------------ |
||
| 26 | */ |
||
| 27 | /** |
||
| 28 | * The HTTP Client instance. |
||
| 29 | * |
||
| 30 | * @var HttpClient |
||
| 31 | */ |
||
| 32 | private static $instance; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $apiKey; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $apiBaseUrl; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var HeaderBag |
||
| 46 | */ |
||
| 47 | private $headers; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var CurlOptions |
||
| 51 | */ |
||
| 52 | private $options; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var resource |
||
| 56 | */ |
||
| 57 | private $curl; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | private $timeout = self::DEFAULT_TIMEOUT; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | private $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var mixed |
||
| 71 | */ |
||
| 72 | private $response; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | private $errorCode; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $errorMessage; |
||
| 83 | |||
| 84 | /* ------------------------------------------------------------------------------------------------ |
||
| 85 | | Constructor & Destructor |
||
| 86 | | ------------------------------------------------------------------------------------------------ |
||
| 87 | */ |
||
| 88 | /** |
||
| 89 | * Create a HttpClient instance. |
||
| 90 | */ |
||
| 91 | 5 | private function __construct() |
|
| 92 | { |
||
| 93 | 5 | $this->headers = new HeaderBag; |
|
| 94 | 5 | $this->options = new CurlOptions; |
|
| 95 | 5 | $this->response = null; |
|
| 96 | 5 | } |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Destroy the instance. |
||
| 100 | */ |
||
| 101 | public function __destruct() |
||
| 105 | |||
| 106 | /* ------------------------------------------------------------------------------------------------ |
||
| 107 | | Getters & Setters |
||
| 108 | | ------------------------------------------------------------------------------------------------ |
||
| 109 | */ |
||
| 110 | /** |
||
| 111 | * Set API Key. |
||
| 112 | * |
||
| 113 | * @param string $apiKey |
||
| 114 | * |
||
| 115 | * @return self |
||
| 116 | */ |
||
| 117 | 619 | public function setApiKey($apiKey) |
|
| 118 | { |
||
| 119 | 619 | $this->apiKey = $apiKey; |
|
| 120 | |||
| 121 | 619 | return $this; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set Base URL. |
||
| 126 | * |
||
| 127 | * @param string $apiBaseUrl |
||
| 128 | * |
||
| 129 | * @return self |
||
| 130 | */ |
||
| 131 | public function setApiBaseUrl($apiBaseUrl) |
||
| 132 | { |
||
| 133 | $this->apiBaseUrl = $apiBaseUrl; |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the timeout. |
||
| 140 | * |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | 5 | public function getTimeout() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Set the timeout. |
||
| 150 | * |
||
| 151 | * @param int $seconds |
||
| 152 | * |
||
| 153 | * @return self |
||
| 154 | */ |
||
| 155 | 1244 | public function setTimeout($seconds) |
|
| 156 | { |
||
| 157 | 1244 | $this->timeout = (int) max($seconds, 0); |
|
| 158 | |||
| 159 | 1244 | return $this; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the connect timeout. |
||
| 164 | * |
||
| 165 | * @return int |
||
| 166 | */ |
||
| 167 | 5 | public function getConnectTimeout() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Set the connect timeout. |
||
| 174 | * |
||
| 175 | * @param int $seconds |
||
| 176 | * |
||
| 177 | * @return self |
||
| 178 | */ |
||
| 179 | 5 | public function setConnectTimeout($seconds) |
|
| 180 | { |
||
| 181 | 5 | $this->connectTimeout = (int) max($seconds, 0); |
|
| 182 | |||
| 183 | 5 | return $this; |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set array options. |
||
| 188 | * |
||
| 189 | * @param array $options |
||
| 190 | * |
||
| 191 | * @return self |
||
| 192 | */ |
||
| 193 | 619 | public function setOptionArray(array $options) |
|
| 194 | { |
||
| 195 | 619 | curl_setopt_array($this->curl, $options); |
|
| 196 | |||
| 197 | 619 | return $this; |
|
| 198 | } |
||
| 199 | |||
| 200 | /* ------------------------------------------------------------------------------------------------ |
||
| 201 | | Curl Functions |
||
| 202 | | ------------------------------------------------------------------------------------------------ |
||
| 203 | */ |
||
| 204 | /** |
||
| 205 | * Init curl. |
||
| 206 | */ |
||
| 207 | 619 | private function init() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Execute curl. |
||
| 214 | */ |
||
| 215 | 619 | private function execute() |
|
| 216 | { |
||
| 217 | 619 | $this->response = curl_exec($this->curl); |
|
| 218 | 619 | $this->errorCode = curl_errno($this->curl); |
|
| 219 | 619 | $this->errorMessage = curl_error($this->curl); |
|
| 220 | 619 | } |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Close curl. |
||
| 224 | */ |
||
| 225 | 619 | private function close() |
|
| 226 | { |
||
| 227 | 619 | if (is_resource($this->curl)) { |
|
| 228 | 619 | curl_close($this->curl); |
|
| 229 | 495 | } |
|
| 230 | 619 | } |
|
| 231 | |||
| 232 | /* ------------------------------------------------------------------------------------------------ |
||
| 233 | | Main Functions |
||
| 234 | | ------------------------------------------------------------------------------------------------ |
||
| 235 | */ |
||
| 236 | /** |
||
| 237 | * Get the HTTP. |
||
| 238 | * |
||
| 239 | * @return self |
||
| 240 | */ |
||
| 241 | 1244 | public static function instance() |
|
| 242 | { |
||
| 243 | 1244 | if ( ! self::$instance) { |
|
| 244 | 5 | self::$instance = new self; |
|
| 245 | 4 | } |
|
| 246 | |||
| 247 | 1244 | return self::$instance; |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Curl the request. |
||
| 252 | * |
||
| 253 | * @param string $method |
||
| 254 | * @param string $url |
||
| 255 | * @param array|string $params |
||
| 256 | * @param array $headers |
||
| 257 | * |
||
| 258 | * @throws ApiConnectionException |
||
| 259 | * @throws ApiException |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | 619 | public function request($method, $url, $params, $headers) |
|
| 264 | { |
||
| 265 | 619 | $hasFile = self::processResourceParams($params); |
|
| 266 | |||
| 267 | 619 | if ($method !== 'post') { |
|
| 268 | 435 | $url = str_parse_url($url, $params); |
|
| 269 | 348 | } |
|
| 270 | else { |
||
| 271 | 529 | $params = $hasFile ? $params : str_url_queries($params); |
|
| 272 | } |
||
| 273 | |||
| 274 | 619 | $this->headers->prepare($this->apiKey, $headers, $hasFile); |
|
| 275 | 619 | $this->options->make($method, $url, $params, $this->headers->get(), $hasFile); |
|
| 276 | 619 | $this->options->setOptions([ |
|
| 277 | 619 | CURLOPT_CONNECTTIMEOUT => $this->connectTimeout, |
|
| 278 | 619 | CURLOPT_TIMEOUT => $this->timeout, |
|
| 279 | 495 | ]); |
|
| 280 | |||
| 281 | 619 | $this->init(); |
|
| 282 | 619 | $this->setOptionArray($this->options->get()); |
|
| 283 | 619 | $this->execute(); |
|
| 284 | |||
| 285 | 619 | $this->checkCertErrors(); |
|
| 286 | 619 | $this->checkResponse(); |
|
| 287 | |||
| 288 | 619 | $statusCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
| 289 | 619 | $this->close(); |
|
| 290 | |||
| 291 | 619 | return [$this->response, $statusCode]; |
|
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Check Cert Errors. |
||
| 296 | */ |
||
| 297 | 619 | private function checkCertErrors() |
|
| 298 | { |
||
| 299 | 619 | if (SslChecker::hasCertErrors($this->errorCode)) { |
|
| 300 | $this->headers->set( |
||
| 301 | 'X-Stripe-Client-Info', |
||
| 302 | '{"ca":"using Stripe-supplied CA bundle"}' |
||
| 303 | ); |
||
| 304 | |||
| 305 | $this->setOptionArray([ |
||
| 306 | CURLOPT_HTTPHEADER => $this->headers->get(), |
||
| 307 | CURLOPT_CAINFO => SslChecker::caBundle() |
||
| 308 | ]); |
||
| 309 | |||
| 310 | $this->execute(); |
||
| 311 | } |
||
| 312 | 619 | } |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Process Resource Parameters. |
||
| 316 | * |
||
| 317 | * @param array|string $params |
||
| 318 | * |
||
| 319 | * @throws ApiException |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 619 | private static function processResourceParams(&$params) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Process Resource Parameter. |
||
| 344 | * |
||
| 345 | * @param resource $resource |
||
| 346 | * |
||
| 347 | * @throws ApiException |
||
| 348 | * |
||
| 349 | * @return CURLFile|string |
||
| 350 | */ |
||
| 351 | 10 | private static function processResourceParam($resource) |
|
| 352 | { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Encode array to query string |
||
| 367 | * |
||
| 368 | * @param array $array |
||
| 369 | * @param string|null $prefix |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | 5 | protected static function encode($array, $prefix = null) |
|
| 403 | |||
| 404 | /* ------------------------------------------------------------------------------------------------ |
||
| 405 | | Check Functions |
||
| 406 | | ------------------------------------------------------------------------------------------------ |
||
| 407 | */ |
||
| 408 | /** |
||
| 409 | * Check Resource type is stream. |
||
| 410 | * |
||
| 411 | * @param resource $resource |
||
| 412 | * |
||
| 413 | * @throws ApiException |
||
| 414 | */ |
||
| 415 | 10 | private static function checkResourceType($resource) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Check resource MetaData. |
||
| 426 | * |
||
| 427 | * @param array $metaData |
||
| 428 | * |
||
| 429 | * @throws ApiException |
||
| 430 | */ |
||
| 431 | 10 | private static function checkResourceMetaData(array $metaData) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Check if param is resource File. |
||
| 442 | * |
||
| 443 | * @param mixed $resource |
||
| 444 | * |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | 524 | private static function checkHasResourceFile($resource) |
|
| 453 | |||
| 454 | /* ------------------------------------------------------------------------------------------------ |
||
| 455 | | Other Functions |
||
| 456 | | ------------------------------------------------------------------------------------------------ |
||
| 457 | */ |
||
| 458 | /** |
||
| 459 | * Check Response. |
||
| 460 | * |
||
| 461 | * @throws ApiConnectionException |
||
| 462 | */ |
||
| 463 | 619 | private function checkResponse() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Handle CURL errors. |
||
| 473 | * |
||
| 474 | * @throws ApiConnectionException |
||
| 475 | */ |
||
| 476 | private function handleCurlError() |
||
| 505 | } |
||
| 506 |