Completed
Push — master ( 6427ed...a99496 )
by Oleg
02:58
created

Experiments::update()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 4.1574

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 3
dl 28
loc 28
ccs 11
cts 14
cp 0.7856
crap 4.1574
rs 8.5806
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
    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
                    '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 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 View Code Duplication
    public function update($experimentId, $experiment, $action) 
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...
171
    {
172 1
        if (!is_int($experimentId)) {
173
            throw new Exception("Expected argument of type Experiment");
174
        }
175
        
176 1
        if ($experimentId<0) {
177
            throw new Exception("Expected positive experiment ID argument");
178
        }
179
        
180 1
        if (!($experiment instanceOf Experiment)) {
181
            throw new Exception("Expected argument of type Experiment");
182
        }
183
        
184
        $queryParams = array(
185
            'action' => $action
186 1
        );
187
        
188 1
        $postData = $experiment->toArray();
189
              
190 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", $queryParams, 'PATCH', 
191 1
                $postData);
192
        
193 1
        $experiment = new Experiment($result->getDecodedJsonData());
194 1
        $result->setPayload($experiment);
195
        
196 1
        return $result;
197
    }
198
    
199
    /**
200
     * Delete Experiment by ID
201
     * @param integer $experimentId
202
     * @return Result
203
     * @throws Exception
204
     */
205 1
    public function delete($experimentId) 
206
    {
207 1
        $result = $this->client->sendApiRequest("/experiments/$experimentId", array(), 'DELETE', 
208 1
                array());
209
        
210 1
        return $result;
211
    }
212
}
213
214