1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Oleg Krivtsov <[email protected]> |
4
|
|
|
* @date 07 October 2016 |
5
|
|
|
* @copyright (c) 2016, Web Marketing ROI |
6
|
|
|
*/ |
7
|
|
|
namespace WebMarketingROI\OptimizelyPHP\Service\v2; |
8
|
|
|
|
9
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Audience; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Provides methods for working with Optimizely audiences. |
13
|
|
|
*/ |
14
|
|
|
class Audiences |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Optimizely API Client. |
18
|
|
|
* @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
19
|
|
|
*/ |
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct($client) |
26
|
|
|
{ |
27
|
4 |
|
$this->client = $client; |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* List Audiences for a Project |
32
|
|
|
* @param integer $projectId |
33
|
|
|
* @param integer $page |
34
|
|
|
* @param integer $perPage |
35
|
|
|
* @return Result |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
1 |
|
public function listAll($projectId, $page=1, $perPage=25) |
39
|
|
|
{ |
40
|
1 |
|
if ($page<0) { |
41
|
|
|
throw new Exception('Invalid page number passed', |
42
|
|
|
Exception::CODE_INVALID_ARG); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if ($perPage<0) { |
46
|
|
|
throw new Exception('Invalid page size passed', |
47
|
|
|
Exception::CODE_INVALID_ARG); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$result = $this->client->sendApiRequest('/audiences', |
51
|
|
|
array( |
52
|
1 |
|
'project_id'=>$projectId, |
53
|
1 |
|
'page'=>$page, |
54
|
|
|
'per_page'=>$perPage |
55
|
1 |
|
)); |
56
|
|
|
|
57
|
1 |
|
$audiences = array(); |
58
|
1 |
|
foreach ($result->getDecodedJsonData() as $audienceInfo) { |
59
|
1 |
|
$audience = new Audience($audienceInfo); |
60
|
1 |
|
$audiences[] = $audience; |
61
|
1 |
|
} |
62
|
1 |
|
$result->setPayload($audiences); |
63
|
|
|
|
64
|
1 |
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get metadata for a single Audience. |
69
|
|
|
* @param type $audienceId |
70
|
|
|
* @return Result |
71
|
|
|
* @throws Exception |
72
|
|
|
*/ |
73
|
1 |
|
public function get($audienceId) |
74
|
|
|
{ |
75
|
1 |
|
if (!is_int($audienceId)) { |
76
|
|
|
throw new Exception("Integer audience ID expected, while got '$audienceId'", |
77
|
|
|
Exception::CODE_INVALID_ARG); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$result = $this->client->sendApiRequest("/audiences/$audienceId"); |
81
|
|
|
|
82
|
1 |
|
$audience = new Audience($result->getDecodedJsonData()); |
83
|
1 |
|
$result->setPayload($audience); |
84
|
|
|
|
85
|
1 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create an Audience for a Project. |
90
|
|
|
* @param Result |
91
|
|
|
*/ |
92
|
1 |
View Code Duplication |
public function create($audience) |
|
|
|
|
93
|
|
|
{ |
94
|
1 |
|
if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) { |
95
|
|
|
throw new Exception("Expected argument of type Audience", |
96
|
|
|
Exception::CODE_INVALID_ARG); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$postData = $audience->toArray(); |
100
|
|
|
|
101
|
1 |
|
$result = $this->client->sendApiRequest("/audiences", array(), 'POST', |
102
|
1 |
|
$postData, array(201)); |
103
|
|
|
|
104
|
1 |
|
$audience = new Audience($result->getDecodedJsonData()); |
105
|
1 |
|
$result->setPayload($audience); |
106
|
|
|
|
107
|
1 |
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Update an Audience for a Project |
112
|
|
|
* @param integer $audienceId |
113
|
|
|
* @param Result |
114
|
|
|
* @throws Exception |
115
|
|
|
*/ |
116
|
1 |
View Code Duplication |
public function update($audienceId, $audience) |
|
|
|
|
117
|
|
|
{ |
118
|
1 |
|
if (!($audience instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Audience)) { |
119
|
|
|
throw new Exception("Expected argument of type Audience", |
120
|
|
|
Exception::CODE_INVALID_ARG); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
$postData = $audience->toArray(); |
124
|
|
|
|
125
|
1 |
|
$result = $this->client->sendApiRequest("/audiences/$audienceId", array(), 'PATCH', |
126
|
1 |
|
$postData, array(200)); |
127
|
|
|
|
128
|
1 |
|
$audience = new Audience($result->getDecodedJsonData()); |
129
|
1 |
|
$result->setPayload($audience); |
130
|
|
|
|
131
|
1 |
|
return $result; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
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.