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

TailoredAudienceChanges::getState()   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\Cursor;
7
use Hborras\TwitterAdsSDK\TwitterAds\Resource;
8
use Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience\Exception\InvalidOperation;
9
10
/**
11
 * Represents a Tailored Audience Change object
12
 * Supports: GET, POST
13
 *
14
 * @since 2016-06-27
15
 */
16
final class TailoredAudienceChanges extends Resource
17
{
18
    const RESOURCE_COLLECTION = 'accounts/{account_id}/tailored_audience_changes';
19
    const RESOURCE = 'accounts/{account_id}/tailored_audience_changes/{id}';
20
21
    const ADD = 'ADD';
22
    const REMOVE = 'REMOVE';
23
    const REPLACE = 'REPLACE';
24
25
    protected $input_file_path;
26
    protected $tailored_audience_id;
27
    protected $operation;
28
29
    protected $properties = [
30
        'tailored_audience_id',
31
        'input_file_path',
32
        'operation',
33
    ];
34
35
    /** Read Only */
36
    protected $id;
37
    protected $state;
38
39
    public function updateAudience($tailoredAudienceId, $location, $listType, $operation)
40
    {
41
        $params = [
42
            'tailored_audience_id' => $tailoredAudienceId,
43
            'input_file_path' => $location,
44
            'list_type' => $listType,
45
            'operation' => $operation
46
        ];
47
48
        $resource = str_replace(static::RESOURCE_REPLACE, $this->getAccount()->getId(), TailoredAudienceChanges::RESOURCE_COLLECTION);
49
50
        return $this->getAccount()->getTwitterAds()->post($resource, $params);
51
    }
52
53
    public function status($tailoredAudienceId)
54
    {
55
        $resource = str_replace(static::RESOURCE_REPLACE, $this->getAccount()->getId(), TailoredAudienceChanges::RESOURCE_COLLECTION);
56
        $response = $this->getAccount()->getTwitterAds()->get($resource, []);
57
58
        $tailoredAudienceChanges = new Cursor($this, $this->getAccount(), $response->getBody(), []);
59
60
        foreach ($tailoredAudienceChanges as $tailoredAudienceChange) {
61
            if ($tailoredAudienceChange->getTailoredAudienceId() == $tailoredAudienceId) {
62
                return $tailoredAudienceChange;
63
            }
64
        }
65
        return null;
66
    }
67
68
    public function getId()
69
    {
70
        return $this->id;
71
    }
72
73
    public function getInputFilePath()
74
    {
75
        return $this->input_file_path;
76
    }
77
78
    public function setInputFilePath($path)
79
    {
80
        $this->input_file_path = $path;
81
    }
82
83
    public function getTailoredAudienceId()
84
    {
85
        return $this->tailored_audience_id;
86
    }
87
88
    public function setTailoredAudienceId($id)
89
    {
90
        $this->tailored_audience_id = $id;
91
    }
92
93
    public function getOperation()
94
    {
95
        return $this->assureValidOperation($this->operation);
96
    }
97
98
    public function setOperation($op)
99
    {
100
        $this->operation = $this->assureValidOperation($op);
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getProperties()
107
    {
108
        return $this->properties;
109
    }
110
111
    /**
112
     * @return mixed
113
     */
114
    public function getState()
115
    {
116
        return $this->state;
117
    }
118
119
    private function getOperations()
120
    {
121
        return [
122
            self::ADD,
123
            self::REMOVE,
124
            self::REPLACE,
125
        ];
126
    }
127
128
    private function assureValidOperation($op)
129
    {
130
        foreach ($this->getOperations() as $allowedOp) {
131
            if ($op === $allowedOp) {
132
                return $op;
133
            }
134
        }
135
136
        throw new InvalidOperation(
137
            sprintf('"%s" is not a valid operation for %s', $op, TailoredAudience::class)
138
        );
139
    }
140
}
141