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