File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Http \Client \Common ;
6
+
7
+ use Http \Client \HttpAsyncClient ;
8
+ use Psr \Http \Client \ClientInterface ;
9
+
10
+ /**
11
+ * Build an instance of a PluginClient with a dynamic list of plugins.
12
+ *
13
+ * @author Baptiste Clavié <clavie.b@gmail.com>
14
+ */
15
+ final class PluginClientBuilder
16
+ {
17
+ /** @var Plugin[][] List of plugins ordered by priority [priority => Plugin[]]). */
18
+ private $ plugins = [];
19
+
20
+ /** @var array Array of options to give to the plugin client */
21
+ private $ options = [];
22
+
23
+ /**
24
+ * @param int $priority Priority of the plugin. The higher comes first.
25
+ */
26
+ public function addPlugin (Plugin $ plugin , int $ priority = 0 ): self
27
+ {
28
+ $ this ->plugins [$ priority ][] = $ plugin ;
29
+
30
+ return $ this ;
31
+ }
32
+
33
+ /**
34
+ * @param ClientInterface | HttpAsyncClient $client
35
+ */
36
+ public function createClient ($ client ): PluginClient
37
+ {
38
+ if (!$ client instanceof ClientInterface && !$ client instanceof HttpAsyncClient) {
39
+ throw new \RuntimeException ('You must provide a valid http client ' );
40
+ }
41
+
42
+ $ plugins = $ this ->plugins ;
43
+
44
+ if (0 === count ($ plugins )) {
45
+ $ plugins [] = [];
46
+ }
47
+
48
+ krsort ($ plugins );
49
+ $ plugins = array_merge (...$ plugins );
50
+
51
+ return new PluginClient (
52
+ $ client ,
53
+ array_values ($ plugins ),
54
+ $ this ->options
55
+ );
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments