1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yandex PHP Library |
4
|
|
|
* |
5
|
|
|
* @copyright NIX Solutions Ltd. |
6
|
|
|
* @link https://github.com/nixsolutions/yandex-php-library |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @namespace |
11
|
|
|
*/ |
12
|
|
|
namespace Yandex\Common; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\ClientInterface; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
use GuzzleHttp\Exception\ClientException; |
18
|
|
|
use GuzzleHttp\Psr7\Response; |
19
|
|
|
use Yandex\Common\Exception\MissedArgumentException; |
20
|
|
|
use Yandex\Common\Exception\ProfileNotFoundException; |
21
|
|
|
use Yandex\Common\Exception\YandexException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class AbstractServiceClient |
25
|
|
|
* |
26
|
|
|
* @package Yandex\Common |
27
|
|
|
* |
28
|
|
|
* @author Eugene Zabolotniy <[email protected]> |
29
|
|
|
* @created 13.09.13 12:09 |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractServiceClient extends AbstractPackage |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Request schemes constants |
35
|
|
|
*/ |
36
|
|
|
const HTTPS_SCHEME = 'https'; |
37
|
|
|
const HTTP_SCHEME = 'http'; |
38
|
|
|
|
39
|
|
|
const DECODE_TYPE_JSON = 'json'; |
40
|
|
|
const DECODE_TYPE_XML = 'xml'; |
41
|
|
|
const DECODE_TYPE_DEFAULT = self::DECODE_TYPE_JSON; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $serviceScheme = self::HTTPS_SCHEME; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Can be HTTP 1.0 or HTTP 1.1 |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $serviceProtocolVersion = '1.1'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $serviceDomain = ''; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $servicePort = ''; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $accessToken = ''; |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var \DateTime |
72
|
|
|
*/ |
73
|
|
|
protected $expiresIn = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected $proxy = ''; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
protected $debug = false; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var null |
87
|
|
|
*/ |
88
|
|
|
protected $client = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var string |
92
|
|
|
*/ |
93
|
|
|
protected $libraryName = 'yandex-php-library'; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
3 |
|
public function getUserAgent() |
99
|
|
|
{ |
100
|
3 |
|
return $this->libraryName . '/' . Version::$version; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $accessToken |
105
|
|
|
* |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
147 |
|
public function setAccessToken($accessToken) |
109
|
|
|
{ |
110
|
147 |
|
$this->accessToken = $accessToken; |
111
|
|
|
|
112
|
147 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
46 |
|
public function getAccessToken() |
119
|
|
|
{ |
120
|
46 |
|
return $this->accessToken; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param \DateTime $expiresIn |
125
|
|
|
*/ |
126
|
3 |
|
public function setExpiresIn($expiresIn) |
127
|
|
|
{ |
128
|
3 |
|
$this->expiresIn = $expiresIn; |
129
|
3 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
2 |
|
public function getExpiresIn() |
135
|
|
|
{ |
136
|
2 |
|
return $this->expiresIn; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $proxy |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setProxy($proxy) |
144
|
|
|
{ |
145
|
|
|
$this->proxy = $proxy; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
3 |
|
public function getProxy() |
154
|
|
|
{ |
155
|
3 |
|
return $this->proxy; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $debug |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function setDebug($debug) |
163
|
|
|
{ |
164
|
|
|
$this->debug = (bool) $debug; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
3 |
|
public function getDebug() |
173
|
|
|
{ |
174
|
3 |
|
return $this->debug; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $serviceDomain |
179
|
|
|
* |
180
|
|
|
* @return self |
181
|
|
|
*/ |
182
|
4 |
|
public function setServiceDomain($serviceDomain) |
183
|
|
|
{ |
184
|
4 |
|
$this->serviceDomain = $serviceDomain; |
185
|
|
|
|
186
|
4 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
9 |
|
public function getServiceDomain() |
193
|
|
|
{ |
194
|
9 |
|
return $this->serviceDomain; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $servicePort |
199
|
|
|
* |
200
|
|
|
* @return self |
201
|
|
|
*/ |
202
|
4 |
|
public function setServicePort($servicePort) |
203
|
|
|
{ |
204
|
4 |
|
$this->servicePort = $servicePort; |
205
|
|
|
|
206
|
4 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
4 |
|
public function getServicePort() |
213
|
|
|
{ |
214
|
4 |
|
return $this->servicePort; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $serviceScheme |
219
|
|
|
* |
220
|
|
|
* @return self |
221
|
|
|
*/ |
222
|
4 |
|
public function setServiceScheme($serviceScheme = self::HTTPS_SCHEME) |
223
|
|
|
{ |
224
|
4 |
|
$this->serviceScheme = $serviceScheme; |
225
|
|
|
|
226
|
4 |
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
6 |
|
public function getServiceScheme() |
233
|
|
|
{ |
234
|
6 |
|
return $this->serviceScheme; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $resource |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
17 |
|
public function getServiceUrl($resource = '') |
242
|
|
|
{ |
243
|
17 |
|
return $this->serviceScheme . '://' . $this->serviceDomain . '/' . rawurlencode($resource); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Check package configuration |
248
|
|
|
* |
249
|
|
|
* @return boolean |
250
|
|
|
*/ |
251
|
|
|
protected function doCheckSettings() |
252
|
|
|
{ |
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param ClientInterface $client |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
2 |
|
protected function setClient(ClientInterface $client) |
261
|
|
|
{ |
262
|
2 |
|
$this->client = $client; |
|
|
|
|
263
|
|
|
|
264
|
2 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return ClientInterface |
269
|
|
|
*/ |
270
|
4 |
View Code Duplication |
protected function getClient() |
|
|
|
|
271
|
|
|
{ |
272
|
4 |
|
if (is_null($this->client)) { |
273
|
|
|
$defaultOptions = [ |
274
|
3 |
|
'base_uri' => $this->getServiceUrl(), |
275
|
|
|
'headers' => [ |
276
|
3 |
|
'Authorization' => 'OAuth ' . $this->getAccessToken(), |
277
|
3 |
|
'Host' => $this->getServiceDomain(), |
278
|
3 |
|
'User-Agent' => $this->getUserAgent(), |
279
|
|
|
'Accept' => '*/*' |
280
|
3 |
|
] |
281
|
3 |
|
]; |
282
|
3 |
|
if ($this->getProxy()) { |
283
|
|
|
$defaultOptions['proxy'] = $this->getProxy(); |
284
|
|
|
} |
285
|
3 |
|
if ($this->getDebug()) { |
286
|
|
|
$defaultOptions['debug'] = $this->getDebug(); |
287
|
|
|
} |
288
|
3 |
|
$this->client = new Client($defaultOptions); |
|
|
|
|
289
|
3 |
|
} |
290
|
|
|
|
291
|
4 |
|
return $this->client; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Sends a request |
296
|
|
|
* |
297
|
|
|
* @param string $method HTTP method |
298
|
|
|
* @param string|UriInterface $uri URI object or string. |
299
|
|
|
* @param array $options Request options to apply. |
300
|
|
|
* |
301
|
|
|
* @throws Exception\MissedArgumentException |
302
|
|
|
* @throws Exception\ProfileNotFoundException |
303
|
|
|
* @throws Exception\YandexException |
304
|
|
|
* @return Response |
305
|
|
|
*/ |
306
|
8 |
|
protected function sendRequest($method, $uri, array $options = []) |
307
|
|
|
{ |
308
|
|
|
try { |
309
|
8 |
|
$response = $this->getClient()->request($method, $uri, $options); |
|
|
|
|
310
|
8 |
|
} catch (ClientException $ex) { |
311
|
|
|
// get error from response |
312
|
|
|
$decodedResponseBody = $this->getDecodedBody($ex->getResponse()->getBody()); |
313
|
|
|
$code = $ex->getResponse()->getStatusCode(); |
314
|
|
|
|
315
|
|
|
// handle a service error message |
316
|
|
|
if (is_array($decodedResponseBody) |
317
|
|
|
&& isset($decodedResponseBody['error'], $decodedResponseBody['message']) |
318
|
|
|
) { |
319
|
|
|
switch ($decodedResponseBody['error']) { |
320
|
|
|
case 'MissedRequiredArguments': |
321
|
|
|
throw new MissedArgumentException($decodedResponseBody['message']); |
322
|
|
|
case 'AssistantProfileNotFound': |
323
|
|
|
throw new ProfileNotFoundException($decodedResponseBody['message']); |
324
|
|
|
default: |
325
|
|
|
throw new YandexException($decodedResponseBody['message'], $code); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// unknown error |
330
|
|
|
throw $ex; |
331
|
|
|
} |
332
|
|
|
|
333
|
8 |
|
return $response; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param $body |
338
|
|
|
* @param string $type |
339
|
|
|
* @return mixed|\SimpleXMLElement |
340
|
|
|
*/ |
341
|
90 |
|
protected function getDecodedBody($body, $type = null) |
342
|
|
|
{ |
343
|
90 |
|
if (!isset($type)) { |
344
|
90 |
|
$type = static::DECODE_TYPE_DEFAULT; |
345
|
90 |
|
} |
346
|
|
|
switch ($type) { |
347
|
90 |
|
case self::DECODE_TYPE_XML: |
348
|
14 |
|
return simplexml_load_string((string) $body); |
349
|
76 |
|
case self::DECODE_TYPE_JSON: |
350
|
76 |
|
default: |
351
|
76 |
|
return json_decode((string) $body, true); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
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..