Completed
Push — master ( fed072...89d815 )
by Oleg
16:38
created

Experiments::update()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 3
dl 0
loc 24
ccs 10
cts 12
cp 0.8333
crap 3.0416
rs 8.9713
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 1
                    'per_page'=>$perPage
63
                ));
64
        
65 1
        $experiments = array();
66 1
        foreach ($result->getDecodedJsonData() as $experimentInfo) {
67 1
            $experiment = new Experiment($experimentInfo);
68 1
            $experiments[] = $experiment;
69
        }
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
    public function get($experimentId)
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 1
                    'end_time' => $endTime
121
                ));
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 string $action Action to change the state of the Experiment. 
133
     * @return Result
134
     * @throw Exception
135
     */
136 1
    public function create($experiment, $action = null)
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 ($action!=null && !is_string($action)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $action of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
144
            throw new Exception("Expected string or null action argument",
145
                    Exception::CODE_INVALID_ARG);
146
        }
147
        
148 1
        $queryParams = array();
149 1
        if ($action!=null) $queryParams['action'] = $action;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $action of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
150
        
151 1
        $postData = $experiment->toArray();
152
        
153 1
        $result = $this->client->sendApiRequest("/experiments", $queryParams, 'POST', 
154 1
                $postData);
155
        
156 1
        $experiment = new Experiment($result->getDecodedJsonData());
157 1
        $result->setPayload($experiment);
158
        
159 1
        return $result;
160
    }
161
    
162
    /**
163
     * Update an Experiment by ID
164
     * @param integer $experimentId
165
     * @param Experiment $experiment
166
     * @param string $action Action to change the state of the Experiment. 
167
     * @return Result
168
     * @throws Exception
169
     */
170 1
    public function update($experimentId, $experiment, $action) 
171
    {
172 1
        if ($experimentId<0) {
173
            throw new Exception("Expected positive experiment ID argument");
174
        }
175
        
176 1
        if (!($experiment instanceOf Experiment)) {
177
            throw new Exception("Expected argument of type Experiment");
178
        }
179
        
180
        $queryParams = array(
181 1
            'action' => $action
182
        );
183
        
184 1
        $postData = $experiment->toArray();
185
              
186 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", $queryParams, 'PATCH', 
187 1
                $postData);
188
        
189 1
        $experiment = new Experiment($result->getDecodedJsonData());
190 1
        $result->setPayload($experiment);
191
        
192 1
        return $result;
193
    }
194
    
195
    /**
196
     * Delete Experiment by ID
197
     * @param integer $experimentId
198
     * @return Result
199
     * @throws Exception
200
     */
201 1
    public function delete($experimentId) 
202
    {
203 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", array(), 'DELETE', 
204 1
                array());
205
        
206 1
        return $result;
207
    }
208
}
209
210