|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CoyoteCert\Http; |
| 4 | + |
| 5 | +use Psr\Http\Client\ClientInterface; |
| 6 | +use Psr\Http\Message\RequestFactoryInterface; |
| 7 | +use Psr\Http\Message\StreamFactoryInterface; |
| 8 | +use CoyoteCert\Interfaces\HttpClientInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Adapts any PSR-18 HTTP client to CoyoteCert's internal HttpClientInterface. |
| 12 | + * |
| 13 | + * Both $requestFactory and $streamFactory are optional when the PSR-18 client |
| 14 | + * also implements those interfaces (e.g. Symfony's Psr18Client, nyholm/psr7). |
| 15 | + * |
| 16 | + * Usage: |
| 17 | + * |
| 18 | + * // Symfony — client implements all three interfaces |
| 19 | + * CoyoteCert::with(new LetsEncrypt()) |
| 20 | + * ->httpClient(new \Symfony\Component\HttpClient\Psr18Client()) |
| 21 | + * ->issue(); |
| 22 | + * |
| 23 | + * // Guzzle — separate factory required |
| 24 | + * CoyoteCert::with(new LetsEncrypt()) |
| 25 | + * ->httpClient(new \GuzzleHttp\Client(), new \GuzzleHttp\Psr7\HttpFactory()) |
| 26 | + * ->issue(); |
| 27 | + */ |
| 28 | +class Psr18Adapter implements HttpClientInterface |
| 29 | +{ |
| 30 | + private RequestFactoryInterface $requestFactory; |
| 31 | + private StreamFactoryInterface $streamFactory; |
| 32 | + |
| 33 | + public function __construct( |
| 34 | + private readonly ClientInterface $client, |
| 35 | + ?RequestFactoryInterface $requestFactory = null, |
| 36 | + ?StreamFactoryInterface $streamFactory = null, |
| 37 | + ) { |
| 38 | + $this->requestFactory = $requestFactory |
| 39 | + ?? ($client instanceof RequestFactoryInterface ? $client |
| 40 | + : throw new \InvalidArgumentException( |
| 41 | + 'Provide a RequestFactoryInterface or use a client that implements it (e.g. Symfony Psr18Client).' |
| 42 | + )); |
| 43 | + |
| 44 | + $this->streamFactory = $streamFactory |
| 45 | + ?? ($client instanceof StreamFactoryInterface ? $client |
| 46 | + : throw new \InvalidArgumentException( |
| 47 | + 'Provide a StreamFactoryInterface or use a client that implements it (e.g. Symfony Psr18Client).' |
| 48 | + )); |
| 49 | + } |
| 50 | + |
| 51 | + public function head(string $url): Response |
| 52 | + { |
| 53 | + $request = $this->requestFactory->createRequest('HEAD', $url) |
| 54 | + ->withHeader('User-Agent', 'blendbyte/coyote-cert') |
| 55 | + ->withHeader('Accept', 'application/json'); |
| 56 | + |
| 57 | + return $this->send($request, $url); |
| 58 | + } |
| 59 | + |
| 60 | + public function get(string $url, array $headers = [], array $arguments = [], int $maxRedirects = 0): Response |
| 61 | + { |
| 62 | + if (!empty($arguments)) { |
| 63 | + $url .= '?' . http_build_query($arguments); |
| 64 | + } |
| 65 | + |
| 66 | + $request = $this->requestFactory->createRequest('GET', $url) |
| 67 | + ->withHeader('User-Agent', 'blendbyte/coyote-cert') |
| 68 | + ->withHeader('Accept', 'application/json'); |
| 69 | + |
| 70 | + foreach ($headers as $header) { |
| 71 | + [$name, $value] = explode(':', $header, 2); |
| 72 | + $request = $request->withAddedHeader(trim($name), trim($value)); |
| 73 | + } |
| 74 | + |
| 75 | + return $this->send($request, $url); |
| 76 | + } |
| 77 | + |
| 78 | + public function post(string $url, array $payload = [], array $headers = [], int $maxRedirects = 0): Response |
| 79 | + { |
| 80 | + $body = json_encode($payload, JSON_THROW_ON_ERROR); |
| 81 | + $request = $this->requestFactory->createRequest('POST', $url) |
| 82 | + ->withHeader('User-Agent', 'blendbyte/coyote-cert') |
| 83 | + ->withHeader('Accept', 'application/json') |
| 84 | + ->withHeader('Content-Type', 'application/jose+json') |
| 85 | + ->withBody($this->streamFactory->createStream($body)); |
| 86 | + |
| 87 | + foreach ($headers as $header) { |
| 88 | + [$name, $value] = explode(':', $header, 2); |
| 89 | + $request = $request->withAddedHeader(trim($name), trim($value)); |
| 90 | + } |
| 91 | + |
| 92 | + return $this->send($request, $url); |
| 93 | + } |
| 94 | + |
| 95 | + private function send(\Psr\Http\Message\RequestInterface $request, string $url): Response |
| 96 | + { |
| 97 | + $psrResponse = $this->client->sendRequest($request); |
| 98 | + |
| 99 | + $headers = []; |
| 100 | + foreach ($psrResponse->getHeaders() as $name => $values) { |
| 101 | + $headers[strtolower($name)] = implode(', ', $values); |
| 102 | + } |
| 103 | + |
| 104 | + $rawBody = (string) $psrResponse->getBody(); |
| 105 | + $body = json_validate($rawBody) |
| 106 | + ? json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR) |
| 107 | + : $rawBody; |
| 108 | + |
| 109 | + return new Response( |
| 110 | + headers: $headers, |
| 111 | + requestedUrl: $url, |
| 112 | + statusCode: $psrResponse->getStatusCode(), |
| 113 | + body: $body, |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments