1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\AppnexusClient\service; |
4
|
|
|
|
5
|
|
|
use Audiens\AppnexusClient\Auth; |
6
|
|
|
use Audiens\AppnexusClient\CacheableInterface; |
7
|
|
|
use Audiens\AppnexusClient\entity\UploadJob; |
8
|
|
|
use Audiens\AppnexusClient\entity\UploadTicket; |
9
|
|
|
use Audiens\AppnexusClient\entity\UploadJobStatus; |
10
|
|
|
use Audiens\AppnexusClient\exceptions\UploadException; |
11
|
|
|
use Audiens\AppnexusClient\repository\RepositoryResponse; |
12
|
|
|
use Doctrine\Common\Cache\Cache; |
13
|
|
|
use GuzzleHttp\Client; |
14
|
|
|
use GuzzleHttp\ClientInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class UserSegmentRepository |
18
|
|
|
*/ |
19
|
|
|
class UserUpload implements CacheableInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
const BASE_URL = 'http://api.adnxs.com/batch-segment'; |
23
|
|
|
|
24
|
|
|
/** @var \SplQueue */ |
25
|
|
|
protected $userSegments; |
26
|
|
|
|
27
|
|
|
/** @var Client|Auth */ |
28
|
|
|
protected $client; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
protected $memberId; |
32
|
|
|
|
33
|
|
|
/** @var Cache */ |
34
|
|
|
protected $cache; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
protected $cacheEnabled; |
38
|
|
|
|
39
|
|
|
const CACHE_NAMESPACE = 'appnexus_segment_user_upload'; |
40
|
|
|
|
41
|
|
|
const CACHE_EXPIRATION = 3600; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* SegmentRepository constructor. |
45
|
|
|
* |
46
|
|
|
* @param ClientInterface $client |
47
|
|
|
* @param Cache|null $cache |
48
|
|
|
*/ |
49
|
|
|
public function __construct(ClientInterface $client, Cache $cache = null) |
50
|
|
|
{ |
51
|
|
|
$this->client = $client; |
|
|
|
|
52
|
|
|
$this->cache = $cache; |
53
|
|
|
$this->cacheEnabled = $cache instanceof Cache; |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $fileAsString |
59
|
|
|
* @param $memberId |
60
|
|
|
* |
61
|
|
|
* @return UploadJobStatus |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
public function upload($memberId, $fileAsString) |
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
$tempFile = tmpfile(); |
68
|
|
|
fwrite($tempFile, $fileAsString); |
69
|
|
|
fseek($tempFile, 0); |
70
|
|
|
|
71
|
|
|
$job = $this->getUploadTicket($memberId); |
72
|
|
|
|
73
|
|
|
$response = $this->client->request('POST', $job->getUploadUrl(), ['body' => $tempFile]); |
74
|
|
|
|
75
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
78
|
|
|
throw UploadException::failed($repositoryResponse); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->getJobStatus($job); |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $memberId |
87
|
|
|
* |
88
|
|
|
* @return UploadTicket |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
public function getUploadTicket($memberId) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
$compiledUrl = self::BASE_URL.'?member_id='.$memberId; |
95
|
|
|
|
96
|
|
|
$response = $this->client->request('POST', $compiledUrl); |
97
|
|
|
|
98
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
99
|
|
|
|
100
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
101
|
|
|
throw UploadException::failed($repositoryResponse); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'])) { |
105
|
|
|
throw UploadException::missingIndex('response->batch_segment_upload_job'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$uploadJob = UploadTicket::fromArray( |
109
|
|
|
$repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'] |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
return $uploadJob; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param UploadTicket $uploadTicket |
118
|
|
|
* |
119
|
|
|
* @return UploadJob $uploadJob |
120
|
|
|
* @throws \Exception |
121
|
|
|
*/ |
122
|
|
|
public function getJobStatus(UploadTicket $uploadTicket) |
123
|
|
|
{ |
124
|
|
|
|
125
|
|
|
$compiledUrl = self::BASE_URL."?member_id={$uploadTicket->getMemberId()}&job_id={$uploadTicket->getJobId()}"; |
126
|
|
|
|
127
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
128
|
|
|
|
129
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
130
|
|
|
|
131
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
132
|
|
|
throw UploadException::failed($repositoryResponse); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0])) { |
136
|
|
|
throw UploadException::missingIndex('response->batch_segment_upload_job->0'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$uploadJobStatus = UploadJobStatus::fromArray( |
140
|
|
|
$repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0] |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return $uploadJobStatus; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $memberId |
150
|
|
|
* @param int $start |
151
|
|
|
* @param int $maxResults |
152
|
|
|
* |
153
|
|
|
* @return UploadJobStatus[] |
154
|
|
|
* @throws \Exception |
155
|
|
|
*/ |
156
|
|
|
public function getUploadHistory($memberId, $start = 0, $maxResults = 100) |
157
|
|
|
{ |
158
|
|
|
|
159
|
|
|
$compiledUrl = self::BASE_URL."?member_id=$memberId&start_element=$start&num_elements=$maxResults"; |
160
|
|
|
|
161
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
162
|
|
|
|
163
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
164
|
|
|
|
165
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
166
|
|
|
throw UploadException::failed($repositoryResponse); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0])) { |
170
|
|
|
throw UploadException::missingIndex('response->batch_segment_upload_job->0'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$uploadStatuses = []; |
174
|
|
|
|
175
|
|
|
$responseAsArray = $repositoryResponse->getResponseAsArray(); |
176
|
|
|
|
177
|
|
|
foreach ($responseAsArray['response']['batch_segment_upload_job'] as $response) { |
178
|
|
|
$uploadStatuses[] = UploadJobStatus::fromArray($response); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $uploadStatuses; |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return boolean |
187
|
|
|
*/ |
188
|
|
|
public function isCacheEnabled() |
189
|
|
|
{ |
190
|
|
|
return $this->cacheEnabled; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function disableCache() |
194
|
|
|
{ |
195
|
|
|
$this->cacheEnabled = false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function enableCache() |
199
|
|
|
{ |
200
|
|
|
$this->cacheEnabled = true; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
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.