Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/DecoratorTrait.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpClient;
13
14 use Symfony\Contracts\HttpClient\HttpClientInterface;
15 use Symfony\Contracts\HttpClient\ResponseInterface;
16 use Symfony\Contracts\HttpClient\ResponseStreamInterface;
17 use Symfony\Contracts\Service\ResetInterface;
18
19 /**
20 * Eases with writing decorators.
21 *
22 * @author Nicolas Grekas <[email protected]>
23 */
24 trait DecoratorTrait
25 {
26 private HttpClientInterface $client;
27
28 public function __construct(?HttpClientInterface $client = null)
29 {
30 $this->client = $client ?? HttpClient::create();
31 }
32
33 public function request(string $method, string $url, array $options = []): ResponseInterface
34 {
35 return $this->client->request($method, $url, $options);
36 }
37
38 public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
39 {
40 return $this->client->stream($responses, $timeout);
41 }
42
43 public function withOptions(array $options): static
44 {
45 $clone = clone $this;
46 $clone->client = $this->client->withOptions($options);
47
48 return $clone;
49 }
50
51 public function reset(): void
52 {
53 if ($this->client instanceof ResetInterface) {
54 $this->client->reset();
55 }
56 }
57 }