1 | <?php |
||
15 | */ |
||
16 | final class GuzzleAdapter implements AdapterInterface |
||
17 | { |
||
18 | /** |
||
19 | * Collection of Promise\PromiseInterface instances with keys matching what was given from start(). |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $promises = []; |
||
24 | |||
25 | /** |
||
26 | * Collection of Api\Response with keys matching what was given from start(). |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private $responses = []; |
||
31 | |||
32 | /** |
||
33 | * Collection of \Exception with keys matching what was given from start(). |
||
34 | * |
||
35 | * @var ArrayObject |
||
36 | */ |
||
37 | private $exceptions; |
||
38 | |||
39 | /** |
||
40 | * @var GuzzleClientInterface |
||
41 | */ |
||
42 | private $client; |
||
43 | |||
44 | public function __construct(GuzzleClientInterface $client = null) |
||
54 | |||
55 | /** |
||
56 | * @see AdapterInterface::start() |
||
57 | */ |
||
58 | public function start(Request $request) : string |
||
72 | |||
73 | /** |
||
74 | * @see Adapter::end() |
||
75 | * |
||
76 | * @throws \InvalidArgumentException |
||
77 | */ |
||
78 | public function end(string $endHandle) : Response |
||
117 | |||
118 | /** |
||
119 | * Helper method to execute all guzzle promises. |
||
120 | * |
||
121 | * @param array $promises |
||
122 | * @param array $exceptions |
||
123 | * |
||
124 | * @return array Array of fulfilled PSR7 responses. |
||
125 | */ |
||
126 | private function fulfillPromises(array $promises, ArrayObject $exceptions) : array |
||
127 | { |
||
128 | if (empty($promises)) { |
||
129 | return []; |
||
130 | } |
||
131 | |||
132 | $results = new ArrayObject(); |
||
133 | Promise\each( |
||
134 | $this->promises, |
||
135 | function (ResponseInterface $response, $index) use ($results) { |
||
136 | $results[$index] = $response; |
||
137 | }, |
||
138 | function (RequestException $e, $index) use ($exceptions) { |
||
139 | $exceptions[$index] = $e; |
||
140 | } |
||
141 | )->wait(); |
||
142 | |||
143 | return $results->getArrayCopy(); |
||
144 | } |
||
146 |