| Total Complexity | 71 |
| Total Lines | 656 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 58 | class HttpClient |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * The base URL |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected string $baseUrl; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The request headers |
||
| 68 | * @var array<string, array<int, mixed>> |
||
| 69 | */ |
||
| 70 | protected array $headers = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The request parameters |
||
| 74 | * @var array<string, mixed> |
||
| 75 | */ |
||
| 76 | protected array $parameters = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The request cookies |
||
| 80 | * @var array<string, mixed> |
||
| 81 | */ |
||
| 82 | protected array $cookies = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Indicating the number of seconds to use as a timeout for HTTP requests |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | protected int $timeout = 30; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Indicating if the validity of SSL certificates should be enforced in HTTP requests |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | protected bool $verifySslCertificate = true; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The username to use for basic authentication |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected string $username = ''; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The password to use for basic authentication |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | protected string $password = ''; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Create new instance |
||
| 110 | * @param string $baseUrl |
||
| 111 | */ |
||
| 112 | public function __construct(string $baseUrl = '') |
||
| 113 | { |
||
| 114 | $this->baseUrl = $baseUrl; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set the base URL |
||
| 119 | * @param string $baseUrl |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | public function setBaseUrl(string $baseUrl): self |
||
| 123 | { |
||
| 124 | $this->baseUrl = $baseUrl; |
||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Return the base URL |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getBaseUrl(): string |
||
| 133 | { |
||
| 134 | return $this->baseUrl; |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Add request header |
||
| 140 | * @param string $name |
||
| 141 | * @param mixed $value |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function header(string $name, $value): self |
||
| 145 | { |
||
| 146 | if (array_key_exists($name, $this->headers) === false) { |
||
| 147 | $this->headers[$name] = []; |
||
| 148 | } |
||
| 149 | $this->headers[$name][] = $value; |
||
| 150 | |||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Add multiple request headers |
||
| 156 | * @param array<string, mixed> $headers |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function headers(array $headers): self |
||
| 160 | { |
||
| 161 | foreach ($headers as $name => $value) { |
||
| 162 | $this->header($name, $value); |
||
| 163 | } |
||
| 164 | |||
| 165 | return $this; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Add request query parameter |
||
| 170 | * @param string $name |
||
| 171 | * @param mixed $value |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | public function parameter(string $name, $value): self |
||
| 175 | { |
||
| 176 | $this->parameters[$name] = $value; |
||
| 177 | |||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Add multiple request parameter |
||
| 184 | * @param array<string, mixed> $parameters |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function parameters(array $parameters): self |
||
| 188 | { |
||
| 189 | foreach ($parameters as $name => $value) { |
||
| 190 | $this->parameter($name, $value); |
||
| 191 | } |
||
| 192 | |||
| 193 | return $this; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add request cookie |
||
| 198 | * @param string $name |
||
| 199 | * @param mixed $value |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | public function cookie(string $name, $value): self |
||
| 203 | { |
||
| 204 | $this->cookies[$name] = $value; |
||
| 205 | |||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Add multiple request cookie |
||
| 211 | * @param array<string, mixed> $cookies |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function cookies(?array $cookies = null): self |
||
| 215 | { |
||
| 216 | if ($cookies === null) { |
||
| 217 | $cookies = $_COOKIE; |
||
| 218 | } |
||
| 219 | |||
| 220 | foreach ($cookies as $name => $value) { |
||
| 221 | $this->cookie($name, $value); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $this; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set the basic authentication to use on the request |
||
| 229 | * @param string $usename |
||
| 230 | * @param string $password |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function basicAuthentication(string $usename, string $password = ''): self |
||
| 234 | { |
||
| 235 | $this->username = $usename; |
||
| 236 | $this->password = $password; |
||
| 237 | |||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set the request timeout |
||
| 243 | * @param int $timeout |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function timeout(int $timeout): self |
||
| 247 | { |
||
| 248 | $this->timeout = $timeout; |
||
| 249 | |||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Controls if the validity of SSL certificates should be verified. |
||
| 255 | * WARNING: This should never be done in a production setup and should be used for debugging only. |
||
| 256 | * @param bool $verifySslCertificate |
||
| 257 | * @return self |
||
| 258 | */ |
||
| 259 | public function verifySslCertificate(bool $verifySslCertificate): self |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set request content type |
||
| 268 | * @param string $contentType |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function contentType(string $contentType): self |
||
| 272 | { |
||
| 273 | // If this is a multipart request and boundary was not defined, |
||
| 274 | // we define a boundary as this is required for multipart requests. |
||
| 275 | if (stripos($contentType, 'multipart/') !== false) { |
||
| 276 | if (stripos($contentType, 'boundary') === false) { |
||
| 277 | $contentType .= sprintf('; boundary="%s"', uniqid((string) time())); |
||
| 278 | // remove double semi-colon, except after scheme |
||
| 279 | $contentType = preg_replace('/(.)(;{2,})/', '$1;', $contentType); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return $this->header('Content-Type', $contentType); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set the request content type as JSON |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function json(): self |
||
| 291 | { |
||
| 292 | $this->contentType('application/json'); |
||
| 293 | |||
| 294 | return $this; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set the request content type as form |
||
| 299 | * @return $this |
||
| 300 | */ |
||
| 301 | public function form(): self |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set the request content type as multipart |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | public function mutlipart(): self |
||
| 313 | { |
||
| 314 | $this->contentType('multipart/form-data'); |
||
| 315 | |||
| 316 | return $this; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Set request accept content type |
||
| 321 | * @param string $contentType |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function accept(string $contentType): self |
||
| 325 | { |
||
| 326 | return $this->header('Accept', $contentType); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set request authorization header |
||
| 331 | * @param string $scheme the scheme to use in the value of the Authorization header (e.g. Bearer) |
||
| 332 | * @param string $value the value to set for the the Authorization header |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function authorization(string $scheme, string $value): self |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Return the headers |
||
| 342 | * @return array<string, array<int, mixed>> |
||
| 343 | */ |
||
| 344 | public function getHeaders(): array |
||
| 345 | { |
||
| 346 | return $this->headers; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return the parameters |
||
| 351 | * @return array<string, mixed> |
||
| 352 | */ |
||
| 353 | public function getParameters(): array |
||
| 354 | { |
||
| 355 | return $this->parameters; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Return the cookies |
||
| 360 | * @return array<string, mixed> |
||
| 361 | */ |
||
| 362 | public function getCookies(): array |
||
| 363 | { |
||
| 364 | return $this->cookies; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Return the timeout |
||
| 369 | * @return int |
||
| 370 | */ |
||
| 371 | public function getTimeout(): int |
||
| 372 | { |
||
| 373 | return $this->timeout; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Whether to verify SSL certificate |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function isVerifySslCertificate(): bool |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Return the username for basic authentication |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function getUsername(): string |
||
| 390 | { |
||
| 391 | return $this->username; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return the password for basic authentication |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function getPassword(): string |
||
| 399 | { |
||
| 400 | return $this->password; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Execute the request as a GET request to the specified path |
||
| 405 | * @param string $path |
||
| 406 | * @return HttpResponse |
||
| 407 | */ |
||
| 408 | public function get(string $path = ''): HttpResponse |
||
| 409 | { |
||
| 410 | return $this->request($path, HttpMethod::GET); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Execute the request as a POST request to the specified path |
||
| 415 | * @param string $path |
||
| 416 | * @param mixed|null $body the request body |
||
| 417 | * @return HttpResponse |
||
| 418 | */ |
||
| 419 | public function post(string $path = '', $body = null): HttpResponse |
||
| 420 | { |
||
| 421 | return $this->request($path, HttpMethod::POST, $body); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Execute the request as a PUT request to the specified path |
||
| 426 | * @param string $path |
||
| 427 | * @param mixed|null $body the request body |
||
| 428 | * @return HttpResponse |
||
| 429 | */ |
||
| 430 | public function put(string $path = '', $body = null): HttpResponse |
||
| 431 | { |
||
| 432 | return $this->request($path, HttpMethod::PUT, $body); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Execute the request as a DELETE request to the specified path |
||
| 437 | * @param string $path |
||
| 438 | * @param mixed|null $body the request body |
||
| 439 | * @return HttpResponse |
||
| 440 | */ |
||
| 441 | public function delete(string $path = '', $body = null): HttpResponse |
||
| 442 | { |
||
| 443 | return $this->request($path, HttpMethod::DELETE, $body); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Execute the request as a HEAD request to the specified path |
||
| 448 | * @param string $path |
||
| 449 | * @param mixed|null $body the request body |
||
| 450 | * @return HttpResponse |
||
| 451 | */ |
||
| 452 | public function head(string $path = '', $body = null): HttpResponse |
||
| 453 | { |
||
| 454 | return $this->request($path, HttpMethod::HEAD, $body); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Execute the request as a TRACE request to the specified path |
||
| 459 | * @param string $path |
||
| 460 | * @param mixed|null $body the request body |
||
| 461 | * @return HttpResponse |
||
| 462 | */ |
||
| 463 | public function trace(string $path = '', $body = null): HttpResponse |
||
| 464 | { |
||
| 465 | return $this->request($path, HttpMethod::TRACE, $body); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Execute the request as a OPTIONS request to the specified path |
||
| 470 | * @param string $path |
||
| 471 | * @param mixed|null $body the request body |
||
| 472 | * @return HttpResponse |
||
| 473 | */ |
||
| 474 | public function options(string $path = '', $body = null): HttpResponse |
||
| 475 | { |
||
| 476 | return $this->request($path, HttpMethod::OPTIONS, $body); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Execute the request as a CONNECT request to the specified path |
||
| 481 | * @param string $path |
||
| 482 | * @param mixed|null $body the request body |
||
| 483 | * @return HttpResponse |
||
| 484 | */ |
||
| 485 | public function connect(string $path = '', $body = null): HttpResponse |
||
| 486 | { |
||
| 487 | return $this->request($path, HttpMethod::CONNECT, $body); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Construct the HTTP request and sends it using the provided method and request body |
||
| 492 | * @param string $path |
||
| 493 | * @param string $method |
||
| 494 | * @param mixed|null $body |
||
| 495 | * @return HttpResponse |
||
| 496 | */ |
||
| 497 | public function request(string $path, string $method = HttpMethod::GET, $body = null): HttpResponse |
||
| 498 | { |
||
| 499 | $ch = curl_init(); |
||
| 500 | |||
| 501 | $this->processUrl($path, $ch); |
||
| 502 | |||
| 503 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
||
| 504 | |||
| 505 | // Do body first as this might add additional headers |
||
| 506 | $this->processBody($ch, $body); |
||
| 507 | $this->processHeaders($ch); |
||
| 508 | $this->processCookies($ch); |
||
| 509 | |||
| 510 | return $this->send($ch); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Send the request |
||
| 515 | * @param mixed $ch the cURL handle |
||
| 516 | * @return HttpResponse |
||
| 517 | */ |
||
| 518 | protected function send($ch): HttpResponse |
||
| 519 | { |
||
| 520 | $responseHeaders = []; |
||
| 521 | curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) { |
||
| 522 | if (strpos($header, ':') !== false) { |
||
| 523 | list($name, $value) = explode(':', $header); |
||
| 524 | if (array_key_exists($name, $responseHeaders) === false) { |
||
| 525 | $responseHeaders[$name] = []; |
||
| 526 | } |
||
| 527 | $responseHeaders[$name][] = trim($value); |
||
| 528 | } |
||
| 529 | |||
| 530 | return strlen($header); |
||
| 531 | }); |
||
| 532 | |||
| 533 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 534 | // Ensure we are coping with 300 (redirect) responses |
||
| 535 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
| 536 | curl_setopt($ch, CURLOPT_HEADER, true); |
||
| 537 | |||
| 538 | // Set request timeout |
||
| 539 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); |
||
| 540 | // Set verification of SSL certificates |
||
| 541 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySslCertificate); |
||
| 542 | |||
| 543 | if (!empty($this->username)) { |
||
| 544 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
||
| 545 | curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $this->username, $this->password)); |
||
| 546 | } |
||
| 547 | |||
| 548 | $response = curl_exec($ch); |
||
| 549 | $requestInfo = curl_getinfo($ch); |
||
| 550 | $error = curl_error($ch); |
||
| 551 | $errorCode = curl_errno($ch); |
||
| 552 | if (!empty($error)) { |
||
| 553 | throw new HttpClientException($error, $errorCode); |
||
| 554 | } |
||
| 555 | |||
| 556 | return new HttpResponse($requestInfo, $responseHeaders, $response, $error); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Process URL |
||
| 561 | * @param string $path |
||
| 562 | * @param mixed $ch the cURL handle |
||
| 563 | * @return void |
||
| 564 | */ |
||
| 565 | protected function processUrl(string $path, $ch): void |
||
| 566 | { |
||
| 567 | $url = $this->buildUrl($path); |
||
| 568 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Build the request full URL |
||
| 573 | * @param string $path |
||
| 574 | * @return string |
||
| 575 | */ |
||
| 576 | protected function buildUrl(string $path): string |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Process the request headers |
||
| 603 | * @param mixed $ch the cURL handle |
||
| 604 | * @return void |
||
| 605 | */ |
||
| 606 | protected function processHeaders($ch): void |
||
| 607 | { |
||
| 608 | $headers = []; |
||
| 609 | foreach ($this->headers as $name => $values) { |
||
| 610 | foreach ($values as $value) { |
||
| 611 | $headers[] = sprintf('%s: %s', $name, $value); |
||
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | if (count($headers) > 0) { |
||
| 616 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
| 617 | } |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Process the request cookies |
||
| 622 | * @param mixed $ch the cURL handle |
||
| 623 | * @return void |
||
| 624 | */ |
||
| 625 | protected function processCookies($ch): void |
||
| 626 | { |
||
| 627 | $cookies = []; |
||
| 628 | foreach ($this->cookies as $name => $value) { |
||
| 629 | $cookies[] = sprintf('%s=%s', $name, $value); |
||
| 630 | } |
||
| 631 | if (count($cookies) > 0) { |
||
| 632 | curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookies)); |
||
| 633 | } |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Process the request body |
||
| 638 | * @param mixed $ch the cURL handle |
||
| 639 | * @param array<mixed>|object|null $body the request body |
||
| 640 | * @return void |
||
| 641 | */ |
||
| 642 | protected function processBody($ch, $body = null): void |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Parse boundary from request content type |
||
| 665 | * @param string $contentType |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | protected function parseBoundaryFromContentType(string $contentType): string |
||
| 669 | { |
||
| 670 | $match = []; |
||
| 671 | if (preg_match('/boundary="([^\"]+)"/is', $contentType, $match) > 0) { |
||
| 672 | return $match[1]; |
||
| 673 | } |
||
| 674 | |||
| 675 | throw new InvalidArgumentException( |
||
| 676 | 'The provided Content-Type header contained a "multipart/*" content type but did not ' |
||
| 677 | . 'define a boundary.' |
||
| 678 | ); |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Build the multipart body |
||
| 683 | * @param array<string, mixed> $fields |
||
| 684 | * @param string $boundary |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | protected function buildMultipartBody(array $fields, string $boundary): string |
||
| 714 | } |
||
| 715 | } |
||
| 716 |