| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\MetadataService; |
| 6 |
|
| 7 |
use function is_array; |
| 8 |
use const JSON_ERROR_NONE; |
| 9 |
use JsonException; |
| 10 |
use LogicException; |
| 11 |
use const PHP_QUERY_RFC3986; |
| 12 |
use Psr\Http\Client\ClientInterface as Psr18ClientInterface; |
| 13 |
use Psr\Http\Message\RequestFactoryInterface as Psr17RequestFactoryInterface; |
| 14 |
use Psr\Http\Message\ResponseInterface as Psr17ResponseInterface; |
| 15 |
use Psr\Http\Message\StreamFactoryInterface as Psr17StreamFactoryInterface; |
| 16 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 17 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 18 |
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
| 19 |
|
| 20 |
class Psr18HttpClient implements HttpClientInterface |
| 21 |
{ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private array $options = []; |
| 26 |
|
| 27 |
public function __construct( |
| 28 |
private readonly Psr18ClientInterface $client, |
| 29 |
private readonly Psr17RequestFactoryInterface $requestFactory, |
| 30 |
private readonly Psr17StreamFactoryInterface $streamFactory, |
| 31 |
) { |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 38 |
{ |
| 39 |
$baseUri = $options['base_uri'] ?? ''; |
| 40 |
$query = $options['query'] ?? []; |
| 41 |
if ($query !== []) { |
| 42 |
$url .= (! str_contains($url, '?') ? '?' : '&') . http_build_query($query, '', '&', PHP_QUERY_RFC3986); |
| 43 |
} |
| 44 |
$request = $this->requestFactory->createRequest($method, $baseUri . $url); |
| 45 |
$body = $options['body'] ?? null; |
| 46 |
if ($body !== null) { |
| 47 |
$request = $request->withBody($this->streamFactory->createStream($body)); |
| 48 |
} |
| 49 |
foreach ($this->options as $name => $value) { |
| 50 |
$request = $request->withHeader($name, $value); |
| 51 |
} |
| 52 |
foreach ($options['headers'] ?? [] as $name => $value) { |
| 53 |
$request = $request->withHeader($name, $value); |
| 54 |
} |
| 55 |
$response = $this->client->sendRequest($request); |
| 56 |
|
| 57 |
return static::fromPsr17($response); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface |
| 64 |
{ |
| 65 |
throw new LogicException('Not implemented'); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function withOptions(array $options): static |
| 72 |
{ |
| 73 |
$this->options = $options; |
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
protected static function fromPsr17(Psr17ResponseInterface $response): ResponseInterface |
| 78 |
{ |
| 79 |
|
| 80 |
$headers = $response->getHeaders(); |
| 81 |
$content = $response->getBody() |
| 82 |
->getContents(); |
| 83 |
$status = $response->getStatusCode(); |
| 84 |
|
| 85 |
return new class($status, $headers, $content) implements ResponseInterface { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function __construct( |
| 90 |
private readonly int $status, |
| 91 |
private readonly array $headers, |
| 92 |
private readonly string $content, |
| 93 |
) { |
| 94 |
} |
| 95 |
|
| 96 |
public function getStatusCode(): int |
| 97 |
{ |
| 98 |
return $this->status; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function getHeaders(bool $throw = true): array |
| 105 |
{ |
| 106 |
return $this->headers; |
| 107 |
} |
| 108 |
|
| 109 |
public function getContent(bool $throw = true): string |
| 110 |
{ |
| 111 |
return $this->content; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function toArray(bool $throw = true): array |
| 118 |
{ |
| 119 |
$result = json_decode($this->content, true); |
| 120 |
if (! is_array($result) || json_last_error() !== JSON_ERROR_NONE) { |
| 121 |
throw new JsonException('Failed to decode JSON response: ' . json_last_error_msg()); |
| 122 |
} |
| 123 |
|
| 124 |
return $result; |
| 125 |
} |
| 126 |
|
| 127 |
public function cancel(): void |
| 128 |
{ |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
public function getInfo(?string $type = null): mixed |
| 133 |
{ |
| 134 |
return null; |
| 135 |
} |
| 136 |
}; |
| 137 |
} |
| 138 |
} |