Completed
Push — master ( 90835e...e5901e )
by Oleg
02:50
created

Experiments   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.19%

Importance

Changes 0
Metric Value
dl 0
loc 189
ccs 60
cts 73
cp 0.8219
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B listAll() 0 25 4
A get() 0 14 2
A getResults() 0 19 2
B create() 0 26 3
B update() 0 29 4
A delete() 0 7 1
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 Result
39
     * @throws Exception
40
     */
41 1
    public function listAll($projectId, $campaignId=null, $includeClassic=false, $page=1, $perPage=25)
42
    {
43 1
        if (empty($projectId) && empty($campaignId)) {
44
            throw new Exception('Project ID or Campaign ID must be non-empty',
45
                    Exception::ERROR_INVALID_ARG);
46
        }
47
        
48 1
        $result = $this->client->sendApiRequest('/experiments', 
49
                array(
50 1
                    'project_id'=>$projectId, 
51 1
                    'campaign_id'=>$campaignId,
52 1
                    'include_classic'=>$includeClassic,
53 1
                    'page'=>$page,
54
                    'per_page'=>$perPage
55 1
                ));
56
        
57 1
        $experiments = array();
58 1
        foreach ($result->getDecodedJsonData() as $experimentInfo) {
59 1
            $experiment = new Experiment($experimentInfo);
60 1
            $experiments[] = $experiment;
61 1
        }
62 1
        $result->setPayload($experiments);
63
        
64 1
        return $result;
65
    }
66
    
67
    /**
68
     * Get metadata for a single Experiment.
69
     * @param integer $experimentId
70
     * @return Result
71
     * @throws Exception
72
     */
73 1
    public function get($experimentId)
74
    {
75 1
        if (!is_int($experimentId)) {
76
            throw new Exception("Integer experiment ID expected, while got '$experimentId'",
77
                    Exception::CODE_INVALID_ARG);
78
        }
79
        
80 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId");
81
        
82 1
        $experiment = new Experiment($result->getDecodedJsonData());
83 1
        $result->setPayload($experiment);
84
        
85 1
        return $result;
86
    }
87
    
88
    /**
89
     * Get results for a single experiment
90
     * @param integer $experimentId The id for the experiment you want results for
91
     * @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.
92
     * @param string $startTime The earliest time to count events in results. Defaults to the time that the experiment was first activated.
93
     * @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.
94
     * @return Result
95
     * @throws Exception
96
     */
97 1
    public function getResults($experimentId, $baselineVariationId = null, $startTime = null, $endTime = null)
98
    {
99 1
        if (!is_int($experimentId)) {
100
            throw new Exception("Integer experiment ID expected, while got '$experimentId'",
101
                    Exception::CODE_INVALID_ARG);
102
        }
103
        
104 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId/results",
105
                array(
106 1
                    'baseline_variation_id' => $baselineVariationId,
107 1
                    'start_time' => $startTime,
108
                    'end_time' => $endTime
109 1
                ));
110
        
111 1
        $experimentResults = new ExperimentResults($result->getDecodedJsonData());
112 1
        $result->setPayload($experimentResults);
113
        
114 1
        return $result;
115
    }
116
    
117
    /**
118
     * Create an experiment in a Project.
119
     * @param Experiment $experiment
120
     * @param boolean $publish Set to true to make the the experiment live to the world upon creation.
121
     * @return Result
122
     * @throw Exception
123
     */
124 1
    public function create($experiment, $publish)
125
    {
126 1
        if (!($experiment instanceOf Experiment)) {
127
            throw new Exception("Expected argument of type Experiment",
128
                    Exception::CODE_INVALID_ARG);
129
        }
130
        
131 1
        if (!is_bool($publish)) {
132
            throw new Exception("Expected boolean publish argument",
133
                    Exception::CODE_INVALID_ARG);
134
        }
135
        
136
        $queryParams = array(
137 1
            'publish' => $publish,            
138 1
        );
139
        
140 1
        $postData = $experiment->toArray();
141
        
142 1
        $result = $this->client->sendApiRequest("/experiments", $queryParams, 'POST', 
143 1
                $postData);
144
        
145 1
        $experiment = new Experiment($result->getDecodedJsonData());
146 1
        $result->setPayload($experiment);
147
        
148 1
        return $result;
149
    }
150
    
151
    /**
152
     * Update an Experiment by ID
153
     * @param integer $experimentId
154
     * @param Experiment $experiment
155
     * @param boolean $overrideChanges If there are draft changes already in the experiment, you can override those changes by providing this query parameter.
156
     * @param boolean $publish Whether to publish the changes to the world.
157
     * @return Result
158
     * @throws Exception
159
     */
160 1
    public function update($experimentId, $experiment, $overrideChanges, $publish) 
161
    {
162 1
        if (!is_int($experimentId)) {
163
            throw new Exception("Expected argument of type Experiment");
164
        }
165
        
166 1
        if ($experimentId<0) {
167
            throw new Exception("Expected positive experiment ID argument");
168
        }
169
        
170 1
        if (!($experiment instanceOf Experiment)) {
171
            throw new Exception("Expected argument of type Experiment");
172
        }
173
        
174
        $queryParams = array(
175 1
            'override_changes' => $overrideChanges,
176
            'publish' => $publish
177 1
        );
178
        
179 1
        $postData = $experiment->toArray();
180
              
181 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", $queryParams, 'PATCH', 
182 1
                $postData);
183
        
184 1
        $experiment = new Experiment($result->getDecodedJsonData());
185 1
        $result->setPayload($experiment);
186
        
187 1
        return $result;
188
    }
189
    
190
    /**
191
     * Delete Experiment by ID
192
     * @param integer $experimentId
193
     * @return Result
194
     * @throws Exception
195
     */
196 1
    public function delete($experimentId) 
197
    {
198 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", array(), 'DELETE', 
199 1
                array());
200
        
201 1
        return $result;
202
    }
203
}
204
205