1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\AppnexusClient\repository; |
4
|
|
|
|
5
|
|
|
use Audiens\AppnexusClient\Auth; |
6
|
|
|
use Audiens\AppnexusClient\entity\UserSegment; |
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\ClientInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class UserSegmentRepository |
13
|
|
|
*/ |
14
|
|
|
class UserSegmentRepository |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
const BASE_URL = 'http://api.appnexus.com/batch-segment'; |
18
|
|
|
// const BASE_URL_ADNXS = 'http://sand.api.appnexus.com/batch-segment'; |
19
|
|
|
|
20
|
|
|
/** @var \SplQueue */ |
21
|
|
|
protected $userSegments; |
22
|
|
|
|
23
|
|
|
/** @var Client|Auth */ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** @var int */ |
27
|
|
|
protected $memberId; |
28
|
|
|
|
29
|
|
|
/** @var Cache */ |
30
|
|
|
protected $cache; |
31
|
|
|
|
32
|
|
|
/** @var bool */ |
33
|
|
|
protected $cacheEnabled; |
34
|
|
|
|
35
|
|
|
const CACHE_NAMESPACE = 'appnexus_segment_user_upload'; |
36
|
|
|
|
37
|
|
|
const CACHE_EXPIRATION = 3600; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SegmentRepository constructor. |
41
|
|
|
* |
42
|
|
|
* @param ClientInterface $client |
43
|
|
|
* @param Cache|null $cache |
44
|
|
|
*/ |
45
|
|
|
public function __construct(ClientInterface $client, Cache $cache = null) |
46
|
|
|
{ |
47
|
|
|
$this->client = $client; |
|
|
|
|
48
|
|
|
$this->cache = $cache; |
49
|
|
|
$this->cacheEnabled = $cache instanceof Cache; |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $fileAsString |
56
|
|
|
* @param $memberId |
57
|
|
|
* |
58
|
|
|
* @return RepositoryResponse |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function upload($fileAsString,$memberId){ |
62
|
|
|
|
63
|
|
|
$tempFile = tmpfile(); |
64
|
|
|
|
65
|
|
|
fwrite($tempFile, $fileAsString); |
66
|
|
|
fseek($tempFile, 0); |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
$url = $this->getUploadUrl($memberId); |
70
|
|
|
|
71
|
|
|
$contentPayload = [ |
|
|
|
|
72
|
|
|
# Content-Disposition: form-data; name="file"; filename="/the/full/path/of/image/file/you/want/to/upload.png" |
73
|
|
|
'Content-Disposition' => sprintf('form-data; name="%s"; filename="%s"', 'file', $myFile), |
|
|
|
|
74
|
|
|
# Content-Type: application/octet-stream |
75
|
|
|
# 'Content-Type' => 'application/octet-stream' |
|
|
|
|
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$response = $this->client->request('PUT', $url, ['body' => $tempFile]); |
79
|
|
|
|
80
|
|
|
$stream = $response->getBody(); |
81
|
|
|
|
82
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
print_r($responseContent);die(); |
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
fclose($tempFile); // this removes the file |
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $memberId |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
|
|
public function getUploadUrl($memberId) |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
$compiledUrl = self::BASE_URL.'?member_id='.$memberId; |
107
|
|
|
|
108
|
|
|
$response = $this->client->request('POST', $compiledUrl); |
109
|
|
|
|
110
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
|
|
|
|
111
|
|
|
|
112
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
113
|
|
|
throw new \Exception('name me'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!isset($responseContent['response']['batch_segment_upload_job']['upload_url'])) { |
|
|
|
|
117
|
|
|
throw new \Exception('name me'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$stream = $response->getBody(); |
121
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
122
|
|
|
$stream->rewind(); |
123
|
|
|
|
124
|
|
|
return $responseContent['response']['batch_segment_upload_job']['upload_url']; |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
|
|
public function isCacheEnabled() |
133
|
|
|
{ |
134
|
|
|
return $this->cacheEnabled; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function disableCache() |
138
|
|
|
{ |
139
|
|
|
$this->cacheEnabled = false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function enableCache() |
143
|
|
|
{ |
144
|
|
|
$this->cacheEnabled = true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
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.