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 | /** @var array */ |
||
| 45 | private static $allowedMethods = ['get', 'post', 'delete']; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Arcanedev\Stripe\Utilities\ErrorsHandler |
||
| 49 | */ |
||
| 50 | private $errorsHandler; |
||
| 51 | |||
| 52 | /* ------------------------------------------------------------------------------------------------ |
||
| 53 | | Getters & Setters |
||
| 54 | | ------------------------------------------------------------------------------------------------ |
||
| 55 | */ |
||
| 56 | /** |
||
| 57 | * Create Requestor instance. |
||
| 58 | * |
||
| 59 | * @param string|null $apiKey |
||
| 60 | * @param string|null $apiBase |
||
| 61 | */ |
||
| 62 | 708 | public function __construct($apiKey = null, $apiBase = null) |
|
| 68 | |||
| 69 | /* ------------------------------------------------------------------------------------------------ |
||
| 70 | | Getters & Setters |
||
| 71 | | ------------------------------------------------------------------------------------------------ |
||
| 72 | */ |
||
| 73 | /** |
||
| 74 | * Get Stripe API Key. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 684 | public function getApiKey() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Set API Key |
||
| 89 | * |
||
| 90 | * @param string $apiKey |
||
| 91 | * |
||
| 92 | * @return self |
||
| 93 | */ |
||
| 94 | 708 | private function setApiKey($apiKey) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Set API Base URL |
||
| 103 | * |
||
| 104 | * @param string|null $apiBaseUrl |
||
| 105 | * |
||
| 106 | * @return self |
||
| 107 | */ |
||
| 108 | 708 | private function setApiBase($apiBaseUrl) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Get the HTTP client |
||
| 121 | * |
||
| 122 | * @return \Arcanedev\Stripe\Contracts\Http\Curl\HttpClientInterface |
||
| 123 | */ |
||
| 124 | 684 | private function httpClient() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Set the HTTP client |
||
| 137 | * |
||
| 138 | * @param \Arcanedev\Stripe\Contracts\Http\Curl\HttpClientInterface $client |
||
| 139 | */ |
||
| 140 | 1289 | public static function setHttpClient(HttpClientInterface $client) |
|
| 144 | |||
| 145 | /* ------------------------------------------------------------------------------------------------ |
||
| 146 | | Request Functions |
||
| 147 | | ------------------------------------------------------------------------------------------------ |
||
| 148 | */ |
||
| 149 | /** |
||
| 150 | * Make Requestor instance. |
||
| 151 | * |
||
| 152 | * @param string|null $apiKey |
||
| 153 | * @param string $apiBase |
||
| 154 | * |
||
| 155 | * @return self |
||
| 156 | */ |
||
| 157 | 679 | public static function make($apiKey = null, $apiBase = '') |
|
| 161 | |||
| 162 | /** |
||
| 163 | * GET Request. |
||
| 164 | * |
||
| 165 | * @param string $url |
||
| 166 | * @param array|null $params |
||
| 167 | * @param array|null $headers |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | 295 | public function get($url, $params = [], $headers = null) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * POST Request. |
||
| 178 | * |
||
| 179 | * @param string $url |
||
| 180 | * @param array|null $params |
||
| 181 | * @param array|null $headers |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | 35 | public function post($url, $params = [], $headers = null) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * DELETE Request. |
||
| 192 | * |
||
| 193 | * @param string $url |
||
| 194 | * @param array|null $params |
||
| 195 | * @param array|null $headers |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function delete($url, $params = [], $headers = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Make a request. |
||
| 206 | * Note: An array whose first element is the Response object and second element is the API key used to make the request. |
||
| 207 | * |
||
| 208 | * @param string $method |
||
| 209 | * @param string $url |
||
| 210 | * @param array|null $params |
||
| 211 | * @param array|null $headers |
||
| 212 | * |
||
| 213 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | 679 | public function request($method, $url, $params = null, $headers = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Raw request. |
||
| 232 | * |
||
| 233 | * @param string $method |
||
| 234 | * @param string $url |
||
| 235 | * @param array $params |
||
| 236 | * @param array $headers |
||
| 237 | * |
||
| 238 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
| 239 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 240 | * @throws \Arcanedev\Stripe\Exceptions\ApiKeyNotSetException |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | 679 | private function requestRaw($method, $url, $params, $headers) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Interpret Response. |
||
| 263 | * |
||
| 264 | * @param string $respBody |
||
| 265 | * @param int $respCode |
||
| 266 | * |
||
| 267 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 268 | * @throws \Arcanedev\Stripe\Exceptions\AuthenticationException |
||
| 269 | * @throws \Arcanedev\Stripe\Exceptions\CardException |
||
| 270 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
| 271 | * @throws \Arcanedev\Stripe\Exceptions\RateLimitException |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | 649 | private function interpretResponse($respBody, $respCode) |
|
| 298 | |||
| 299 | /* ------------------------------------------------------------------------------------------------ |
||
| 300 | | Check Functions |
||
| 301 | | ------------------------------------------------------------------------------------------------ |
||
| 302 | */ |
||
| 303 | /** |
||
| 304 | * Check if API Key Exists. |
||
| 305 | * |
||
| 306 | * @throws \Arcanedev\Stripe\Exceptions\ApiKeyNotSetException |
||
| 307 | */ |
||
| 308 | 684 | private function checkApiKey() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Check if the API Key is set. |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | 684 | private function isApiKeyExists() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Check Http Method. |
||
| 329 | * |
||
| 330 | * @param string $method |
||
| 331 | * |
||
| 332 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 333 | */ |
||
| 334 | 683 | private function checkMethod(&$method) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Check Resource type is stream. |
||
| 348 | * |
||
| 349 | * @param resource $resource |
||
| 350 | * |
||
| 351 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 352 | */ |
||
| 353 | 10 | private static function checkResourceType($resource) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Check resource MetaData. |
||
| 364 | * |
||
| 365 | * @param array $metaData |
||
| 366 | * |
||
| 367 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 368 | */ |
||
| 369 | 10 | private static function checkResourceMetaData(array $metaData) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Check if param is resource File. |
||
| 380 | * |
||
| 381 | * @param mixed $resource |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 559 | private static function checkHasResourceFile($resource) |
|
| 391 | |||
| 392 | /* ------------------------------------------------------------------------------------------------ |
||
| 393 | | Other Functions |
||
| 394 | | ------------------------------------------------------------------------------------------------ |
||
| 395 | */ |
||
| 396 | /** |
||
| 397 | * Process Resource Parameters. |
||
| 398 | * |
||
| 399 | * @param array|string $params |
||
| 400 | * |
||
| 401 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | 679 | private static function processResourceParams(&$params) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Process Resource Parameter. |
||
| 426 | * |
||
| 427 | * @param resource $resource |
||
| 428 | * |
||
| 429 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 430 | * |
||
| 431 | * @return \CURLFile|string |
||
| 432 | */ |
||
| 433 | 10 | private static function processResourceParam($resource) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Encode Objects. |
||
| 449 | * |
||
| 450 | * @param \Arcanedev\Stripe\StripeResource|bool|array|string $obj |
||
| 451 | * |
||
| 452 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
| 453 | * |
||
| 454 | * @return array|string |
||
| 455 | */ |
||
| 456 | 684 | private static function encodeObjects($obj) |
|
| 474 | } |
||
| 475 |