Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/MetadataService/Psr18HttpClient.php

raw symbols SCIP
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 * @var array<string, string>
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 * @param array{base_uri?: string, query?: array<string, string>, body?: string, headers?: array<string, string>, ...} $options
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 * @param ResponseInterface|iterable<array-key, ResponseInterface> $responses
62 */
63 public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface
64 {
65 throw new LogicException('Not implemented');
66 }
67
68 /**
69 * @param array<string, string> $options
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 /** @var array<string, list<string>> $headers */
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 * @param array<string, list<string>> $headers
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 * @return array<string, list<string>>
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 * @return array<array-key, mixed>
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 // noop
130 }
131
132 public function getInfo(?string $type = null): mixed
133 {
134 return null;
135 }
136 };
137 }
138 }