|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Audiens\AdForm\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Audiens\AdForm\Cache\CacheInterface; |
|
6
|
|
|
use Audiens\AdForm\Entity\Agency; |
|
7
|
|
|
use Audiens\AdForm\Entity\AgencyHydrator; |
|
8
|
|
|
use Audiens\AdForm\Exception\ApiException; |
|
9
|
|
|
use Audiens\AdForm\Exception\EntityNotFoundException; |
|
10
|
|
|
use Audiens\AdForm\HttpClient; |
|
11
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
class AgencyManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var HttpClient */ |
|
17
|
|
|
protected $httpClient; |
|
18
|
|
|
|
|
19
|
|
|
/** @var CacheInterface */ |
|
20
|
|
|
protected $cache; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $cachePrefix = 'agency'; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(HttpClient $httpClient, CacheInterface $cache = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->httpClient = $httpClient; |
|
28
|
|
|
$this->cache = $cache; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return an agency based on ID |
|
33
|
|
|
* |
|
34
|
|
|
* @param int $agencyId ID of the agency |
|
35
|
|
|
* |
|
36
|
|
|
* @throws EntityNotFoundException if the API call fails |
|
37
|
|
|
* @throws RuntimeException if the response is null |
|
38
|
|
|
* |
|
39
|
|
|
* @return Agency |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getItem(int $agencyId): Agency |
|
42
|
|
|
{ |
|
43
|
|
|
// Endpoint URI |
|
44
|
|
|
$uri = sprintf('v1/agencies/%d', $agencyId); |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
$data = null; |
|
48
|
|
|
|
|
49
|
|
|
// try to get from cache |
|
50
|
|
|
if ($this->cache) { |
|
51
|
|
|
$data = $this->cache->get($this->cachePrefix, $uri, []); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// load from API |
|
55
|
|
|
if (!$data) { |
|
56
|
|
|
$data = $this->httpClient->get($uri)->getBody()->getContents(); |
|
57
|
|
|
|
|
58
|
|
|
if ($this->cache && $data) { |
|
59
|
|
|
$this->cache->put($this->cachePrefix, $uri, [], $data); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return AgencyHydrator::fromStdClass(json_decode($data)); |
|
64
|
|
|
} catch (ClientException $e) { |
|
65
|
|
|
$response = $e->getResponse(); |
|
66
|
|
|
if ($response === null) { |
|
67
|
|
|
throw new RuntimeException('Null response'); |
|
68
|
|
|
} |
|
69
|
|
|
$responseBody = $response->getBody()->getContents(); |
|
70
|
|
|
$responseCode = $response->getStatusCode(); |
|
71
|
|
|
|
|
72
|
|
|
throw EntityNotFoundException::translate($agencyId, $responseBody, $responseCode); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns an array of agencies |
|
78
|
|
|
* |
|
79
|
|
|
* @param int $limit |
|
80
|
|
|
* @param int $offset |
|
81
|
|
|
* @param bool $active |
|
82
|
|
|
* @param int $countryId |
|
83
|
|
|
* |
|
84
|
|
|
* @throws ApiException if the API call fails |
|
85
|
|
|
* @throws RuntimeException if the response is null |
|
86
|
|
|
* |
|
87
|
|
|
* @return Agency[] |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getItems(int $limit = 1000, int $offset = 0, ?bool $active = null, ?int $countryId = null): array |
|
90
|
|
|
{ |
|
91
|
|
|
// Endpoint URI |
|
92
|
|
|
$uri = 'v1/agencies'; |
|
93
|
|
|
|
|
94
|
|
|
$options = [ |
|
95
|
|
|
'query' => [ |
|
96
|
|
|
'limit' => $limit, |
|
97
|
|
|
'offset' => $offset, |
|
98
|
|
|
'active' => $active, |
|
99
|
|
|
'countryId' => $countryId |
|
100
|
|
|
], |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$agencies = []; |
|
105
|
|
|
|
|
106
|
|
|
try { |
|
107
|
|
|
$data = null; |
|
108
|
|
|
|
|
109
|
|
|
// try to get from cache |
|
110
|
|
|
if ($this->cache) { |
|
111
|
|
|
$data = $this->cache->get($this->cachePrefix, $uri, $options); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// load from API |
|
115
|
|
|
if (!$data) { |
|
116
|
|
|
$data = $this->httpClient->get($uri, $options)->getBody()->getContents(); |
|
117
|
|
|
|
|
118
|
|
|
if ($this->cache && $data) { |
|
119
|
|
|
$this->cache->put($this->cachePrefix, $uri, $options, $data); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$classArray = \json_decode($data); |
|
124
|
|
|
|
|
125
|
|
|
foreach ($classArray as $class) { |
|
126
|
|
|
$agencies[] = AgencyHydrator::fromStdClass($class); |
|
127
|
|
|
} |
|
128
|
|
|
} catch (ClientException $e) { |
|
129
|
|
|
$response = $e->getResponse(); |
|
130
|
|
|
if ($response === null) { |
|
131
|
|
|
throw new RuntimeException('Null response'); |
|
132
|
|
|
} |
|
133
|
|
|
$responseBody = $response->getBody()->getContents(); |
|
134
|
|
|
$responseCode = $response->getStatusCode(); |
|
135
|
|
|
|
|
136
|
|
|
throw ApiException::translate($responseBody, $responseCode); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $agencies; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns an array of agencies for a Data Provider |
|
144
|
|
|
* |
|
145
|
|
|
* @param int $dataProviderId |
|
146
|
|
|
* @param int $limit |
|
147
|
|
|
* @param int $offset |
|
148
|
|
|
* @param bool $active |
|
149
|
|
|
* @param int $countryId |
|
150
|
|
|
* |
|
151
|
|
|
* @throws ApiException if the API call fails |
|
152
|
|
|
* @throws RuntimeException if the response is null |
|
153
|
|
|
* |
|
154
|
|
|
* @return Agency[] |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getItemsDataProvider(int $dataProviderId, int $limit = 1000, int $offset = 0, ?bool $active = null, ?int $countryId = null): array |
|
157
|
|
|
{ |
|
158
|
|
|
// Endpoint URI |
|
159
|
|
|
$uri = sprintf('v1/dataproviders/%d/agencies', $dataProviderId); |
|
160
|
|
|
|
|
161
|
|
|
$options = [ |
|
162
|
|
|
'query' => [ |
|
163
|
|
|
'limit' => $limit, |
|
164
|
|
|
'offset' => $offset, |
|
165
|
|
|
'active' => $active, |
|
166
|
|
|
'countryId' => $countryId |
|
167
|
|
|
], |
|
168
|
|
|
]; |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
$agencies = []; |
|
172
|
|
|
|
|
173
|
|
|
try { |
|
174
|
|
|
$data = null; |
|
175
|
|
|
|
|
176
|
|
|
// try to get from cache |
|
177
|
|
|
if ($this->cache) { |
|
178
|
|
|
$data = $this->cache->get($this->cachePrefix, $uri, $options); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
// load from API |
|
182
|
|
|
if (!$data) { |
|
183
|
|
|
$data = $this->httpClient->get($uri, $options)->getBody()->getContents(); |
|
184
|
|
|
|
|
185
|
|
|
if ($this->cache && $data) { |
|
186
|
|
|
$this->cache->put($this->cachePrefix, $uri, $options, $data); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$classArray = \json_decode($data); |
|
191
|
|
|
|
|
192
|
|
|
foreach ($classArray as $class) { |
|
193
|
|
|
$agencies[] = AgencyHydrator::fromStdClass($class); |
|
194
|
|
|
} |
|
195
|
|
|
} catch (ClientException $e) { |
|
196
|
|
|
$response = $e->getResponse(); |
|
197
|
|
|
if ($response === null) { |
|
198
|
|
|
throw new RuntimeException('Null response'); |
|
199
|
|
|
} |
|
200
|
|
|
$responseBody = $response->getBody()->getContents(); |
|
201
|
|
|
$responseCode = $response->getStatusCode(); |
|
202
|
|
|
|
|
203
|
|
|
throw ApiException::translate($responseBody, $responseCode); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $agencies; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|