|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
|
6
|
|
|
use Hborras\TwitterAdsSDK\TONUpload; |
|
7
|
|
|
use Hborras\TwitterAdsSDK\TwitterAds; |
|
8
|
|
|
use Hborras\TwitterAdsSDK\TwitterAds\Resource; |
|
9
|
|
|
use Hborras\TwitterAdsSDK\TwitterAdsException; |
|
10
|
|
|
use Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience\Exception\InvalidType; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class TailoredAudience |
|
14
|
|
|
* @package Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience |
|
15
|
|
|
*/ |
|
16
|
|
|
final class TailoredAudience extends Resource |
|
17
|
|
|
{ |
|
18
|
|
|
const RESOURCE_COLLECTION = 'accounts/{account_id}/tailored_audiences'; |
|
19
|
|
|
const RESOURCE = 'accounts/{account_id}/tailored_audiences/{id}'; |
|
20
|
|
|
|
|
21
|
|
|
const LIST_TYPE_EMAIL = 'EMAIL'; |
|
22
|
|
|
const LIST_TYPE_DEVICE_ID = 'DEVICE_ID'; |
|
23
|
|
|
const LIST_TYPE_TWITTER_ID = 'TWITTER_ID'; |
|
24
|
|
|
const LIST_TYPE_HANDLE = 'HANDLE'; |
|
25
|
|
|
const LIST_TYPE_PHONE_NUMBER = 'PHONE_NUMBER'; |
|
26
|
|
|
|
|
27
|
|
|
/** Writable */ |
|
28
|
|
|
protected $list_type; |
|
29
|
|
|
protected $name; |
|
30
|
|
|
|
|
31
|
|
|
protected $properties = [ |
|
32
|
|
|
'name', |
|
33
|
|
|
'list_type', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** Read Only */ |
|
37
|
|
|
protected $deleted; |
|
38
|
|
|
protected $targetable; |
|
39
|
|
|
protected $audience_size; |
|
40
|
|
|
protected $id; |
|
41
|
|
|
protected $updated_at; |
|
42
|
|
|
protected $created_at; |
|
43
|
|
|
protected $audience_type; |
|
44
|
|
|
protected $reasons_not_targetable; |
|
45
|
|
|
protected $targetable_types; |
|
46
|
|
|
protected $partner_source; |
|
47
|
|
|
protected $metadata; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Uploads and creates a new tailored audience |
|
51
|
|
|
* |
|
52
|
|
|
* @param $filePath |
|
53
|
|
|
* @param $name |
|
54
|
|
|
* @param $listType |
|
55
|
|
|
* @return Resource |
|
56
|
|
|
* @throws TwitterAds\Errors\BadRequest |
|
57
|
|
|
* @throws TwitterAds\Errors\Forbidden |
|
58
|
|
|
* @throws TwitterAds\Errors\NotAuthorized |
|
59
|
|
|
* @throws TwitterAds\Errors\NotFound |
|
60
|
|
|
* @throws TwitterAds\Errors\RateLimit |
|
61
|
|
|
* @throws TwitterAds\Errors\ServerError |
|
62
|
|
|
* @throws TwitterAds\Errors\ServiceUnavailable |
|
63
|
|
|
* @throws TwitterAdsException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function create($filePath, $name, $listType) |
|
66
|
|
|
{ |
|
67
|
|
|
$upload = new TONUpload($this->getTwitterAds(), $filePath); |
|
68
|
|
|
|
|
69
|
|
|
$this->createAudience($name, $listType); |
|
70
|
|
|
$location = $upload->perform(); |
|
71
|
|
|
$tailoredAudienceChange = new TailoredAudienceChanges(); |
|
72
|
|
|
$tailoredAudienceChange->updateAudience($this->getId(), $location, $listType, TailoredAudienceChanges::ADD); |
|
73
|
|
|
|
|
74
|
|
|
return $this->reload(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Create a simple tailored audience object |
|
79
|
|
|
* |
|
80
|
|
|
* @param $name |
|
81
|
|
|
* @param $listType |
|
82
|
|
|
* @return $this |
|
83
|
|
|
* @throws TwitterAds\Errors\BadRequest |
|
84
|
|
|
* @throws TwitterAds\Errors\Forbidden |
|
85
|
|
|
* @throws TwitterAds\Errors\NotAuthorized |
|
86
|
|
|
* @throws TwitterAds\Errors\NotFound |
|
87
|
|
|
* @throws TwitterAds\Errors\RateLimit |
|
88
|
|
|
* @throws TwitterAds\Errors\ServerError |
|
89
|
|
|
* @throws TwitterAds\Errors\ServiceUnavailable |
|
90
|
|
|
* @throws TwitterAdsException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function createAudience($name, $listType) |
|
93
|
|
|
{ |
|
94
|
|
|
$params = ['name' => $name, 'list_type' => $listType]; |
|
95
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_COLLECTION); |
|
96
|
|
|
$response = $this->getTwitterAds()->post($resource, $params); |
|
97
|
|
|
|
|
98
|
|
|
return $this->fromResponse($response->getBody()->data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the TailoredAudienceChange with the status |
|
103
|
|
|
* @return null |
|
104
|
|
|
*/ |
|
105
|
|
|
public function status() |
|
106
|
|
|
{ |
|
107
|
|
|
$tailoredAudienceChange = new TailoredAudienceChanges(); |
|
108
|
|
|
return $tailoredAudienceChange->status($this->getId()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
public function isDeleted() |
|
115
|
|
|
{ |
|
116
|
|
|
return filter_var($this->deleted, FILTER_VALIDATE_BOOLEAN); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return boolean |
|
121
|
|
|
*/ |
|
122
|
|
|
public function isTargetable() |
|
123
|
|
|
{ |
|
124
|
|
|
return filter_var($this->targetable, FILTER_VALIDATE_BOOLEAN); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return integer |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getAudienceSize() |
|
131
|
|
|
{ |
|
132
|
|
|
return (int)$this->audience_size; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getId() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->id; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return DateTimeImmutable |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getCreatedAt() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->created_at; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return DateTimeImmutable |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getUpdatedAt() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->updated_at; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return mixed |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getListType() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->assureValidType($this->list_type); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $type |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setListType($type) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->list_type = $this->assureValidType($type); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getAudienceType() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->audience_type; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getReasonsNotTargetable() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->reasons_not_targetable; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getTargetableTypes() |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->targetable_types; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getName() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->name; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param string $name |
|
209
|
|
|
*/ |
|
210
|
|
|
public function setName($name) |
|
211
|
|
|
{ |
|
212
|
|
|
$this->name = $name; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return string |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getPartnerSource() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->partner_source; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
|
|
public static function getTypes() |
|
227
|
|
|
{ |
|
228
|
|
|
return [ |
|
229
|
|
|
self::LIST_TYPE_DEVICE_ID, |
|
230
|
|
|
self::LIST_TYPE_EMAIL, |
|
231
|
|
|
self::LIST_TYPE_HANDLE, |
|
232
|
|
|
self::LIST_TYPE_PHONE_NUMBER, |
|
233
|
|
|
self::LIST_TYPE_TWITTER_ID, |
|
234
|
|
|
]; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Asserts that the given type is valid |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $type |
|
241
|
|
|
* @throws InvalidType - if type is invalid or null |
|
242
|
|
|
* |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
private function assureValidType($type) |
|
246
|
|
|
{ |
|
247
|
|
|
foreach (self::getTypes() as $allowedType) { |
|
248
|
|
|
if ($type === $allowedType) { |
|
249
|
|
|
return $type; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
throw new InvalidType( |
|
254
|
|
|
sprintf('"%s" is not a valid type for %s', $type, TailoredAudience::class) |
|
255
|
|
|
); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @return mixed |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getMetadata() |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->metadata; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|