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 RequestorInterface |
||
| 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\HttpClientInterface |
||
| 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 | 718 | public function __construct($apiKey = null, $apiBase = null) |
|
| 72 | |||
| 73 | /* ------------------------------------------------------------------------------------------------ |
||
| 74 | | Getters & Setters |
||
| 75 | | ------------------------------------------------------------------------------------------------ |
||
| 76 | */ |
||
| 77 | /** |
||
| 78 | * Get Stripe API Key. |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | 694 | public function getApiKey() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Set API Key |
||
| 93 | * |
||
| 94 | * @param string $apiKey |
||
| 95 | * |
||
| 96 | * @return self |
||
| 97 | */ |
||
| 98 | 718 | private function setApiKey($apiKey) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Set API Base URL |
||
| 107 | * |
||
| 108 | * @param string|null $apiBaseUrl |
||
| 109 | * |
||
| 110 | * @return self |
||
| 111 | */ |
||
| 112 | 718 | private function setApiBase($apiBaseUrl) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Get the HTTP client |
||
| 125 | * |
||
| 126 | * @return \Arcanedev\Stripe\Contracts\Http\Curl\HttpClientInterface |
||
| 127 | */ |
||
| 128 | 694 | private function httpClient() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Set the HTTP client |
||
| 141 | * |
||
| 142 | * @param \Arcanedev\Stripe\Contracts\Http\Curl\HttpClientInterface $client |
||
| 143 | */ |
||
| 144 | 1299 | public static function setHttpClient(HttpClientInterface $client) |
|
| 148 | |||
| 149 | /* ------------------------------------------------------------------------------------------------ |
||
| 150 | | Request Functions |
||
| 151 | | ------------------------------------------------------------------------------------------------ |
||
| 152 | */ |
||
| 153 | /** |
||
| 154 | * Make Requestor instance. |
||
| 155 | * |
||
| 156 | * @param string|null $apiKey |
||
| 157 | * @param string $apiBase |
||
| 158 | * |
||
| 159 | * @return self |
||
| 160 | */ |
||
| 161 | 689 | public static function make($apiKey = null, $apiBase = '') |
|
| 165 | |||
| 166 | /** |
||
| 167 | * GET Request. |
||
| 168 | * |
||
| 169 | * @param string $url |
||
| 170 | * @param array|null $params |
||
| 171 | * @param array|null $headers |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | 320 | public function get($url, $params = [], $headers = null) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * POST Request. |
||
| 182 | * |
||
| 183 | * @param string $url |
||
| 184 | * @param array|null $params |
||
| 185 | * @param array|null $headers |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 45 | public function post($url, $params = [], $headers = null) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * DELETE Request. |
||
| 196 | * |
||
| 197 | * @param string $url |
||
| 198 | * @param array|null $params |
||
| 199 | * @param array|null $headers |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function delete($url, $params = [], $headers = null) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Make a request. |
||
| 210 | * Note: An array whose first element is the Response object and second element is the API key used to make the request. |
||
| 211 | * |
||
| 212 | * @param string $method |
||
| 213 | * @param string $url |
||
| 214 | * @param array|null $params |
||
| 215 | * @param array|null $headers |
||
| 216 | * |
||
| 217 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | 689 | public function request($method, $url, $params = null, $headers = null) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Raw request. |
||
| 236 | * |
||
| 237 | * @param string $method |
||
| 238 | * @param string $url |
||
| 239 | * @param array $params |
||
| 240 | * @param array $headers |
||
| 241 | * |
||
| 242 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
| 243 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 244 | * @throws \Arcanedev\Stripe\Exceptions\ApiKeyNotSetException |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | 689 | private function requestRaw($method, $url, $params, $headers) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Interpret Response. |
||
| 267 | * |
||
| 268 | * @param string $respBody |
||
| 269 | * @param int $respCode |
||
| 270 | * |
||
| 271 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 272 | * @throws \Arcanedev\Stripe\Exceptions\AuthenticationException |
||
| 273 | * @throws \Arcanedev\Stripe\Exceptions\CardException |
||
| 274 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
| 275 | * @throws \Arcanedev\Stripe\Exceptions\RateLimitException |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | 694 | private function interpretResponse($respBody, $respCode) |
|
| 302 | |||
| 303 | /* ------------------------------------------------------------------------------------------------ |
||
| 304 | | Check Functions |
||
| 305 | | ------------------------------------------------------------------------------------------------ |
||
| 306 | */ |
||
| 307 | /** |
||
| 308 | * Check if API Key Exists. |
||
| 309 | * |
||
| 310 | * @throws \Arcanedev\Stripe\Exceptions\ApiKeyNotSetException |
||
| 311 | */ |
||
| 312 | 694 | private function checkApiKey() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Check if the API Key is set. |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | 694 | private function isApiKeyExists() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Check Http Method. |
||
| 333 | * |
||
| 334 | * @param string $method |
||
| 335 | * |
||
| 336 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 337 | */ |
||
| 338 | 693 | private function checkMethod(&$method) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Check Resource type is stream. |
||
| 352 | * |
||
| 353 | * @param resource $resource |
||
| 354 | * |
||
| 355 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 356 | */ |
||
| 357 | 10 | private static function checkResourceType($resource) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Check resource MetaData. |
||
| 368 | * |
||
| 369 | * @param array $metaData |
||
| 370 | * |
||
| 371 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 372 | */ |
||
| 373 | 10 | private static function checkResourceMetaData(array $metaData) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Check if param is resource File. |
||
| 384 | * |
||
| 385 | * @param mixed $resource |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | 569 | private static function checkHasResourceFile($resource) |
|
| 395 | |||
| 396 | /* ------------------------------------------------------------------------------------------------ |
||
| 397 | | Other Functions |
||
| 398 | | ------------------------------------------------------------------------------------------------ |
||
| 399 | */ |
||
| 400 | /** |
||
| 401 | * Process Resource Parameters. |
||
| 402 | * |
||
| 403 | * @param array|string $params |
||
| 404 | * |
||
| 405 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | 689 | private static function processResourceParams(&$params) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Process Resource Parameter. |
||
| 430 | * |
||
| 431 | * @param resource $resource |
||
| 432 | * |
||
| 433 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 434 | * |
||
| 435 | * @return \CURLFile|string |
||
| 436 | */ |
||
| 437 | 10 | private static function processResourceParam($resource) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Encode Objects. |
||
| 453 | * |
||
| 454 | * @param \Arcanedev\Stripe\StripeResource|bool|array|string $obj |
||
| 455 | * |
||
| 456 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 457 | * |
||
| 458 | * @return array|string |
||
| 459 | */ |
||
| 460 | 694 | private static function encodeObjects($obj) |
|
| 478 | } |
||
| 479 |