| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Preloader client. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Preload; |
| 11 |
|
| 12 |
use SolidWP\Performance\Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 13 |
use SolidWP\Performance\Symfony\Contracts\HttpClient\HttpClientInterface; |
| 14 |
use SolidWP\Performance\Symfony\Contracts\HttpClient\ResponseInterface; |
| 15 |
|
| 16 |
/** |
| 17 |
* The Preloader client. |
| 18 |
* |
| 19 |
* @package SolidWP\Performance |
| 20 |
*/ |
| 21 |
class Client { |
| 22 |
|
| 23 |
/** |
| 24 |
* The underlying HTTP client. |
| 25 |
* |
| 26 |
* @var HttpClientInterface |
| 27 |
*/ |
| 28 |
private HttpClientInterface $client; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Uri |
| 32 |
*/ |
| 33 |
private Uri $uri; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param HttpClientInterface $client The underlying HTTP client. |
| 37 |
* @param Uri $uri The URI object. |
| 38 |
*/ |
| 39 |
public function __construct( HttpClientInterface $client, Uri $uri ) { |
| 40 |
$this->client = $client; |
| 41 |
$this->uri = $uri; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the underlying HTTP client. |
| 46 |
* |
| 47 |
* @return HttpClientInterface |
| 48 |
*/ |
| 49 |
public function client(): HttpClientInterface { |
| 50 |
return $this->client; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Perform a GET request. |
| 55 |
* |
| 56 |
* @param string $path The relative path to access to the WordPress home URL. |
| 57 |
* @param array<string, mixed> $params The query string parameters to pass to the request. |
| 58 |
* @param array<string, string> $headers Additional headers to send along with the request. |
| 59 |
* @param array<string, mixed> $options Additional Symfony HTTP Client options. |
| 60 |
* |
| 61 |
* @throws TransportExceptionInterface When an error occurs at the transport level, e.g. DNS failure, network failure etc... |
| 62 |
* |
| 63 |
* @return ResponseInterface |
| 64 |
*/ |
| 65 |
public function get( string $path, array $params = [], array $headers = [], array $options = [] ): ResponseInterface { |
| 66 |
return $this->request( |
| 67 |
$path, |
| 68 |
'GET', |
| 69 |
array_merge( |
| 70 |
$options, |
| 71 |
[ |
| 72 |
'query' => $params, |
| 73 |
'headers' => $headers, |
| 74 |
], |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Send a request. |
| 81 |
* |
| 82 |
* @param string $path The relative path or URL to make relative to access to the WordPress home URL. |
| 83 |
* @param string $method The Request Method, e.g. GET, POST, DELETE etc... |
| 84 |
* @param array<string, mixed> $options Additional Symfony HTTP Client options. |
| 85 |
* |
| 86 |
* @throws TransportExceptionInterface When an error occurs at the transport level, e.g. DNS failure, network failure etc... |
| 87 |
* |
| 88 |
* @return ResponseInterface |
| 89 |
*/ |
| 90 |
public function request( string $path, string $method = 'GET', array $options = [] ): ResponseInterface { |
| 91 |
$uri = $this->uri->make_relative( $path ); |
| 92 |
|
| 93 |
return $this->client->request( $method, $uri, $options ); |
| 94 |
} |
| 95 |
} |
| 96 |
|