forked from traderinteractive/tol-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuzzleAdapter.php
More file actions
151 lines (132 loc) · 4.07 KB
/
GuzzleAdapter.php
File metadata and controls
151 lines (132 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace TraderInteractive\Api;
use ArrayObject;
use TraderInteractive\Util;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Concrete implentation of Adapter interface
*/
final class GuzzleAdapter implements AdapterInterface
{
/**
* @var int
*/
const DEFAULT_CONCURRENCY_LIMIT = PHP_INT_MAX;
/**
* Collection of Promise\PromiseInterface instances with keys matching what was given from start().
*
* @var array
*/
private $promises = [];
/**
* Collection of Api\Response with keys matching what was given from start().
*
* @var array
*/
private $responses = [];
/**
* Collection of \Exception with keys matching what was given from start().
*
* @var ArrayObject
*/
private $exceptions;
/**
* @var int
*/
private $concurrencyLimit;
/**
* @var GuzzleClientInterface
*/
private $client;
public function __construct(
GuzzleClientInterface $client = null,
int $concurrencyLimit = self::DEFAULT_CONCURRENCY_LIMIT
) {
$this->exceptions = new ArrayObject();
$this->client = $client ?? new GuzzleClient(
[
'allow_redirects' => false, //stop guzzle from following redirects
'http_errors' => false, //only for 400/500 error codes, actual exceptions can still happen
]
);
$this->concurrencyLimit = $concurrencyLimit;
}
/**
* @see AdapterInterface::start()
*/
public function start(RequestInterface $request) : string
{
$handle = uniqid();
$this->promises[$handle] = $this->client->sendAsync($request);
return $handle;
}
/**
* @see Adapter::end()
*
* @throws \InvalidArgumentException
*/
public function end(string $endHandle) : ResponseInterface
{
$results = $this->fulfillPromises($this->promises, $this->exceptions);
foreach ($results as $handle => $response) {
try {
$contents = (string)$response->getBody();
if (trim($contents) !== '') {
json_decode($contents, true);
Util::ensure(
JSON_ERROR_NONE,
json_last_error(),
'\UnexpectedValueException',
[json_last_error_msg()]
);
}
$this->responses[$handle] = $response;
} catch (\Exception $e) {
$this->exceptions[$handle] = $e;
}
}
$this->promises = [];
if ($this->exceptions->offsetExists($endHandle)) {
$exception = $this->exceptions[$endHandle];
unset($this->exceptions[$endHandle]);
throw $exception;
}
if (array_key_exists($endHandle, $this->responses)) {
$response = $this->responses[$endHandle];
unset($this->responses[$endHandle]);
return $response;
}
throw new \InvalidArgumentException('$endHandle not found');
}
/**
* Helper method to execute all guzzle promises.
*
* @param array $promises
* @param array $exceptions
*
* @return array Array of fulfilled PSR7 responses.
*/
private function fulfillPromises(array $promises, ArrayObject $exceptions) : array
{
if (empty($promises)) {
return [];
}
$results = new ArrayObject();
Promise\Each::ofLimit(
$this->promises,
$this->concurrencyLimit,
function (ResponseInterface $response, $index) use ($results) {
$results[$index] = $response;
},
function (RequestException $e, $index) use ($exceptions) {
$exceptions[$index] = $e;
}
)->wait();
return $results->getArrayCopy();
}
}