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