1 | <?php |
||
19 | final class PluginClient implements HttpClient, HttpAsyncClient |
||
20 | { |
||
21 | /** |
||
22 | * An HTTP async client. |
||
23 | * |
||
24 | * @var HttpAsyncClient |
||
25 | */ |
||
26 | private $client; |
||
27 | |||
28 | /** |
||
29 | * The plugin chain. |
||
30 | * |
||
31 | * @var Plugin[] |
||
32 | */ |
||
33 | private $plugins; |
||
34 | |||
35 | /** |
||
36 | * A list of options. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $options; |
||
41 | |||
42 | /** |
||
43 | * @param HttpClient|HttpAsyncClient $client |
||
44 | * @param Plugin[] $plugins |
||
45 | * @param array $options { |
||
46 | * |
||
47 | * @var int $max_restarts |
||
48 | * @var Plugin[] $debug_plugins an array of plugins that are injected between each normal plugin |
||
49 | * } |
||
50 | * |
||
51 | * @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient |
||
52 | */ |
||
53 | 11 | public function __construct($client, array $plugins = [], array $options = []) |
|
54 | { |
||
55 | 11 | if ($client instanceof HttpAsyncClient) { |
|
56 | 3 | $this->client = $client; |
|
57 | 8 | } elseif ($client instanceof HttpClient) { |
|
58 | 8 | $this->client = new EmulatedHttpAsyncClient($client); |
|
59 | } else { |
||
60 | throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient'); |
||
61 | } |
||
62 | |||
63 | 11 | $this->plugins = $plugins; |
|
64 | 11 | $this->options = $this->configure($options); |
|
65 | 11 | } |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 5 | public function sendRequest(RequestInterface $request) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | public function sendAsyncRequest(RequestInterface $request) |
|
101 | |||
102 | /** |
||
103 | * Configure the plugin client. |
||
104 | * |
||
105 | * @param array $options |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 11 | private function configure(array $options = []) |
|
136 | |||
137 | /** |
||
138 | * Create the plugin chain. |
||
139 | * |
||
140 | * @param Plugin[] $pluginList A list of plugins |
||
141 | * @param callable $clientCallable Callable making the HTTP call |
||
142 | * |
||
143 | * @return callable |
||
144 | */ |
||
145 | 6 | private function createPluginChain($pluginList, callable $clientCallable) |
|
179 | } |
||
180 |
If you suppress an error, we recommend checking for the error condition explicitly: