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

TailoredAudience::getReasonsNotTargetable()   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\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
     * @return boolean
50
     */
51
    public function isDeleted()
52
    {
53
        return filter_var($this->deleted, FILTER_VALIDATE_BOOLEAN);
54
    }
55
56
    /**
57
     * @return boolean
58
     */
59
    public function isTargetable()
60
    {
61
        return filter_var($this->targetable, FILTER_VALIDATE_BOOLEAN);
62
    }
63
64
    /**
65
     * @return integer
66
     */
67
    public function getAudienceSize()
68
    {
69
        return intval($this->audience_size);
70
    }
71
72
    /**
73
     * @return mixed
74
     */
75
    public function getId()
76
    {
77
        return $this->id;
78
    }
79
80
    /**
81
     * @return \DateTimeImmutable
82
     */
83
    public function getCreatedAt()
84
    {
85
        return $this->created_at;
86
    }
87
88
    /**
89
     * @return \DateTimeImmutable
90
     */
91
    public function getUpdatedAt()
92
    {
93
        return $this->updated_at;
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99
    public function getListType()
100
    {
101
        return $this->assureValidType($this->list_type);
102
    }
103
104
    /**
105
     * @param string $type
106
     */
107
    public function setListType($type)
108
    {
109
        $this->list_type = $this->assureValidType($type);
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getAudienceType()
116
    {
117
        return $this->audience_type;
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getReasonsNotTargetable()
124
    {
125
        return $this->reasons_not_targetable;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function getTargetableTypes()
132
    {
133
        return $this->targetable_types;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getName()
140
    {
141
        return $this->name;
142
    }
143
144
    /**
145
     * @param string $name
146
     */
147
    public function setName($name)
148
    {
149
        $this->name = $name;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getPartnerSource()
156
    {
157
        return $this->partner_source;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    public static function getTypes()
164
    {
165
        return [
166
            self::LIST_TYPE_DEVICE_ID,
167
            self::LIST_TYPE_EMAIL,
168
            self::LIST_TYPE_HANDLE,
169
            self::LIST_TYPE_PHONE_NUMBER,
170
            self::LIST_TYPE_TWITTER_ID,
171
        ];
172
    }
173
174
    /**
175
     * Asserts that the given type is valid
176
     *
177
     * @param string $type
178
     * @throws InvalidType - if type is invalid or null
179
     *
180
     * @return string
181
     */
182 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...
183
    {
184
        foreach (self::getTypes() as $allowedType) {
185
            if ($type === $allowedType) {
186
                return $type;
187
            }
188
        }
189
190
        throw new InvalidType(
191
            sprintf('"%s" is not a valid type for %s', $type, TailoredAudience::class)
192
        );
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function getProperties()
199
    {
200
        return $this->properties;
201
    }
202
}
203