Completed
Push — master ( 8b4718...61d59f )
by Oleg
02:27
created

Experiments::create()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.1707

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 26
ccs 11
cts 15
cp 0.7332
crap 3.1707
rs 8.8571
c 0
b 0
f 0
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\Exception;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Experiment;
11
use WebMarketingROI\OptimizelyPHP\Resource\v2\ExperimentResults;
12
13
/**
14
 * Provides methods for working with Optimizely experiments.
15
 */
16
class Experiments 
17
{
18
    /**
19
     * Optimizely API Client.
20
     * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient
21
     */
22
    private $client;
23
    
24
    /**
25
     * Constructor.
26
     */
27 12
    public function __construct($client)
28
    {
29 12
        $this->client = $client;
30 12
    }
31
    
32
    /**
33
     * Get a list of all the experiments by Project or Campaign
34
     * @param integer $projectId
35
     * @param integer $campaignId
36
     * @param boolean $includeClassic
37
     * @param integer $page
38
     * @param integer $perPage
39
     * @return Result
40
     * @throws Exception
41
     */
42 4
    public function listAll($projectId, $campaignId=null, $includeClassic=false, $page=1, $perPage=25)
43
    {
44 4
        if ($projectId==null && $campaignId==null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $campaignId of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
45 1
            throw new Exception('Project ID or Campaign ID must be non-null');
46
        }
47
        
48 3
        if ($page<0) {
49 1
            throw new Exception('Invalid page number passed');
50
        }
51
        
52 2
        if ($perPage<0 || $perPage>100) {
53 1
            throw new Exception('Invalid page size passed');
54
        }
55
        
56 1
        $result = $this->client->sendApiRequest('/experiments', 
57
                array(
58 1
                    'project_id'=>$projectId, 
59 1
                    'campaign_id'=>$campaignId,
60 1
                    'include_classic'=>$includeClassic,
61 1
                    'page'=>$page,
62
                    'per_page'=>$perPage
63 1
                ));
64
        
65 1
        $experiments = array();
66 1
        foreach ($result->getDecodedJsonData() as $experimentInfo) {
67 1
            $experiment = new Experiment($experimentInfo);
68 1
            $experiments[] = $experiment;
69 1
        }
70 1
        $result->setPayload($experiments);
71
        
72 1
        return $result;
73
    }
74
    
75
    /**
76
     * Get metadata for a single Experiment.
77
     * @param integer $experimentId
78
     * @return Result
79
     * @throws Exception
80
     */
81 3 View Code Duplication
    public function get($experimentId)
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...
82
    {
83 3
        if (!is_int($experimentId)) {
84 1
            throw new Exception("Integer experiment ID expected, while got '$experimentId'",
85 1
                    Exception::CODE_INVALID_ARG);
86
        }
87
        
88 2
        if ($experimentId<0) {
89 1
            throw new Exception("A positive experiment ID expected");
90
        }
91
        
92 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId");
93
        
94 1
        $experiment = new Experiment($result->getDecodedJsonData());
95 1
        $result->setPayload($experiment);
96
        
97 1
        return $result;
98
    }
99
    
100
    /**
101
     * Get results for a single experiment
102
     * @param integer $experimentId The id for the experiment you want results for
103
     * @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.
104
     * @param string $startTime The earliest time to count events in results. Defaults to the time that the experiment was first activated.
105
     * @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.
106
     * @return Result
107
     * @throws Exception
108
     */
109 1
    public function getResults($experimentId, $baselineVariationId = null, $startTime = null, $endTime = null)
110
    {
111 1
        if (!is_int($experimentId)) {
112
            throw new Exception("Integer experiment ID expected, while got '$experimentId'",
113
                    Exception::CODE_INVALID_ARG);
114
        }
115
        
116 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId/results",
117
                array(
118 1
                    'baseline_variation_id' => $baselineVariationId,
119 1
                    'start_time' => $startTime,
120
                    'end_time' => $endTime
121 1
                ));
122
        
123 1
        $experimentResults = new ExperimentResults($result->getDecodedJsonData());
124 1
        $result->setPayload($experimentResults);
125
        
126 1
        return $result;
127
    }
128
    
129
    /**
130
     * Create an experiment in a Project.
131
     * @param Experiment $experiment
132
     * @param boolean $publish Set to true to make the the experiment live to the world upon creation.
133
     * @return Result
134
     * @throw Exception
135
     */
136 1
    public function create($experiment, $publish)
137
    {
138 1
        if (!($experiment instanceOf Experiment)) {
139
            throw new Exception("Expected argument of type Experiment",
140
                    Exception::CODE_INVALID_ARG);
141
        }
142
        
143 1
        if (!is_bool($publish)) {
144
            throw new Exception("Expected boolean publish argument",
145
                    Exception::CODE_INVALID_ARG);
146
        }
147
        
148
        $queryParams = array(
149 1
            'publish' => $publish,            
150 1
        );
151
        
152 1
        $postData = $experiment->toArray();
153
        
154 1
        $result = $this->client->sendApiRequest("/experiments", $queryParams, 'POST', 
155 1
                $postData);
156
        
157 1
        $experiment = new Experiment($result->getDecodedJsonData());
158 1
        $result->setPayload($experiment);
159
        
160 1
        return $result;
161
    }
162
    
163
    /**
164
     * Update an Experiment by ID
165
     * @param integer $experimentId
166
     * @param Experiment $experiment
167
     * @param boolean $overrideChanges If there are draft changes already in the experiment, you can override those changes by providing this query parameter.
168
     * @param boolean $publish Whether to publish the changes to the world.
169
     * @return Result
170
     * @throws Exception
171
     */
172 1
    public function update($experimentId, $experiment, $overrideChanges, $publish) 
173
    {
174 1
        if (!is_int($experimentId)) {
175
            throw new Exception("Expected argument of type Experiment");
176
        }
177
        
178 1
        if ($experimentId<0) {
179
            throw new Exception("Expected positive experiment ID argument");
180
        }
181
        
182 1
        if (!($experiment instanceOf Experiment)) {
183
            throw new Exception("Expected argument of type Experiment");
184
        }
185
        
186
        $queryParams = array(
187 1
            'override_changes' => $overrideChanges,
188
            'publish' => $publish
189 1
        );
190
        
191 1
        $postData = $experiment->toArray();
192
              
193 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", $queryParams, 'PATCH', 
194 1
                $postData);
195
        
196 1
        $experiment = new Experiment($result->getDecodedJsonData());
197 1
        $result->setPayload($experiment);
198
        
199 1
        return $result;
200
    }
201
    
202
    /**
203
     * Delete Experiment by ID
204
     * @param integer $experimentId
205
     * @return Result
206
     * @throws Exception
207
     */
208 1
    public function delete($experimentId) 
209
    {
210 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", array(), 'DELETE', 
211 1
                array());
212
        
213 1
        return $result;
214
    }
215
}
216
217