1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation\Transport; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Middleware\Locator\Locator; |
6
|
|
|
use ApiClients\Foundation\Middleware\MiddlewareInterface; |
7
|
|
|
use ApiClients\Foundation\Middleware\MiddlewareRunner; |
8
|
|
|
use ApiClients\Foundation\Transport\CommandBus; |
9
|
|
|
use Clue\React\Buzz\Browser; |
10
|
|
|
use Interop\Container\ContainerInterface; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use React\EventLoop\LoopInterface; |
15
|
|
|
use React\Promise\PromiseInterface; |
16
|
|
|
use RingCentral\Psr7\Uri; |
17
|
|
|
use function React\Promise\reject; |
18
|
|
|
use function React\Promise\resolve; |
19
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
20
|
|
|
|
21
|
|
|
final class Client implements ClientInterface |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_OPTIONS = [ |
24
|
|
|
Options::SCHEMA => 'https', |
25
|
|
|
Options::PATH => '/', |
26
|
|
|
Options::HEADERS => [], |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var LoopInterface |
31
|
|
|
*/ |
32
|
|
|
protected $loop; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ContainerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $locator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Browser |
41
|
|
|
*/ |
42
|
|
|
protected $browser; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $options = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string[] |
51
|
|
|
*/ |
52
|
|
|
protected $middleware = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param LoopInterface $loop |
56
|
|
|
* @param Locator|ContainerInterface $locator |
57
|
|
|
* @param Browser $buzz |
58
|
|
|
* @param array $options |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
LoopInterface $loop, |
62
|
|
|
Locator $locator, |
63
|
|
|
Browser $buzz, |
64
|
|
|
array $options = [] |
65
|
|
|
) { |
66
|
|
|
$this->loop = $loop; |
67
|
|
|
$this->locator = $locator; |
|
|
|
|
68
|
|
|
$this->browser = $buzz; |
69
|
|
|
$this->options = $options + self::DEFAULT_OPTIONS; |
70
|
|
|
|
71
|
|
|
if (isset($this->options[Options::MIDDLEWARE])) { |
72
|
|
|
$this->middleware = $this->options[Options::MIDDLEWARE]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function constructMiddlewares(array $options): MiddlewareRunner |
77
|
|
|
{ |
78
|
|
|
$set = $this->middleware; |
79
|
|
|
|
80
|
|
|
if (isset($options[Options::MIDDLEWARE])) { |
81
|
|
|
$set = $this->combinedMiddlewares($options[Options::MIDDLEWARE]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$args = []; |
85
|
|
|
$args[] = $options; |
86
|
|
|
foreach ($set as $middleware) { |
87
|
|
|
$args[] = $this->locator->get($middleware); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return new MiddlewareRunner(...$args); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function combinedMiddlewares(array $extraMiddlewares): array |
94
|
|
|
{ |
95
|
|
|
$set = $this->middleware; |
96
|
|
|
|
97
|
|
|
foreach ($extraMiddlewares as $middleware) { |
98
|
|
|
if (in_array($middleware, $set)) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$set[] = $middleware; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $set; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param RequestInterface $request |
110
|
|
|
* @param array $options |
111
|
|
|
* @return PromiseInterface |
112
|
|
|
*/ |
113
|
|
|
public function request(RequestInterface $request, array $options = []): PromiseInterface |
114
|
|
|
{ |
115
|
|
|
$request = $this->applyApiSettingsToRequest($request); |
116
|
|
|
$options = $this->applyRequestOptions($options); |
117
|
|
|
$executioner = $this->constructMiddlewares($options); |
118
|
|
|
|
119
|
|
|
return $executioner->pre($request)->then(function ($request) use ($options) { |
120
|
|
|
return resolve($this->browser->send( |
121
|
|
|
$request |
122
|
|
|
)); |
123
|
|
|
}, function (ResponseInterface $response) { |
124
|
|
|
return resolve($response); |
125
|
|
|
})->then(function (ResponseInterface $response) use ($executioner) { |
126
|
|
|
return $executioner->post($response); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function applyApiSettingsToRequest(RequestInterface $request): RequestInterface |
131
|
|
|
{ |
132
|
|
|
$uri = $request->getUri(); |
133
|
|
|
if (strpos((string)$uri, '://') === false) { |
134
|
|
|
$uri = Uri::resolve( |
135
|
|
|
new Uri( |
136
|
|
|
$this->options[Options::SCHEMA] . |
137
|
|
|
'://' . |
138
|
|
|
$this->options[Options::HOST] . |
139
|
|
|
$this->options[Options::PATH] |
140
|
|
|
), |
141
|
|
|
$request->getUri() |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
foreach ($this->options[Options::HEADERS] as $key => $value) { |
146
|
|
|
$request = $request->withAddedHeader($key, $value); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $request->withUri($uri)->withAddedHeader('User-Agent', $this->options[Options::USER_AGENT]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function applyRequestOptions(array $options): array |
153
|
|
|
{ |
154
|
|
|
if (!isset($this->options[Options::DEFAULT_REQUEST_OPTIONS])) { |
155
|
|
|
return $options; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return array_merge_recursive( |
159
|
|
|
$this->options[Options::DEFAULT_REQUEST_OPTIONS], |
160
|
|
|
$options |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..