1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Itmedia\ZippyBusBundle\Client; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use GuzzleHttp\Exception\ServerException; |
10
|
|
|
use Itmedia\ZippyBusBundle\Exception\HttpBadRequestException; |
11
|
|
|
use Itmedia\ZippyBusBundle\Exception\HttpForbiddenException; |
12
|
|
|
use Itmedia\ZippyBusBundle\Exception\HttpServerErrorException; |
13
|
|
|
use Itmedia\ZippyBusBundle\Exception\HttpUnauthorizedException; |
14
|
|
|
use Itmedia\ZippyBusBundle\Exception\ZippyBusException; |
15
|
|
|
use Psr\SimpleCache\CacheInterface; |
16
|
|
|
|
17
|
|
|
class ZippyBusClient |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $apiUrl = 'https://zippybus.com/api/v1/'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CacheInterface |
27
|
|
|
*/ |
28
|
|
|
private $cache; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Client |
32
|
|
|
*/ |
33
|
|
|
private $httpClient; |
34
|
|
|
|
35
|
|
|
private $cacheTtl; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function __construct(string $token, CacheInterface $cache, int $cacheTtl = 3600) |
39
|
|
|
{ |
40
|
|
|
$this->cache = $cache; |
41
|
|
|
$this->cacheTtl = $cacheTtl; |
42
|
|
|
|
43
|
|
|
$this->httpClient = new Client([ |
44
|
|
|
'base_uri' => $this->apiUrl, |
45
|
|
|
'timeout' => 5, |
46
|
|
|
'headers' => [ |
47
|
|
|
'Authorization' => sprintf('Bearer %s', $token) |
48
|
|
|
] |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Запрос к сервису |
55
|
|
|
* |
56
|
|
|
* @param string $path Путь к ресурсу (без `https://zippybus.com/api/v1/`) |
57
|
|
|
* @param array $params Параметры автоподстановки пути, вида `['id' => 'value']`, путь должен быть вида `/resource/{id}` |
58
|
|
|
* @param array $options Опции GuzzleClient |
59
|
|
|
* @return array |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
|
|
public function get(string $path, array $params = [], array $options = []): array |
63
|
|
|
{ |
64
|
|
|
$cacheKey = md5(sprintf('%s.%s.%s', $path, \json_encode($params), \json_encode($options))); |
65
|
|
|
|
66
|
|
|
$content = $this->cache->get($cacheKey); |
67
|
|
|
if ($content) { |
68
|
|
|
return $content; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$client = $this->httpClient; |
72
|
|
|
|
73
|
|
|
foreach ($params as $param => $value) { |
74
|
|
|
$path = str_replace('{' . $param . '}', $value, $path); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
$response = $client->get($path, $options); |
79
|
|
|
$content = \GuzzleHttp\json_decode((string)$response->getBody(), true); |
80
|
|
|
$this->cache->set($cacheKey, $content, $this->cacheTtl); |
81
|
|
|
return $content; |
|
|
|
|
82
|
|
|
} catch (\Exception $exception) { |
83
|
|
|
throw $this->handleException($exception); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function handleException(\Exception $exception): \Throwable |
89
|
|
|
{ |
90
|
|
|
if ($exception instanceof ClientException) { |
91
|
|
|
switch ($exception->getCode()) { |
92
|
|
|
case 401: |
93
|
|
|
return new HttpUnauthorizedException('Unauthorized', 401, $exception); |
94
|
|
|
case 403: |
95
|
|
|
return new HttpForbiddenException('Forbidden', 403, $exception); |
96
|
|
|
default: |
97
|
|
|
return new HttpBadRequestException($exception->getMessage(), $exception->getCode(), $exception); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
if ($exception instanceof ServerException) { |
101
|
|
|
return new HttpServerErrorException($exception->getMessage(), $exception->getCode(), $exception); |
102
|
|
|
} |
103
|
|
|
return new ZippyBusException($exception->getMessage(), $exception->getCode(), $exception); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|