1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Clients; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Middleware; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use RuntimeException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class AbstractClient |
17
|
|
|
* |
18
|
|
|
* @package Slides\Connector\Auth\Clients |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractClient |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* HTTP client |
24
|
|
|
* |
25
|
|
|
* @var Client |
26
|
|
|
*/ |
27
|
|
|
protected $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Base URL of resource |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $baseUrl; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Client's request |
38
|
|
|
* |
39
|
|
|
* @var Request |
40
|
|
|
*/ |
41
|
|
|
protected $request; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Client's response |
45
|
|
|
* |
46
|
|
|
* @var Response |
47
|
|
|
*/ |
48
|
|
|
protected $response; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* HTTP request headers |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $headers = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The query parameters. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $query = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The body of an entity enclosing request. |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $body; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The composed credentials. |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $credentials = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Whether client is aborted to perform requests |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
protected $isAborted = false; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* List of supported requests |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
abstract public function requests(): array; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Authorize the client |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
abstract protected function authorize(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get client's credentials |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
abstract protected function credentials(): array; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* BaseClient constructor. |
108
|
|
|
* |
109
|
|
|
* @param array $config |
110
|
|
|
* @param array $credentials |
111
|
|
|
*/ |
112
|
|
|
public function __construct(array $config = [], array $credentials = []) |
113
|
|
|
{ |
114
|
|
|
$this->credentials = array_merge($this->credentials(), $credentials); |
115
|
|
|
|
116
|
|
|
$this->client = new Client(array_merge([ |
117
|
|
|
'base_uri' => $this->baseUrl, |
118
|
|
|
'handler' => $this->createClientHandler(), |
119
|
|
|
'http_errors' => false, |
120
|
|
|
'headers' => [ |
121
|
|
|
'User-Agent' => null |
122
|
|
|
] |
123
|
|
|
], $config)); |
124
|
|
|
|
125
|
|
|
$this->boot(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Boot the client |
130
|
|
|
*/ |
131
|
|
|
protected function boot() |
132
|
|
|
{ |
133
|
|
|
if (!$this->baseUrl) { |
134
|
|
|
throw new InvalidArgumentException('Base URL should be defined'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->authorize(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Send a request |
142
|
|
|
* |
143
|
|
|
* @param AbstractRequest $request |
144
|
|
|
* @param array $options Request options |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
* |
148
|
|
|
* @throws RuntimeException |
149
|
|
|
*/ |
150
|
|
|
public function request(AbstractRequest $request, array $options = []) |
151
|
|
|
{ |
152
|
|
|
if (!$this->resolveRequest($request)) { |
153
|
|
|
throw new InvalidArgumentException(static::class . ': ' . get_class($request) . ' should be registered'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$request->mergeOptions($options) |
157
|
|
|
->mergeOptions(array_filter([ |
158
|
|
|
'query' => $this->query, |
159
|
|
|
'json' => $this->body |
160
|
|
|
])) |
161
|
|
|
->compose(); |
162
|
|
|
|
163
|
|
|
$response = $this->send( |
164
|
|
|
$request->getMethod(), |
165
|
|
|
$request->getUri(), |
166
|
|
|
$request->getOptions() |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$request->setResponse($response); |
170
|
|
|
|
171
|
|
|
// Validate a response |
172
|
|
|
if (!$request->success()) { |
173
|
|
|
throw new RuntimeException(json_encode($response, JSON_PRETTY_PRINT)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $response; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Send a request |
181
|
|
|
* |
182
|
|
|
* @param string $method |
183
|
|
|
* @param string $url |
184
|
|
|
* @param array $options |
185
|
|
|
* |
186
|
|
|
* @return array|null|string |
187
|
|
|
* |
188
|
|
|
* @throws |
189
|
|
|
*/ |
190
|
|
|
public function send(string $method, string $url, array $options = []) |
191
|
|
|
{ |
192
|
|
|
if ($this->isAborted) { |
193
|
|
|
throw new RuntimeException('Cannot send a request while client is aborted'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$this->request = new Request($method, $url, $this->headers); |
197
|
|
|
|
198
|
|
|
if (!$this->response = $this->client->send($this->request, $options)) { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$contents = (string) $this->response->getBody(); |
203
|
|
|
|
204
|
|
|
return json_decode($contents, true) ?: $contents; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Set headers |
209
|
|
|
* |
210
|
|
|
* @param array $headers |
211
|
|
|
*/ |
212
|
|
|
public function headers(array $headers) |
213
|
|
|
{ |
214
|
|
|
$this->headers = $headers; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Add query parameters. |
219
|
|
|
* |
220
|
|
|
* @param array $parameters |
221
|
|
|
* |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
|
|
public function query(array $parameters) |
225
|
|
|
{ |
226
|
|
|
$this->query = array_merge($this->query, $parameters); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Add body parameters. |
231
|
|
|
* |
232
|
|
|
* @param array $parameters |
233
|
|
|
*/ |
234
|
|
|
public function body(array $parameters) |
235
|
|
|
{ |
236
|
|
|
$this->body = array_merge((array) $this->body, $parameters); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get credentials parameter |
241
|
|
|
* |
242
|
|
|
* @param string $key |
243
|
|
|
* @param mixed $default |
244
|
|
|
* |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
public function credential(string $key, $default = null) |
248
|
|
|
{ |
249
|
|
|
return Arr::get($this->credentials, $key, $default); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set the client as aborted and send notification |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
public function abort() |
258
|
|
|
{ |
259
|
|
|
$this->isAborted = true; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get response |
264
|
|
|
* |
265
|
|
|
* @return Response |
266
|
|
|
*/ |
267
|
|
|
public function getResponse() |
268
|
|
|
{ |
269
|
|
|
return $this->response; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Create a handler for Guzzle HTTP Client |
274
|
|
|
* |
275
|
|
|
* @return HandlerStack |
276
|
|
|
*/ |
277
|
|
|
private function createClientHandler() |
278
|
|
|
{ |
279
|
|
|
$stack = HandlerStack::create(); |
280
|
|
|
|
281
|
|
|
$stack->push(Middleware::mapRequest(function (RequestInterface $request) { |
282
|
|
|
return $this->request = $request; |
|
|
|
|
283
|
|
|
})); |
284
|
|
|
|
285
|
|
|
return $stack; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Resolve request |
290
|
|
|
* |
291
|
|
|
* @param AbstractRequest $request |
292
|
|
|
* |
293
|
|
|
* @return AbstractRequest|null |
294
|
|
|
*/ |
295
|
|
|
private function resolveRequest(AbstractRequest $request) |
296
|
|
|
{ |
297
|
|
|
foreach ($this->requests() as $class) { |
298
|
|
|
if (class_basename($class) !== class_basename($request)) { |
299
|
|
|
continue; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $request; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return null; |
306
|
|
|
} |
307
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.