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\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
|
|
|
use CachableTrait; |
23
|
|
|
|
24
|
|
|
const BASE_URL = 'http://api.adnxs.com/batch-segment'; |
25
|
|
|
|
26
|
|
|
const SANDBOX_BASE_URL = 'http://api-test.adnxs.com/batch-segment'; |
27
|
|
|
|
28
|
|
|
/** @var \SplQueue */ |
29
|
|
|
protected $userSegments; |
30
|
|
|
|
31
|
|
|
/** @var Client|Auth */ |
32
|
|
|
protected $client; |
33
|
|
|
|
34
|
|
|
/** @var int */ |
35
|
|
|
protected $memberId; |
36
|
|
|
|
37
|
|
|
/** @var Cache */ |
38
|
|
|
protected $cache; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $baseUrl; |
42
|
|
|
|
43
|
|
|
const CACHE_NAMESPACE = 'appnexus_segment_user_upload'; |
44
|
|
|
|
45
|
|
|
const CACHE_EXPIRATION = 3600; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* SegmentRepository constructor. |
49
|
|
|
* |
50
|
|
|
* @param ClientInterface $client |
51
|
|
|
* @param Cache|null $cache |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function __construct(ClientInterface $client, Cache $cache = null) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->client = $client; |
|
|
|
|
56
|
|
|
$this->cache = $cache; |
57
|
|
|
$this->cacheEnabled = $cache instanceof Cache; |
58
|
|
|
$this->baseUrl = self::BASE_URL; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getBaseUrl() |
65
|
|
|
{ |
66
|
|
|
return $this->baseUrl; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $baseUrl |
71
|
|
|
*/ |
72
|
|
|
public function setBaseUrl($baseUrl) |
73
|
|
|
{ |
74
|
|
|
$this->baseUrl = $baseUrl; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $memberId |
80
|
|
|
* @param $fileAsString |
81
|
|
|
* |
82
|
|
|
* @return UploadJobStatus |
83
|
|
|
* @throws UploadException |
84
|
|
|
*/ |
85
|
|
|
public function upload($memberId, $fileAsString) |
86
|
|
|
{ |
87
|
|
|
if (empty($fileAsString)) { |
88
|
|
|
throw UploadException::emptyFile(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$tempFile = tmpfile(); |
92
|
|
|
fwrite($tempFile, $fileAsString); |
93
|
|
|
fseek($tempFile, 0); |
94
|
|
|
|
95
|
|
|
$job = $this->getUploadTicket($memberId); |
96
|
|
|
|
97
|
|
|
$response = $this->client->request('POST', $job->getUploadUrl(), ['body' => $tempFile]); |
98
|
|
|
|
99
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
100
|
|
|
|
101
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
102
|
|
|
throw UploadException::failed($repositoryResponse); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->getJobStatus($job); |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $memberId |
111
|
|
|
* |
112
|
|
|
* @return UploadJobStatus |
113
|
|
|
* @throws UploadException |
114
|
|
|
*/ |
115
|
|
|
public function getUploadTicket($memberId) |
116
|
|
|
{ |
117
|
|
|
|
118
|
|
|
$compiledUrl = $this->baseUrl.'?member_id='.$memberId; |
119
|
|
|
|
120
|
|
|
$response = $this->client->request('POST', $compiledUrl); |
121
|
|
|
|
122
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
123
|
|
|
|
124
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
125
|
|
|
throw UploadException::failed($repositoryResponse); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'])) { |
129
|
|
|
throw UploadException::missingIndex('response->batch_segment_upload_job'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$uploadJob = UploadTicket::fromArray( |
133
|
|
|
$repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'] |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
return $uploadJob; |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param UploadTicket $uploadTicket |
142
|
|
|
* |
143
|
|
|
* @return UploadJobStatus |
144
|
|
|
* @throws UploadException |
145
|
|
|
*/ |
146
|
|
|
public function getJobStatus(UploadTicket $uploadTicket) |
147
|
|
|
{ |
148
|
|
|
|
149
|
|
|
$compiledUrl = $this->baseUrl."?member_id={$uploadTicket->getMemberId()}&job_id={$uploadTicket->getJobId()}"; |
150
|
|
|
|
151
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
152
|
|
|
|
153
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
154
|
|
|
|
155
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
156
|
|
|
throw UploadException::failed($repositoryResponse); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0])) { |
160
|
|
|
throw UploadException::missingIndex('response->batch_segment_upload_job->0'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$uploadJobStatus = UploadJobStatus::fromArray( |
164
|
|
|
$repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0] |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
return $uploadJobStatus; |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $memberId |
173
|
|
|
* @param int $start |
174
|
|
|
* @param int $maxResults |
175
|
|
|
* |
176
|
|
|
* @return UploadJobStatus[] |
177
|
|
|
* @throws \Exception |
178
|
|
|
*/ |
179
|
|
|
public function getUploadHistory($memberId, $start = 0, $maxResults = 100) |
180
|
|
|
{ |
181
|
|
|
|
182
|
|
|
$compiledUrl = $this->baseUrl."?member_id=$memberId&start_element=$start&num_elements=$maxResults"; |
183
|
|
|
|
184
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
185
|
|
|
|
186
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
187
|
|
|
|
188
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
189
|
|
|
throw UploadException::failed($repositoryResponse); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0])) { |
193
|
|
|
throw UploadException::missingIndex('response->batch_segment_upload_job->0'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$uploadStatuses = []; |
197
|
|
|
|
198
|
|
|
$responseAsArray = $repositoryResponse->getResponseAsArray(); |
199
|
|
|
|
200
|
|
|
foreach ($responseAsArray['response']['batch_segment_upload_job'] as $response) { |
201
|
|
|
$uploadStatuses[] = UploadJobStatus::fromArray($response); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $uploadStatuses; |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.