| Total Complexity | 55 |
| Total Lines | 491 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 161 | class Client |
||
| 162 | { |
||
| 163 | const TOO_MANY_REQUESTS_HTTP_CODE = 429; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var string |
||
| 167 | */ |
||
| 168 | protected $host; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var array |
||
| 172 | */ |
||
| 173 | protected $headers; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var string |
||
| 177 | */ |
||
| 178 | protected $version; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | protected $path; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var array |
||
| 187 | */ |
||
| 188 | protected $curlOptions; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var bool |
||
| 192 | */ |
||
| 193 | protected $isConcurrentRequest; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var array |
||
| 197 | */ |
||
| 198 | protected $savedRequests; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @var bool |
||
| 202 | */ |
||
| 203 | protected $verifySSLCerts; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @var bool |
||
| 207 | */ |
||
| 208 | protected $retryOnLimit; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Supported HTTP verbs. |
||
| 212 | * |
||
| 213 | * @var array |
||
| 214 | */ |
||
| 215 | private $methods = ['get', 'post', 'patch', 'put', 'delete']; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Initialize the client. |
||
| 219 | * |
||
| 220 | * @param string $host the base url (e.g. https://api.sendgrid.com) |
||
| 221 | * @param array $headers global request headers |
||
| 222 | * @param string $version api version (configurable) - this is specific to the SendGrid API |
||
| 223 | * @param array $path holds the segments of the url path |
||
| 224 | * @param array $curlOptions extra options to set during curl initialization |
||
| 225 | * @param bool $retryOnLimit set default retry on limit flag |
||
| 226 | * @param bool $verifySSLCerts set default verify certificates flag |
||
| 227 | */ |
||
| 228 | public function __construct( |
||
| 229 | $host, |
||
| 230 | $headers = null, |
||
| 231 | $version = null, |
||
| 232 | $path = null, |
||
| 233 | $curlOptions = null, |
||
| 234 | $retryOnLimit = false, |
||
| 235 | $verifySSLCerts = true |
||
| 236 | ) { |
||
| 237 | $this->host = $host; |
||
| 238 | $this->headers = $headers ?: []; |
||
| 239 | $this->version = $version; |
||
| 240 | $this->path = $path ?: []; |
||
| 241 | $this->curlOptions = $curlOptions ?: []; |
||
| 242 | $this->retryOnLimit = $retryOnLimit; |
||
| 243 | $this->verifySSLCerts = $verifySSLCerts; |
||
| 244 | $this->isConcurrentRequest = false; |
||
| 245 | $this->savedRequests = []; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getHost() |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | public function getHeaders() |
||
| 260 | { |
||
| 261 | return $this->headers; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string|null |
||
| 266 | */ |
||
| 267 | public function getVersion() |
||
| 268 | { |
||
| 269 | return $this->version; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getPath() |
||
| 276 | { |
||
| 277 | return $this->path; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | public function getCurlOptions() |
||
| 284 | { |
||
| 285 | return $this->curlOptions; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set extra options to set during curl initialization. |
||
| 290 | * |
||
| 291 | * @param array $options |
||
| 292 | * |
||
| 293 | * @return Client |
||
| 294 | */ |
||
| 295 | public function setCurlOptions(array $options) |
||
| 296 | { |
||
| 297 | $this->curlOptions = $options; |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Set default retry on limit flag. |
||
| 304 | * |
||
| 305 | * @param bool $retry |
||
| 306 | * |
||
| 307 | * @return Client |
||
| 308 | */ |
||
| 309 | public function setRetryOnLimit($retry) |
||
| 310 | { |
||
| 311 | $this->retryOnLimit = $retry; |
||
| 312 | |||
| 313 | return $this; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Set default verify certificates flag |
||
| 318 | * |
||
| 319 | * @param bool $verifySSLCerts |
||
| 320 | * |
||
| 321 | * @return Client |
||
| 322 | */ |
||
| 323 | public function setVerifySSLCerts($verifySSLCerts) |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Set concurrent request flag |
||
| 332 | * |
||
| 333 | * @param bool $isConcurrent |
||
| 334 | * |
||
| 335 | * @return Client |
||
| 336 | */ |
||
| 337 | public function setIsConcurrentRequest($isConcurrent) |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Build the final URL to be passed. |
||
| 346 | * |
||
| 347 | * @param array $queryParams an array of all the query parameters |
||
| 348 | * |
||
| 349 | * Nested arrays will resolve to multiple instances of the same parameter |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | private function buildUrl($queryParams = null) |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Creates curl options for a request |
||
| 366 | * this function does not mutate any private variables. |
||
| 367 | * |
||
| 368 | * @param string $method |
||
| 369 | * @param array $body |
||
| 370 | * @param array $headers |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | private function createCurlOptions($method, $body = null, $headers = null) |
||
| 375 | { |
||
| 376 | $options = [ |
||
| 377 | CURLOPT_RETURNTRANSFER => true, |
||
| 378 | CURLOPT_HEADER => true, |
||
| 379 | CURLOPT_CUSTOMREQUEST => strtoupper($method), |
||
| 380 | CURLOPT_SSL_VERIFYPEER => $this->verifySSLCerts, |
||
| 381 | CURLOPT_FAILONERROR => false, |
||
| 382 | ] + $this->curlOptions; |
||
| 383 | |||
| 384 | if (isset($headers)) { |
||
| 385 | $headers = array_merge($this->headers, $headers); |
||
| 386 | } else { |
||
| 387 | $headers = $this->headers; |
||
| 388 | } |
||
| 389 | |||
| 390 | if (isset($body)) { |
||
| 391 | $encodedBody = json_encode($body); |
||
| 392 | $options[CURLOPT_POSTFIELDS] = $encodedBody; |
||
| 393 | $headers = array_merge($headers, ['Content-Type: application/json']); |
||
| 394 | } |
||
| 395 | $options[CURLOPT_HTTPHEADER] = $headers; |
||
| 396 | |||
| 397 | if (class_exists('\\Composer\\CaBundle\\CaBundle') && method_exists('\\Composer\\CaBundle\\CaBundle', 'getSystemCaRootBundlePath')) { |
||
| 398 | $caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(); |
||
| 399 | if (is_dir($caPathOrFile) || (is_link($caPathOrFile) && is_dir(readlink($caPathOrFile)))) { |
||
| 400 | $options[CURLOPT_CAPATH] = $caPathOrFile; |
||
| 401 | } else { |
||
| 402 | $options[CURLOPT_CAINFO] = $caPathOrFile; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | return $options; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param array $requestData (method, url, body and headers) |
||
| 411 | * @param bool $retryOnLimit |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | private function createSavedRequest(array $requestData, $retryOnLimit = false) |
||
| 416 | { |
||
| 417 | return array_merge($requestData, ['retryOnLimit' => $retryOnLimit]); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param array $requests |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | private function createCurlMultiHandle(array $requests) |
||
| 426 | { |
||
| 427 | $channels = []; |
||
| 428 | $multiHandle = curl_multi_init(); |
||
| 429 | |||
| 430 | foreach ($requests as $id => $data) { |
||
| 431 | $channels[$id] = curl_init($data['url']); |
||
| 432 | $curlOpts = $this->createCurlOptions($data['method'], $data['body'], $data['headers']); |
||
| 433 | curl_setopt_array($channels[$id], $curlOpts); |
||
| 434 | curl_multi_add_handle($multiHandle, $channels[$id]); |
||
|
|
|||
| 435 | } |
||
| 436 | |||
| 437 | return [$channels, $multiHandle]; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Prepare response object. |
||
| 442 | * |
||
| 443 | * @param resource $channel the curl resource |
||
| 444 | * @param string $content |
||
| 445 | * |
||
| 446 | * @return Response object |
||
| 447 | */ |
||
| 448 | private function parseResponse($channel, $content) |
||
| 449 | { |
||
| 450 | $headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE); |
||
| 451 | $statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE); |
||
| 452 | |||
| 453 | $responseBody = mb_substr($content, $headerSize); |
||
| 454 | |||
| 455 | $responseHeaders = mb_substr($content, 0, $headerSize); |
||
| 456 | $responseHeaders = explode("\n", $responseHeaders); |
||
| 457 | $responseHeaders = array_map('trim', $responseHeaders); |
||
| 458 | |||
| 459 | return new Response($statusCode, $responseBody, $responseHeaders); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Retry request. |
||
| 464 | * |
||
| 465 | * @param array $responseHeaders headers from rate limited response |
||
| 466 | * @param string $method the HTTP verb |
||
| 467 | * @param string $url the final url to call |
||
| 468 | * @param array $body request body |
||
| 469 | * @param array $headers original headers |
||
| 470 | * |
||
| 471 | * @return Response response object |
||
| 472 | * |
||
| 473 | * @throws InvalidRequest |
||
| 474 | */ |
||
| 475 | private function retryRequest(array $responseHeaders, $method, $url, $body, $headers) |
||
| 476 | { |
||
| 477 | $sleepDurations = $responseHeaders['X-Ratelimit-Reset'] - time(); |
||
| 478 | sleep($sleepDurations > 0 ? $sleepDurations : 0); |
||
| 479 | |||
| 480 | return $this->makeRequest($method, $url, $body, $headers, false); |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Make the API call and return the response. |
||
| 485 | * This is separated into it's own function, so we can mock it easily for testing. |
||
| 486 | * |
||
| 487 | * @param string $method the HTTP verb |
||
| 488 | * @param string $url the final url to call |
||
| 489 | * @param array $body request body |
||
| 490 | * @param array $headers any additional request headers |
||
| 491 | * @param bool $retryOnLimit should retry if rate limit is reach? |
||
| 492 | * |
||
| 493 | * @return Response object |
||
| 494 | * |
||
| 495 | * @throws InvalidRequest |
||
| 496 | */ |
||
| 497 | public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = false) |
||
| 498 | { |
||
| 499 | $channel = curl_init($url); |
||
| 500 | |||
| 501 | $options = $this->createCurlOptions($method, $body, $headers); |
||
| 502 | |||
| 503 | curl_setopt_array($channel, $options); |
||
| 504 | $content = curl_exec($channel); |
||
| 505 | |||
| 506 | if ($content === false) { |
||
| 507 | throw new InvalidRequest(curl_error($channel), curl_errno($channel)); |
||
| 508 | } |
||
| 509 | |||
| 510 | $response = $this->parseResponse($channel, $content); |
||
| 511 | |||
| 512 | if ($retryOnLimit && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) { |
||
| 513 | $responseHeaders = $response->headers(true); |
||
| 514 | |||
| 515 | return $this->retryRequest($responseHeaders, $method, $url, $body, $headers); |
||
| 516 | } |
||
| 517 | |||
| 518 | curl_close($channel); |
||
| 519 | |||
| 520 | return $response; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Send all saved requests at once. |
||
| 525 | * |
||
| 526 | * @param array $requests |
||
| 527 | * |
||
| 528 | * @return Response[] |
||
| 529 | * |
||
| 530 | * @throws InvalidRequest |
||
| 531 | */ |
||
| 532 | public function makeAllRequests(array $requests = []) |
||
| 533 | { |
||
| 534 | if (empty($requests)) { |
||
| 535 | $requests = $this->savedRequests; |
||
| 536 | } |
||
| 537 | list($channels, $multiHandle) = $this->createCurlMultiHandle($requests); |
||
| 538 | |||
| 539 | // running all requests |
||
| 540 | $isRunning = null; |
||
| 541 | do { |
||
| 542 | curl_multi_exec($multiHandle, $isRunning); |
||
| 543 | } while ($isRunning); |
||
| 544 | |||
| 545 | // get response and close all handles |
||
| 546 | $retryRequests = []; |
||
| 547 | $responses = []; |
||
| 548 | $sleepDurations = 0; |
||
| 549 | foreach ($channels as $id => $channel) { |
||
| 550 | $content = curl_multi_getcontent($channel); |
||
| 551 | |||
| 552 | if ($content === false) { |
||
| 553 | throw new InvalidRequest(curl_error($channel), curl_errno($channel)); |
||
| 554 | } |
||
| 555 | |||
| 556 | $response = $this->parseResponse($channel, $content); |
||
| 557 | |||
| 558 | if ($requests[$id]['retryOnLimit'] && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) { |
||
| 559 | $headers = $response->headers(true); |
||
| 560 | $sleepDurations = max($sleepDurations, $headers['X-Ratelimit-Reset'] - time()); |
||
| 561 | $requestData = [ |
||
| 562 | 'method' => $requests[$id]['method'], |
||
| 563 | 'url' => $requests[$id]['url'], |
||
| 564 | 'body' => $requests[$id]['body'], |
||
| 565 | 'headers' => $headers, |
||
| 566 | ]; |
||
| 567 | $retryRequests[] = $this->createSavedRequest($requestData, false); |
||
| 568 | } else { |
||
| 569 | $responses[] = $response; |
||
| 570 | } |
||
| 571 | |||
| 572 | curl_multi_remove_handle($multiHandle, $channel); |
||
| 573 | } |
||
| 574 | curl_multi_close($multiHandle); |
||
| 575 | |||
| 576 | // retry requests |
||
| 577 | if (!empty($retryRequests)) { |
||
| 578 | sleep($sleepDurations > 0 ? $sleepDurations : 0); |
||
| 579 | $responses = array_merge($responses, $this->makeAllRequests($retryRequests)); |
||
| 580 | } |
||
| 581 | |||
| 582 | return $responses; |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Add variable values to the url. (e.g. /your/api/{variable_value}/call) |
||
| 587 | * Another example: if you have a PHP reserved word, such as and, in your url, you must use this method. |
||
| 588 | * |
||
| 589 | * @param string $name name of the url segment |
||
| 590 | * |
||
| 591 | * @return Client object |
||
| 592 | */ |
||
| 593 | public function _($name = null) |
||
| 594 | { |
||
| 595 | if (isset($name)) { |
||
| 596 | $this->path[] = $name; |
||
| 597 | } |
||
| 598 | $client = new static($this->host, $this->headers, $this->version, $this->path); |
||
| 599 | $client->setCurlOptions($this->curlOptions); |
||
| 600 | $client->setVerifySSLCerts($this->verifySSLCerts); |
||
| 601 | $client->setRetryOnLimit($this->retryOnLimit); |
||
| 602 | $this->path = []; |
||
| 603 | |||
| 604 | return $client; |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Dynamically add method calls to the url, then call a method. |
||
| 609 | * (e.g. client.name.name.method()). |
||
| 610 | * |
||
| 611 | * @param string $name name of the dynamic method call or HTTP verb |
||
| 612 | * @param array $args parameters passed with the method call |
||
| 613 | * |
||
| 614 | * @return Client|Response|Response[]|null object |
||
| 615 | * |
||
| 616 | * @throws InvalidRequest |
||
| 617 | */ |
||
| 618 | public function __call($name, $args) |
||
| 652 | } |
||
| 653 | } |
||
| 654 |