From 1915f71dc4af9132074ce0ba88a967c86d912cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Sun, 6 Jan 2019 22:26:31 +0100 Subject: [PATCH 1/3] Add plugin client builder --- src/PluginClientBuilder.php | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/PluginClientBuilder.php diff --git a/src/PluginClientBuilder.php b/src/PluginClientBuilder.php new file mode 100644 index 0000000..c1012df --- /dev/null +++ b/src/PluginClientBuilder.php @@ -0,0 +1,71 @@ + + */ +final class PluginClientBuilder +{ + /** @var Plugin[][] List of plugins ordered by priority [priority => Plugin[]]). */ + private $plugins = []; + + /** @var array Array of options to give to the plugin client */ + private $options = []; + + /** + * @param int $priority Priority of the plugin. The higher comes first. + */ + public function addPlugin(Plugin $plugin, int $priority = 0): self + { + $this->plugins[$priority][] = $plugin; + + return $this; + } + + public function setOption(string $name, $value): self + { + $this->options[$name] = $value; + + return $this; + } + + public function removeOption(string $name): self + { + unset($this->options[$name]); + + return $this; + } + + /** + * @param ClientInterface | HttpAsyncClient $client + */ + public function createClient($client): PluginClient + { + if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { + throw new \RuntimeException('You must provide a valid http client'); + } + + $plugins = $this->plugins; + + if (0 === count($plugins)) { + $plugins[] = []; + } + + krsort($plugins); + $plugins = array_merge(...$plugins); + + return new PluginClient( + $client, + array_values($plugins), + $this->options + ); + } +} From 0c0a37db65c702b6f03005878f567a8da0fbcaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Sat, 23 Nov 2019 12:52:29 +0100 Subject: [PATCH 2/3] Add a test on the plugin client builder --- tests/PluginClientBuilderTest.php | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/PluginClientBuilderTest.php diff --git a/tests/PluginClientBuilderTest.php b/tests/PluginClientBuilderTest.php new file mode 100644 index 0000000..422cf7e --- /dev/null +++ b/tests/PluginClientBuilderTest.php @@ -0,0 +1,57 @@ + $this->prophesize(Plugin::class)->reveal(), + -10 => $this->prophesize(Plugin::class)->reveal(), + 0 => $this->prophesize(Plugin::class)->reveal(), + ]; + + foreach ($plugins as $priority => $plugin) { + $builder->addPlugin($plugin, $priority); + } + + $client = $this->prophesize($client)->reveal(); + $client = $builder->createClient($client); + + $closure = Closure::bind( + function (): array { + return $this->plugins; + }, + $client, + PluginClient::class + ); + + $plugged = $closure(); + + $expected = $plugins; + krsort($expected); + $expected = array_values($expected); + + $this->assertSame($expected, $plugged); + } + + public function clientProvider(): iterable + { + yield 'sync\'d http client' => [HttpClient::class]; + yield 'async\'d http client' => [HttpAsyncClient::class]; + } +} From cbb22ef6b2004e5edc247514eb86b56059c19913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Sat, 23 Nov 2019 12:59:32 +0100 Subject: [PATCH 3/3] Add test on passing options from the client builder to the client --- tests/PluginClientBuilderTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/PluginClientBuilderTest.php b/tests/PluginClientBuilderTest.php index 422cf7e..190c4a5 100644 --- a/tests/PluginClientBuilderTest.php +++ b/tests/PluginClientBuilderTest.php @@ -49,6 +49,29 @@ function (): array { $this->assertSame($expected, $plugged); } + /** @dataProvider clientProvider */ + public function testOptions(string $client): void + { + $builder = new PluginClientBuilder(); + $builder->setOption('max_restarts', 5); + + $client = $this->prophesize($client)->reveal(); + $client = $builder->createClient($client); + + $closure = Closure::bind( + function (): array { + return $this->options; + }, + $client, + PluginClient::class + ); + + $options = $closure(); + + $this->assertArrayHasKey('max_restarts', $options); + $this->assertSame(5, $options['max_restarts']); + } + public function clientProvider(): iterable { yield 'sync\'d http client' => [HttpClient::class];