Completed
Push — master ( adc61b...88ab02 )
by Hector
03:43
created

TailoredAudience   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 243
Duplicated Lines 4.94 %

Coupling/Cohesion

Components 5
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 243
rs 10
c 1
b 0
f 0
wmc 23
lcom 5
cbo 6

21 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 1
A createAudience() 0 8 1
A status() 0 5 1
A isDeleted() 0 4 1
A isTargetable() 0 4 1
A getAudienceSize() 0 4 1
A getId() 0 4 1
A getCreatedAt() 0 4 1
A getUpdatedAt() 0 4 1
A getListType() 0 4 1
A setListType() 0 4 1
A getAudienceType() 0 4 1
A getReasonsNotTargetable() 0 4 1
A getTargetableTypes() 0 4 1
A getName() 0 4 1
A setName() 0 4 1
A getPartnerSource() 0 4 1
A getTypes() 0 10 1
A assureValidType() 12 12 3
A getProperties() 0 4 1
A getMetadata() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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
    protected $metadata;
42
43
    /**
44
     * Uploads and creates a new tailored audience
45
     *
46
     * @param $filePath
47
     * @param $name
48
     * @param $listType
49
     * @return Resource
50
     * @throws TwitterAds\Errors\ServerError
51
     */
52
    public function create($filePath, $name, $listType)
53
    {
54
        $upload = new TONUpload($this->getTwitterAds(), $filePath);
55
56
        $this->createAudience($name, $listType);
57
        $location = $upload->perform();
58
        $tailoredAudienceChange = new TailoredAudienceChanges();
59
        $tailoredAudienceChange->updateAudience($this->getId(), $location, $listType, TailoredAudienceChanges::ADD);
60
61
        return $this->reload();
62
    }
63
64
    /**
65
     * Create a simple tailored audience object
66
     *
67
     * @param $name
68
     * @param $listType
69
     * @return $this
70
     */
71
    public function createAudience($name, $listType)
72
    {
73
        $params = ['name' => $name, 'list_type' => $listType];
74
        $resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_COLLECTION);
75
        $response = $this->getTwitterAds()->post($resource, $params);
76
77
        return $this->fromResponse($response->getBody()->data);
78
    }
79
80
    /**
81
     * Returns the TailoredAudienceChange with the status
82
     * @return null
83
     */
84
    public function status()
85
    {
86
        $tailoredAudienceChange = new TailoredAudienceChanges();
87
        return $tailoredAudienceChange->status($this->getId());
88
    }
89
90
    /**
91
     * @return boolean
92
     */
93
    public function isDeleted()
94
    {
95
        return filter_var($this->deleted, FILTER_VALIDATE_BOOLEAN);
96
    }
97
98
    /**
99
     * @return boolean
100
     */
101
    public function isTargetable()
102
    {
103
        return filter_var($this->targetable, FILTER_VALIDATE_BOOLEAN);
104
    }
105
106
    /**
107
     * @return integer
108
     */
109
    public function getAudienceSize()
110
    {
111
        return intval($this->audience_size);
112
    }
113
114
    /**
115
     * @return mixed
116
     */
117
    public function getId()
118
    {
119
        return $this->id;
120
    }
121
122
    /**
123
     * @return \DateTimeImmutable
124
     */
125
    public function getCreatedAt()
126
    {
127
        return $this->created_at;
128
    }
129
130
    /**
131
     * @return \DateTimeImmutable
132
     */
133
    public function getUpdatedAt()
134
    {
135
        return $this->updated_at;
136
    }
137
138
    /**
139
     * @return mixed
140
     */
141
    public function getListType()
142
    {
143
        return $this->assureValidType($this->list_type);
144
    }
145
146
    /**
147
     * @param string $type
148
     */
149
    public function setListType($type)
150
    {
151
        $this->list_type = $this->assureValidType($type);
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getAudienceType()
158
    {
159
        return $this->audience_type;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function getReasonsNotTargetable()
166
    {
167
        return $this->reasons_not_targetable;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function getTargetableTypes()
174
    {
175
        return $this->targetable_types;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getName()
182
    {
183
        return $this->name;
184
    }
185
186
    /**
187
     * @param string $name
188
     */
189
    public function setName($name)
190
    {
191
        $this->name = $name;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getPartnerSource()
198
    {
199
        return $this->partner_source;
200
    }
201
202
    /**
203
     * @return array
204
     */
205
    public static function getTypes()
206
    {
207
        return [
208
            self::LIST_TYPE_DEVICE_ID,
209
            self::LIST_TYPE_EMAIL,
210
            self::LIST_TYPE_HANDLE,
211
            self::LIST_TYPE_PHONE_NUMBER,
212
            self::LIST_TYPE_TWITTER_ID,
213
        ];
214
    }
215
216
    /**
217
     * Asserts that the given type is valid
218
     *
219
     * @param string $type
220
     * @throws InvalidType - if type is invalid or null
221
     *
222
     * @return string
223
     */
224 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...
225
    {
226
        foreach (self::getTypes() as $allowedType) {
227
            if ($type === $allowedType) {
228
                return $type;
229
            }
230
        }
231
232
        throw new InvalidType(
233
            sprintf('"%s" is not a valid type for %s', $type, TailoredAudience::class)
234
        );
235
    }
236
237
    /**
238
     * @return array
239
     */
240
    public function getProperties()
241
    {
242
        return $this->properties;
243
    }
244
245
    /**
246
     * @return mixed
247
     */
248
    public function getMetadata()
249
    {
250
        return $this->metadata;
251
    }
252
}
253