Completed
Pull Request — master (#8)
by
unknown
03:08
created

TailoredAudience::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience;
4
5
use Hborras\TwitterAdsSDK\TwitterAds;
6
use Hborras\TwitterAdsSDK\TwitterAds\Account;
7
use Hborras\TwitterAdsSDK\TwitterAds\Resource;
8
use Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience\Exception\InvalidType;
9
10
final class TailoredAudience extends Resource
11
{
12
    const RESOURCE_COLLECTION = 'accounts/{account_id}/tailored_audiences';
13
    const RESOURCE = 'accounts/{account_id}/tailored_audiences/{id}';
14
15
    const LIST_TYPE_EMAIL = 'EMAIL';
16
    const LIST_TYPE_DEVICE_ID = 'DEVICE_ID';
17
    const LIST_TYPE_TWITTER_ID = 'TWITTER_ID';
18
    const LIST_TYPE_HANDLE = 'HANDLE';
19
    const LIST_TYPE_PHONE_NUMBER = 'PHONE_NUMBER';
20
21
    /** Writable */
22
    protected $list_type;
23
    protected $name;
24
25
    protected $properties = [
26
        'name',
27
        'list_type',
28
    ];
29
30
    /** Read Only */
31
    protected $deleted;
32
    protected $targetable;
33
    protected $audience_size;
34
    protected $id;
35
    protected $updated_at;
36
    protected $created_at;
37
    protected $audience_type;
38
    protected $reasons_not_targetable;
39
    protected $targetable_types;
40
    protected $partner_source;
41
42
    public function __construct(Account $account=null, $id=null)
43
    {
44
        parent::__construct($account);
45
        $this->id = $id;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function delete()
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...
52
    {
53
        $resource = str_replace(static::RESOURCE_REPLACE, $this->getAccount()->getId(), static::RESOURCE);
54
        $resource = str_replace(static::RESOURCE_ID_REPLACE, $this->getId(), $resource);
55
        $request = $this->getAccount()->getTwitterAds()->delete($resource, $this->toParams());
56
        $this->fromResponse($request->data);
57
    }
58
59
    /**
60
     * @return boolean
61
     */
62
    public function isDeleted()
63
    {
64
        return filter_var($this->deleted, FILTER_VALIDATE_BOOLEAN);
65
    }
66
67
    /**
68
     * @return boolean
69
     */
70
    public function isTargetable()
71
    {
72
        return filter_var($this->targetable, FILTER_VALIDATE_BOOLEAN);
73
    }
74
75
    /**
76
     * @return integer
77
     */
78
    public function getAudienceSize()
79
    {
80
        return intval($this->audience_size);
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86
    public function getId()
87
    {
88
        return $this->id;
89
    }
90
91
    /**
92
     * @return \DateTimeImmutable
93
     */
94
    public function getCreatedAt()
95
    {
96
        return $this->created_at;
97
    }
98
99
    /**
100
     * @return \DateTimeImmutable
101
     */
102
    public function getUpdatedAt()
103
    {
104
        return $this->updated_at;
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110
    public function getListType()
111
    {
112
        return $this->assureValidType($this->list_type);
113
    }
114
115
    /**
116
     * @param string $type
117
     */
118
    public function setListType($type)
119
    {
120
        $this->list_type = $this->assureValidType($type);
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getAudienceType()
127
    {
128
        return $this->audience_type;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getReasonsNotTargetable()
135
    {
136
        return $this->reasons_not_targetable;
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function getTargetableTypes()
143
    {
144
        return $this->targetable_types;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getName()
151
    {
152
        return $this->name;
153
    }
154
155
    /**
156
     * @param string $name
157
     */
158
    public function setName($name)
159
    {
160
        $this->name = $name;
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getPartnerSource()
167
    {
168
        return $this->partner_source;
169
    }
170
171
    /**
172
     * @return array
173
     */
174
    public static function getTypes()
175
    {
176
        return [
177
            self::LIST_TYPE_DEVICE_ID,
178
            self::LIST_TYPE_EMAIL,
179
            self::LIST_TYPE_HANDLE,
180
            self::LIST_TYPE_PHONE_NUMBER,
181
            self::LIST_TYPE_TWITTER_ID,
182
        ];
183
    }
184
185
    /**
186
     * Asserts that the given type is valid
187
     *
188
     * @param string $type
189
     * @throws InvalidType - if type is invalid or null
190
     *
191
     * @return string
192
     */
193 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...
194
    {
195
        foreach (self::getTypes() as $allowedType) {
196
            if ($type === $allowedType) {
197
                return $type;
198
            }
199
        }
200
201
        throw new InvalidType(
202
            sprintf('"%s" is not a valid type for %s', $type, TailoredAudience::class)
203
        );
204
    }
205
206
    /**
207
     * @return array
208
     */
209
    public function getProperties()
210
    {
211
        return $this->properties;
212
    }
213
}
214