|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Audiens\AppnexusClient\repository; |
|
4
|
|
|
|
|
5
|
|
|
use Audiens\AppnexusClient\CachableTrait; |
|
6
|
|
|
use Audiens\AppnexusClient\CacheableInterface; |
|
7
|
|
|
use Audiens\AppnexusClient\entity\MemberDataSharing; |
|
8
|
|
|
use Audiens\AppnexusClient\entity\MemberDataSharingSegment; |
|
9
|
|
|
use Audiens\AppnexusClient\exceptions\RepositoryException; |
|
10
|
|
|
use Doctrine\Common\Cache\Cache; |
|
11
|
|
|
use GuzzleHttp\ClientInterface; |
|
12
|
|
|
|
|
13
|
|
|
class MemberDataSharingRepository implements CacheableInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use CachableTrait; |
|
16
|
|
|
|
|
17
|
|
|
public const BASE_URL = 'https://api.adnxs.com/member-data-sharing/'; |
|
18
|
|
|
public const SANDBOX_BASE_URL = 'http://api-test.adnxs.com/member-data-sharing/'; |
|
19
|
|
|
public const CACHE_NAMESPACE = 'appnexus_member_data_sharing_find_all'; |
|
20
|
|
|
public const CACHE_EXPIRATION = 3600; |
|
21
|
|
|
|
|
22
|
|
|
/** @var ClientInterface */ |
|
23
|
|
|
protected $client; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int */ |
|
26
|
|
|
protected $memberId; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Cache */ |
|
29
|
|
|
protected $cache; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $baseUrl; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ClientInterface $client |
|
36
|
|
|
* @param Cache $cache |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ClientInterface $client, Cache $cache) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->client = $client; |
|
41
|
|
|
$this->cache = $cache; |
|
42
|
|
|
$this->baseUrl = self::BASE_URL; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getBaseUrl() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->baseUrl; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $baseUrl |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setBaseUrl($baseUrl) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->baseUrl = $baseUrl; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int $start |
|
63
|
|
|
* @param int $maxResults |
|
64
|
|
|
* |
|
65
|
|
|
* @return array|MemberDataSharing[] |
|
66
|
|
|
* @throws RepositoryException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function findAll($start = 0, $maxResults = 100) |
|
69
|
|
|
{ |
|
70
|
|
|
$cacheKey = self::CACHE_NAMESPACE.sha1($start.$maxResults); |
|
71
|
|
|
|
|
72
|
|
|
if ($this->cache->contains($cacheKey)) { |
|
73
|
|
|
return $this->cache->fetch($cacheKey); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$compiledUrl = $this->baseUrl."?start_element=$start&num_elements=$maxResults"; |
|
77
|
|
|
|
|
78
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
|
79
|
|
|
|
|
80
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
81
|
|
|
|
|
82
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
|
83
|
|
|
throw RepositoryException::failed($repositoryResponse); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$stream = $response->getBody(); |
|
87
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
|
88
|
|
|
$stream->rewind(); |
|
89
|
|
|
|
|
90
|
|
|
$result = []; |
|
91
|
|
|
|
|
92
|
|
|
if (!$responseContent['response']['member_data_sharings']) { |
|
93
|
|
|
$responseContent['response']['member_data_sharings'] = []; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
foreach ($responseContent['response']['member_data_sharings'] as $dataArray) { |
|
97
|
|
|
$result[] = MemberDataSharing::fromArray($dataArray); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION); |
|
101
|
|
|
|
|
102
|
|
|
return $result; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param int $buyerId |
|
107
|
|
|
* @param int $memberId |
|
108
|
|
|
* |
|
109
|
|
|
* @return array|MemberDataSharing[] |
|
110
|
|
|
* @throws RepositoryException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function findByBuyerId($buyerId, $memberId) |
|
113
|
|
|
{ |
|
114
|
|
|
$cacheKey = self::CACHE_NAMESPACE.sha1($buyerId.$memberId); |
|
115
|
|
|
|
|
116
|
|
|
if ($this->cache->contains($cacheKey)) { |
|
117
|
|
|
return $this->cache->fetch($cacheKey); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$compiledUrl = $this->baseUrl."?data_member_id=$memberId&buyer_member_id=$buyerId"; |
|
121
|
|
|
|
|
122
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
|
123
|
|
|
|
|
124
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
125
|
|
|
|
|
126
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
|
127
|
|
|
throw RepositoryException::failed($repositoryResponse); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$stream = $response->getBody(); |
|
131
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
|
132
|
|
|
$stream->rewind(); |
|
133
|
|
|
|
|
134
|
|
|
$result = []; |
|
135
|
|
|
|
|
136
|
|
|
if (!$responseContent['response']['member_data_sharings']) { |
|
137
|
|
|
$responseContent['response']['member_data_sharings'] = []; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
foreach ($responseContent['response']['member_data_sharings'] as $dataArray) { |
|
141
|
|
|
$memberDataSharing = MemberDataSharing::fromArray($dataArray); |
|
142
|
|
|
|
|
143
|
|
|
$segments = []; |
|
144
|
|
|
|
|
145
|
|
|
if (!empty($memberDataSharing->getSegments() && count($memberDataSharing->getSegments()) > 0)) { |
|
146
|
|
|
foreach ($memberDataSharing->getSegments() as $segment) { |
|
147
|
|
|
$segments[] = MemberDataSharingSegment::fromArray($segment); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
$memberDataSharing->setSegments($segments); |
|
151
|
|
|
|
|
152
|
|
|
$result[] = $memberDataSharing; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION); |
|
156
|
|
|
|
|
157
|
|
|
return $result; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param int $id |
|
162
|
|
|
* |
|
163
|
|
|
* @return array|MemberDataSharing[] |
|
164
|
|
|
*/ |
|
165
|
|
|
public function findById($id) |
|
166
|
|
|
{ |
|
167
|
|
|
$cacheKey = self::CACHE_NAMESPACE.sha1((string)$id); |
|
168
|
|
|
|
|
169
|
|
|
if ($this->cache->contains($cacheKey)) { |
|
170
|
|
|
return $this->cache->fetch($cacheKey); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$compiledUrl = $this->baseUrl."$id"; |
|
174
|
|
|
|
|
175
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
|
176
|
|
|
|
|
177
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
178
|
|
|
|
|
179
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
|
180
|
|
|
throw RepositoryException::failed($repositoryResponse); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$stream = $response->getBody(); |
|
184
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
|
185
|
|
|
$stream->rewind(); |
|
186
|
|
|
|
|
187
|
|
|
$result = []; |
|
188
|
|
|
|
|
189
|
|
|
if (!$responseContent['response']['member_data_sharing']) { |
|
190
|
|
|
$responseContent['response']['member_data_sharing'] = []; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$memberDataSharing = MemberDataSharing::fromArray($responseContent['response']['member_data_sharing']); |
|
194
|
|
|
|
|
195
|
|
|
$segments = []; |
|
196
|
|
|
|
|
197
|
|
|
if (!empty($memberDataSharing->getSegments() && count($memberDataSharing->getSegments()) > 0)) { |
|
198
|
|
|
foreach ($memberDataSharing->getSegments() as $segment) { |
|
199
|
|
|
$segments[] = MemberDataSharingSegment::fromArray($segment); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
$memberDataSharing->setSegments($segments); |
|
203
|
|
|
|
|
204
|
|
|
$result[] = $memberDataSharing; |
|
205
|
|
|
|
|
206
|
|
|
$this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION); |
|
207
|
|
|
|
|
208
|
|
|
return $result; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param MemberDataSharing $mSharing |
|
213
|
|
|
* |
|
214
|
|
|
* @return RepositoryResponse |
|
215
|
|
|
* @throws RepositoryException |
|
216
|
|
|
*/ |
|
217
|
|
|
public function add(MemberDataSharing $mSharing) |
|
218
|
|
|
{ |
|
219
|
|
|
$compiledUrl = $this->baseUrl; |
|
220
|
|
|
|
|
221
|
|
|
$payload = [ |
|
222
|
|
|
'member_data_sharing' => $mSharing->toArray(), |
|
223
|
|
|
]; |
|
224
|
|
|
|
|
225
|
|
|
if (!empty($mSharing->getSegments())) { |
|
226
|
|
|
$segmentToUpload = []; |
|
227
|
|
|
|
|
228
|
|
|
foreach ($mSharing->getSegments() as $segment) { |
|
229
|
|
|
$segmentToUpload[] = $segment->toArray(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$payload['member_data_sharing']['segments'] = $segmentToUpload; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$response = $this->client->request('POST', $compiledUrl, ['body' => json_encode($payload)]); |
|
236
|
|
|
|
|
237
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
238
|
|
|
|
|
239
|
|
|
if ($repositoryResponse->isSuccessful()) { |
|
240
|
|
|
$stream = $response->getBody(); |
|
241
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
|
242
|
|
|
$stream->rewind(); |
|
243
|
|
|
|
|
244
|
|
|
if (!(isset($responseContent['response']['member_data_sharing']['id']))) { |
|
245
|
|
|
throw RepositoryException::wrongFormat(serialize($responseContent)); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
$mSharing->setId($responseContent['response']['member_data_sharing']['id']); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return $repositoryResponse; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @param int $memberDataSharingId |
|
256
|
|
|
* @param MemberDataSharingSegment[] $segments |
|
257
|
|
|
* |
|
258
|
|
|
* @return RepositoryResponse |
|
259
|
|
|
*/ |
|
260
|
|
|
public function addSegmentsToMemberDataSharing($memberDataSharingId, $segments) |
|
261
|
|
|
{ |
|
262
|
|
|
$compiledUrl = $this->baseUrl.$memberDataSharingId; |
|
263
|
|
|
|
|
264
|
|
|
/** @var MemberDataSharingSegment[] $mdObject */ |
|
265
|
|
|
$mdObjectArray = $this->findById($memberDataSharingId); |
|
266
|
|
|
|
|
267
|
|
|
if (count($mdObjectArray) === 0) { |
|
268
|
|
|
throw new \RuntimeException('Could not find the selected member data sharing'); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
$segmentsOld = $mdObjectArray[0]->getSegments(); |
|
272
|
|
|
|
|
273
|
|
|
$segmentToUpload = []; |
|
274
|
|
|
|
|
275
|
|
|
foreach ($segmentsOld as $sOld) { |
|
276
|
|
|
$segmentToUpload[] = $sOld->toArray(); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
foreach ($segments as $segment) { |
|
280
|
|
|
$segmentToUpload[] = $segment->toArray(); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
$payload = [ |
|
284
|
|
|
'member_data_sharing' => [ |
|
285
|
|
|
'segments' => $segmentToUpload, |
|
286
|
|
|
], |
|
287
|
|
|
]; |
|
288
|
|
|
|
|
289
|
|
|
$response = $this->client->request('PUT', $compiledUrl, ['body' => json_encode($payload)]); |
|
290
|
|
|
|
|
291
|
|
|
return RepositoryResponse::fromResponse($response); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
public function update(MemberDataSharing $memberDataSharing) |
|
295
|
|
|
{ |
|
296
|
|
|
if (!$memberDataSharing->getId()) { |
|
297
|
|
|
throw RepositoryException::missingMemberDataSharingId($memberDataSharing); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
$compiledUrl = $this->baseUrl.'?id='.$memberDataSharing->getId(); |
|
301
|
|
|
|
|
302
|
|
|
$payload = [ |
|
303
|
|
|
'member_data_sharing' => $memberDataSharing->toArray(), |
|
304
|
|
|
]; |
|
305
|
|
|
|
|
306
|
|
|
$response = $this->client->request('PUT', $compiledUrl, ['body' => json_encode($payload)]); |
|
307
|
|
|
|
|
308
|
|
|
return RepositoryResponse::fromResponse($response); |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|