Complex classes like Requestor 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 Requestor, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\Stripe\Http; |
||
| 19 | class Requestor implements RequestorContract |
||
| 20 | { |
||
| 21 | /* ------------------------------------------------------------------------------------------------ |
||
| 22 | | Properties |
||
| 23 | | ------------------------------------------------------------------------------------------------ |
||
| 24 | */ |
||
| 25 | /** |
||
| 26 | * The API key that's to be used to make requests. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $apiKey; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The API base URL. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $apiBaseUrl; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \Arcanedev\Stripe\Contracts\Http\Curl\HttpClient |
||
| 41 | */ |
||
| 42 | private static $httpClient; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The allowed HTTP methods. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private static $allowedMethods = ['get', 'post', 'delete']; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Arcanedev\Stripe\Utilities\ErrorsHandler |
||
| 53 | */ |
||
| 54 | private $errorsHandler; |
||
| 55 | |||
| 56 | /* ------------------------------------------------------------------------------------------------ |
||
| 57 | | Getters & Setters |
||
| 58 | | ------------------------------------------------------------------------------------------------ |
||
| 59 | */ |
||
| 60 | /** |
||
| 61 | * Create Requestor instance. |
||
| 62 | * |
||
| 63 | * @param string|null $apiKey |
||
| 64 | * @param string|null $apiBase |
||
| 65 | */ |
||
| 66 | public function __construct($apiKey = null, $apiBase = null) |
||
| 67 | 355 | { |
|
| 68 | $this->setApiKey($apiKey); |
||
| 69 | 355 | $this->setApiBase($apiBase); |
|
| 70 | 355 | $this->errorsHandler = new ErrorsHandler; |
|
| 71 | 355 | } |
|
| 72 | 355 | ||
| 73 | /* ------------------------------------------------------------------------------------------------ |
||
| 74 | | Getters & Setters |
||
| 75 | | ------------------------------------------------------------------------------------------------ |
||
| 76 | */ |
||
| 77 | /** |
||
| 78 | * Get Stripe API Key. |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function getApiKey() |
||
| 83 | 346 | { |
|
| 84 | if ( ! $this->apiKey) |
||
| 85 | 346 | $this->setApiKey(Stripe::getApiKey()); |
|
| 86 | 345 | ||
| 87 | return trim($this->apiKey); |
||
| 88 | 346 | } |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Set API Key. |
||
| 92 | * |
||
| 93 | * @param string $apiKey |
||
| 94 | * |
||
| 95 | * @return self |
||
| 96 | */ |
||
| 97 | private function setApiKey($apiKey) |
||
| 98 | 355 | { |
|
| 99 | $this->apiKey = $apiKey; |
||
| 100 | 355 | ||
| 101 | return $this; |
||
| 102 | 355 | } |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Set API Base URL. |
||
| 106 | * |
||
| 107 | * @param string|null $apiBaseUrl |
||
| 108 | * |
||
| 109 | * @return self |
||
| 110 | */ |
||
| 111 | private function setApiBase($apiBaseUrl) |
||
| 112 | 355 | { |
|
| 113 | if (empty($apiBaseUrl)) |
||
| 114 | 355 | $apiBaseUrl = Stripe::getApiBaseUrl(); |
|
| 115 | 183 | ||
| 116 | $this->apiBaseUrl = $apiBaseUrl; |
||
| 117 | 355 | ||
| 118 | return $this; |
||
| 119 | 355 | } |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get the HTTP client. |
||
| 123 | * |
||
| 124 | * @return \Arcanedev\Stripe\Contracts\Http\Curl\HttpClient |
||
| 125 | */ |
||
| 126 | private function httpClient() |
||
| 127 | 346 | { |
|
| 128 | // @codeCoverageIgnoreStart |
||
| 129 | if ( ! self::$httpClient) |
||
| 130 | self::$httpClient = HttpClient::instance(); |
||
| 131 | // @codeCoverageIgnoreEnd |
||
| 132 | |||
| 133 | return self::$httpClient; |
||
| 134 | 346 | } |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Set the HTTP client. |
||
| 138 | * |
||
| 139 | * @param \Arcanedev\Stripe\Contracts\Http\Curl\HttpClient $client |
||
| 140 | */ |
||
| 141 | public static function setHttpClient(HttpClientContract $client) |
||
| 142 | 605 | { |
|
| 143 | self::$httpClient = $client; |
||
| 144 | 605 | } |
|
| 145 | 605 | ||
| 146 | /* ------------------------------------------------------------------------------------------------ |
||
| 147 | | Request Functions |
||
| 148 | | ------------------------------------------------------------------------------------------------ |
||
| 149 | */ |
||
| 150 | /** |
||
| 151 | * Make Requestor instance. |
||
| 152 | * |
||
| 153 | * @param string|null $apiKey |
||
| 154 | * @param string $apiBase |
||
| 155 | * |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | public static function make($apiKey = null, $apiBase = '') |
||
| 159 | 344 | { |
|
| 160 | return new self($apiKey, $apiBase); |
||
| 161 | 344 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * GET Request. |
||
| 165 | * |
||
| 166 | * @param string $url |
||
| 167 | * @param array|null $params |
||
| 168 | * @param array|null $headers |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function get($url, $params = [], $headers = null) |
||
| 173 | 162 | { |
|
| 174 | return $this->request('get', $url, $params, $headers); |
||
| 175 | 162 | } |
|
| 176 | |||
| 177 | /** |
||
| 178 | * POST Request. |
||
| 179 | * |
||
| 180 | * @param string $url |
||
| 181 | * @param array|null $params |
||
| 182 | * @param array|null $headers |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function post($url, $params = [], $headers = null) |
||
| 187 | 18 | { |
|
| 188 | return $this->request('post', $url, $params, $headers); |
||
| 189 | 18 | } |
|
| 190 | |||
| 191 | /** |
||
| 192 | * DELETE Request. |
||
| 193 | * |
||
| 194 | * @param string $url |
||
| 195 | * @param array|null $params |
||
| 196 | * @param array|null $headers |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function delete($url, $params = [], $headers = null) |
||
| 201 | { |
||
| 202 | return $this->request('delete', $url, $params, $headers); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Make a request. |
||
| 207 | * Note: An array whose first element is the Response object and second element is the API key used to make the request. |
||
| 208 | * |
||
| 209 | * @param string $method |
||
| 210 | * @param string $url |
||
| 211 | * @param array|null $params |
||
| 212 | * @param array|null $headers |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function request($method, $url, $params = null, $headers = null) |
||
| 217 | 344 | { |
|
| 218 | if (is_null($params)) $params = []; |
||
| 219 | 344 | if (is_null($headers)) $headers = []; |
|
| 220 | 344 | ||
| 221 | list($respBody, $respCode, $respHeaders, $apiKey) = $this->requestRaw($method, $url, $params, $headers); |
||
| 222 | 344 | ||
| 223 | $json = $this->interpretResponse($respBody, $respCode, $respHeaders); |
||
| 224 | 344 | $response = new Response($respBody, $respCode, $headers, $json); |
|
| 225 | 328 | ||
| 226 | return [$response, $apiKey]; |
||
| 227 | 328 | } |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Raw request. |
||
| 231 | * |
||
| 232 | * @param string $method |
||
| 233 | * @param string $url |
||
| 234 | * @param array $params |
||
| 235 | * @param array $headers |
||
| 236 | * |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | private function requestRaw($method, $url, $params, $headers) |
||
| 240 | 344 | { |
|
| 241 | $this->checkApiKey(); |
||
| 242 | 344 | $this->checkMethod($method); |
|
| 243 | 344 | ||
| 244 | $this->httpClient()->setApiKey($this->getApiKey()); |
||
| 245 | 344 | ||
| 246 | $params = self::encodeObjects($params); |
||
| 247 | 344 | $hasFile = self::processResourceParams($params); |
|
| 248 | 344 | ||
| 249 | list($respBody, $respCode, $respHeaders) = $this->httpClient() |
||
| 250 | 344 | ->request($method, $this->apiBaseUrl.$url, $params, $headers, $hasFile); |
|
| 251 | 344 | ||
| 252 | return [$respBody, $respCode, $respHeaders, $this->getApiKey()]; |
||
| 253 | 344 | } |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Interpret Response. |
||
| 257 | * |
||
| 258 | * @param string $respBody |
||
| 259 | * @param int $respCode |
||
| 260 | * @param array $respHeaders |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | * |
||
| 264 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 265 | */ |
||
| 266 | private function interpretResponse($respBody, $respCode, $respHeaders) |
||
| 267 | 346 | { |
|
| 268 | $response = json_decode($respBody, true); |
||
| 269 | $jsonError = json_last_error(); |
||
| 270 | 346 | ||
| 271 | if ($response === null && $jsonError !== JSON_ERROR_NONE) { |
||
| 272 | 346 | throw new ApiException( |
|
| 273 | "Invalid response body from API: $respBody ". |
||
| 274 | 174 | "(HTTP response code was $respCode, json_last_error() was $jsonError)", |
|
| 275 | 2 | 500, 'api_error', null, $respBody |
|
| 276 | 2 | ); |
|
| 277 | 2 | } |
|
| 278 | 1 | ||
| 279 | if ($respCode < 200 || $respCode >= 300) { |
||
| 280 | $this->errorsHandler->handle($respBody, $respCode, $respHeaders, $response); |
||
| 281 | 344 | } |
|
| 282 | |||
| 283 | 328 | return $response; |
|
| 284 | } |
||
| 285 | |||
| 286 | /* ------------------------------------------------------------------------------------------------ |
||
| 287 | | Check Functions |
||
| 288 | | ------------------------------------------------------------------------------------------------ |
||
| 289 | */ |
||
| 290 | /** |
||
| 291 | * Check if API Key Exists. |
||
| 292 | * |
||
| 293 | * @throws \Arcanedev\Stripe\Exceptions\ApiKeyNotSetException |
||
| 294 | */ |
||
| 295 | 346 | private function checkApiKey() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Check if the API Key is set. |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 346 | private function isApiKeyExists() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Check Http Method. |
||
| 313 | * |
||
| 314 | * @param string $method |
||
| 315 | * |
||
| 316 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 317 | */ |
||
| 318 | 345 | private function checkMethod(&$method) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Check Resource type is stream. |
||
| 329 | * |
||
| 330 | * @param resource $resource |
||
| 331 | * |
||
| 332 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 333 | */ |
||
| 334 | 4 | private static function checkResourceType($resource) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Check resource MetaData. |
||
| 342 | * |
||
| 343 | * @param array $metaData |
||
| 344 | * |
||
| 345 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 346 | */ |
||
| 347 | 4 | private static function checkResourceMetaData(array $metaData) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Check if param is resource File. |
||
| 355 | * |
||
| 356 | * @param mixed $resource |
||
| 357 | * |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | 284 | private static function checkHasResourceFile($resource) |
|
| 366 | |||
| 367 | /* ------------------------------------------------------------------------------------------------ |
||
| 368 | | Other Functions |
||
| 369 | | ------------------------------------------------------------------------------------------------ |
||
| 370 | */ |
||
| 371 | /** |
||
| 372 | * Process Resource Parameters. |
||
| 373 | * |
||
| 374 | * @param array|string $params |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 344 | private static function processResourceParams(&$params) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Process Resource Parameter. |
||
| 398 | * |
||
| 399 | * @param resource $resource |
||
| 400 | * |
||
| 401 | * @return \CURLFile|string |
||
| 402 | */ |
||
| 403 | 4 | private static function processResourceParam($resource) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Encode Objects. |
||
| 417 | * |
||
| 418 | * @param \Arcanedev\Stripe\StripeResource|bool|array|string $obj |
||
| 419 | * |
||
| 420 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 421 | * |
||
| 422 | * @return array|string |
||
| 423 | */ |
||
| 424 | 346 | private static function encodeObjects($obj) |
|
| 439 | } |
||
| 440 |