1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Oleg Krivtsov <[email protected]> |
4
|
|
|
* @date 03 October 2016 |
5
|
|
|
* @copyright (c) 2016, Web Marketing ROI |
6
|
|
|
*/ |
7
|
|
|
namespace WebMarketingROI\OptimizelyPHP\Service\v2; |
8
|
|
|
|
9
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Experiment; |
10
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\ExperimentResults; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Provides methods for working with Optimizely experiments. |
14
|
|
|
*/ |
15
|
|
|
class Experiments |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Optimizely API Client. |
19
|
|
|
* @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
*/ |
26
|
7 |
|
public function __construct($client) |
27
|
|
|
{ |
28
|
7 |
|
$this->client = $client; |
29
|
7 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get a list of all the experiments by Project or Campaign |
33
|
|
|
* @param integer $projectId |
34
|
|
|
* @param integer $campaignId |
35
|
|
|
* @param boolean $includeClassic |
36
|
|
|
* @param integer $page |
37
|
|
|
* @param integer $perPage |
38
|
|
|
* @return array |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
1 |
|
public function listAll($projectId, $campaignId=null, $includeClassic=false, $page=1, $perPage=10) |
42
|
|
|
{ |
43
|
1 |
|
if (empty($projectId) && empty($campaignId)) { |
44
|
|
|
throw new \Exception('Project ID or Campaign ID must be non-empty'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$response = $this->client->sendApiRequest('/experiments', |
48
|
|
|
array( |
49
|
1 |
|
'project_id'=>$projectId, |
50
|
1 |
|
'campaign_id'=>$campaignId, |
51
|
1 |
|
'include_classic'=>$includeClassic, |
52
|
1 |
|
'page'=>$page, |
53
|
1 |
|
'per_page'=>$perPage |
54
|
|
|
)); |
55
|
|
|
|
56
|
1 |
|
$experiments = array(); |
57
|
1 |
|
foreach ($response as $experimentInfo) { |
58
|
1 |
|
$experiment = new Experiment($experimentInfo); |
59
|
1 |
|
$experiments[] = $experiment; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $experiments; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get metadata for a single Experiment. |
67
|
|
|
* @param integer $experimentId |
68
|
|
|
* @return Experiment |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
1 |
View Code Duplication |
public function get($experimentId) |
|
|
|
|
72
|
|
|
{ |
73
|
1 |
|
if (!is_int($experimentId)) { |
74
|
|
|
throw new \Exception("Integer experiment ID expected, while got '$experimentId'"); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$response = $this->client->sendApiRequest("/experiments/$experimentId"); |
78
|
|
|
|
79
|
1 |
|
$experiment = new Experiment($response); |
80
|
|
|
|
81
|
1 |
|
return $experiment; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get results for a single experiment |
86
|
|
|
* @param integer $experimentId The id for the experiment you want results for |
87
|
|
|
* @param integer $baselineVariationId The id of the variation to use as the baseline to compare against other variations. Defaults to the first variation if not provided. |
88
|
|
|
* @param string $startTime The earliest time to count events in results. Defaults to the time that the experiment was first activated. |
89
|
|
|
* @param string $endTime The latest time to count events in results. Defaults to the time the experiment was last active or the current time if the experiment is still running. |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
1 |
|
public function getResults($experimentId, $baselineVariationId = null, $startTime = null, $endTime = null) |
93
|
|
|
{ |
94
|
1 |
|
if (!is_int($experimentId)) { |
95
|
|
|
throw new \Exception("Integer experiment ID expected, while got '$experimentId'"); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$response = $this->client->sendApiRequest("/experiments/$experimentId/results", |
99
|
|
|
array( |
100
|
1 |
|
'baseline_variation_id' => $baselineVariationId, |
101
|
1 |
|
'start_time' => $startTime, |
102
|
1 |
|
'end_time' => $endTime |
103
|
|
|
)); |
104
|
|
|
|
105
|
1 |
|
$results = new ExperimentResults($response); |
106
|
|
|
|
107
|
1 |
|
return $results; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create an experiment in a Project. |
112
|
|
|
* @param Experiment $experiment |
113
|
|
|
* @param boolean $publish Set to true to make the the experiment live to the world upon creation. |
114
|
|
|
*/ |
115
|
1 |
|
public function create($experiment, $publish) |
116
|
|
|
{ |
117
|
1 |
|
if (!($experiment instanceOf Experiment)) { |
118
|
|
|
throw new \Exception("Expected argument of type Experiment"); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
if (!is_bool($publish)) { |
122
|
|
|
throw new \Exception("Expected boolean publish argument"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$queryParams = array( |
126
|
1 |
|
'publish' => $publish, |
127
|
|
|
); |
128
|
|
|
|
129
|
1 |
|
$postData = $experiment->toArray(); |
130
|
|
|
|
131
|
1 |
|
$response = $this->client->sendApiRequest("/experiments", $queryParams, 'POST', |
132
|
1 |
|
$postData, array(201)); |
133
|
|
|
|
134
|
1 |
|
return new Experiment($response); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Update an Experiment by ID |
139
|
|
|
* @param integer $experimentId |
140
|
|
|
* @param Experiment $experiment |
141
|
|
|
* @param boolean $overrideChanges If there are draft changes already in the experiment, you can override those changes by providing this query parameter. |
142
|
|
|
* @param boolean $publish Whether to publish the changes to the world. |
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
1 |
View Code Duplication |
public function update($experimentId, $experiment, $overrideChanges, $publish) |
|
|
|
|
146
|
|
|
{ |
147
|
1 |
|
if (!is_int($experimentId)) { |
148
|
|
|
throw new \Exception("Expected argument of type Experiment"); |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
if ($experimentId<0) { |
152
|
|
|
throw new \Exception("Expected positive experiment ID argument"); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
if (!($experiment instanceOf Experiment)) { |
156
|
|
|
throw new \Exception("Expected argument of type Experiment"); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$queryParams = array( |
160
|
1 |
|
'override_changes' => $overrideChanges, |
161
|
1 |
|
'publish' => $publish |
162
|
|
|
); |
163
|
|
|
|
164
|
1 |
|
$postData = $experiment->toArray(); |
165
|
|
|
|
166
|
1 |
|
$response = $this->client->sendApiRequest("/experiments/$experimentId", $queryParams, 'PATCH', |
167
|
1 |
|
$postData, array(200)); |
168
|
|
|
|
169
|
1 |
|
return new Experiment($response); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Delete Experiment by ID |
174
|
|
|
* @param integer $experimentId |
175
|
|
|
* @throws \Exception |
176
|
|
|
*/ |
177
|
1 |
|
public function delete($experimentId) |
178
|
|
|
{ |
179
|
1 |
|
$response = $this->client->sendApiRequest("/experiments/$experimentId", array(), 'DELETE', |
|
|
|
|
180
|
1 |
|
array(), array(204)); |
181
|
1 |
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
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.