|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Phpro\HttpTools\Client; |
| 6 | + |
| 7 | +use Http\Client\Common\Plugin; |
| 8 | +use Http\Client\Common\PluginClient; |
| 9 | +use Http\Client\Plugin\Vcr\NamingStrategy\NamingStrategyInterface; |
| 10 | +use Http\Client\Plugin\Vcr\Recorder\PlayerInterface; |
| 11 | +use Http\Client\Plugin\Vcr\Recorder\RecorderInterface; |
| 12 | +use Http\Client\Plugin\Vcr\RecordPlugin; |
| 13 | +use Http\Client\Plugin\Vcr\ReplayPlugin; |
| 14 | +use Http\Discovery\Psr17FactoryDiscovery; |
| 15 | +use Http\Discovery\Psr18ClientDiscovery; |
| 16 | +use Http\Message\Authentication; |
| 17 | +use Http\Message\Formatter; |
| 18 | +use Phpro\HttpTools\Client\Configurator\PluginsConfigurator; |
| 19 | +use Psr\Http\Client\ClientInterface; |
| 20 | +use Psr\Http\Message\UriInterface; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | +use SplPriorityQueue; |
| 23 | + |
| 24 | +final class ClientBuilder |
| 25 | +{ |
| 26 | + public const int PRIORITY_LEVEL_DEFAULT = 0; |
| 27 | + public const int PRIORITY_LEVEL_SECURITY = 1000; |
| 28 | + public const int PRIORITY_LEVEL_LOGGING = 2000; |
| 29 | + |
| 30 | + private ClientInterface $client; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var SplPriorityQueue<Plugin> |
| 34 | + */ |
| 35 | + private SplPriorityQueue $plugins; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + ?ClientInterface $client = null, |
| 39 | + iterable $middlewares = [], |
| 40 | + ) { |
| 41 | + $this->client = $client ?? Psr18ClientDiscovery::find(); |
| 42 | + $this->plugins = new SplPriorityQueue(); |
| 43 | + |
| 44 | + foreach ($middlewares as $middleware) { |
| 45 | + $this->plugins->insert($middleware, self::PRIORITY_LEVEL_DEFAULT); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static function default( |
| 50 | + ?ClientInterface $client = null, |
| 51 | + ): self { |
| 52 | + return new self($client, [ |
| 53 | + new Plugin\ErrorPlugin(), |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param \Closure(ClientInterface): ClientInterface $decorator |
| 59 | + */ |
| 60 | + public function addDecorator(\Closure $decorator): self |
| 61 | + { |
| 62 | + $this->client = $decorator($this->client); |
| 63 | + |
| 64 | + return $this; |
| 65 | + } |
| 66 | + |
| 67 | + public function addPlugin( |
| 68 | + Plugin $plugin, |
| 69 | + int $priority = self::PRIORITY_LEVEL_DEFAULT, |
| 70 | + ): self { |
| 71 | + $this->plugins->insert($plugin, $priority); |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param \Closure(ClientInterface): Plugin $lazyPlugin |
| 78 | + * @return self |
| 79 | + */ |
| 80 | + public function addPluginWithCurrentlyConfiguredClient( |
| 81 | + \Closure $pluginBuilder, |
| 82 | + int $priority = self::PRIORITY_LEVEL_DEFAULT, |
| 83 | + ): self { |
| 84 | + return $this->addPlugin( |
| 85 | + $pluginBuilder($this->build()), |
| 86 | + $priority, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public function addAuthentication( |
| 91 | + Authentication $authentication, |
| 92 | + int $priority = self::PRIORITY_LEVEL_SECURITY, |
| 93 | + ): self { |
| 94 | + return $this->addPlugin(new Plugin\AuthenticationPlugin($authentication), $priority); |
| 95 | + } |
| 96 | + |
| 97 | + public function addLogger( |
| 98 | + LoggerInterface $logger, |
| 99 | + ?Formatter $formatter = null, |
| 100 | + int $priority = self::PRIORITY_LEVEL_LOGGING, |
| 101 | + ): self { |
| 102 | + return $this->addPlugin(new Plugin\LoggerPlugin($logger, $formatter), $priority); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param array<string, string | string[]> $headers |
| 107 | + * |
| 108 | + * @return $this |
| 109 | + */ |
| 110 | + public function addHeaders( |
| 111 | + array $headers, |
| 112 | + int $priority = self::PRIORITY_LEVEL_DEFAULT, |
| 113 | + ): self { |
| 114 | + return $this->addPlugin(new Plugin\HeaderSetPlugin($headers), $priority); |
| 115 | + } |
| 116 | + |
| 117 | + public function addBaseUri( |
| 118 | + UriInterface|string $baseUri, |
| 119 | + bool $replaceHost = true, |
| 120 | + int $priority = self::PRIORITY_LEVEL_DEFAULT, |
| 121 | + ): self { |
| 122 | + $baseUri = match (true) { |
| 123 | + is_string($baseUri) => Psr17FactoryDiscovery::findUriFactory()->createUri($baseUri), |
| 124 | + default => $baseUri, |
| 125 | + }; |
| 126 | + |
| 127 | + return $this->addPlugin(new Plugin\BaseUriPlugin($baseUri, ['replace' => $replaceHost]), $priority); |
| 128 | + } |
| 129 | + |
| 130 | + public function addRecording( |
| 131 | + NamingStrategyInterface $namingStrategy, |
| 132 | + RecorderInterface&PlayerInterface $recorder, |
| 133 | + int $priority = self::PRIORITY_LEVEL_LOGGING, |
| 134 | + ): self { |
| 135 | + return $this |
| 136 | + ->addPlugin(new RecordPlugin($namingStrategy, $recorder), $priority) |
| 137 | + ->addPlugin(new ReplayPlugin($namingStrategy, $recorder, false), $priority); |
| 138 | + } |
| 139 | + |
| 140 | + public function build(): PluginClient |
| 141 | + { |
| 142 | + return PluginsConfigurator::configure($this->client, [...clone $this->plugins]); |
| 143 | + } |
| 144 | +} |
0 commit comments