Completed
Push — master ( 3d7154...87dec9 )
by Hector
05:10 queued 02:26
created

TailoredAudience::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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.

Loading history...
237
    {
238
        foreach (self::getTypes() as $allowedType) {
239
            if ($type === $allowedType) {
240
                return $type;
241
            }
242
        }
243
244
        throw new InvalidType(
245
            sprintf('"%s" is not a valid type for %s', $type, TailoredAudience::class)
246
        );
247
    }
248
249
    /**
250
     * @return array
251
     */
252
    public function getProperties()
253
    {
254
        return $this->properties;
255
    }
256
257
    /**
258
     * @return mixed
259
     */
260
    public function getMetadata()
261
    {
262
        return $this->metadata;
263
    }
264
}
265