Completed
Push — develop ( 6fdb58...dee5cf )
by Oleg
04:27 queued 01:45
created

Experiment::__construct()   D

Complexity

Conditions 21
Paths 21

Size

Total Lines 46
Code Lines 36

Duplication

Lines 16
Ratio 34.78 %

Code Coverage

Tests 35
CRAP Score 21

Importance

Changes 0
Metric Value
cc 21
eloc 36
nc 21
nop 1
dl 16
loc 46
ccs 35
cts 35
cp 1
crap 21
rs 4.8366
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Oleg Krivtsov <[email protected]>
4
 * @date 05 October 2016
5
 * @copyright (c) 2016, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Resource\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\Schedule;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Variation;
11
use WebMarketingROI\OptimizelyPHP\Resource\v2\Change;
12
use WebMarketingROI\OptimizelyPHP\Resource\v2\Metric;
13
14
/**
15
 * An Optimizely experiment.
16
 */
17
class Experiment
18
{
19
    /**
20
     * The project the Experiment is in
21
     * @var integer
22
     */
23
    private $projectId;
24
    
25
    /**
26
     * List of IDs of all audiences the Experiment is targeted at
27
     * @var array[integer] 
28
     */
29
    private $audienceIds;
30
    
31
    /**
32
     * The ID for the Campaign that the Experiment is in
33
     * @var integer 
34
     */
35
    private $campaignId;
36
    
37
    /**
38
     * Experiment-level changes that will run before all Variations. Typically 
39
     * this is custom CSS or custom JavaScript.
40
     * @var array[Change] 
41
     */
42
    private $changes;
43
    
44
    /**
45
     * The time the experiment was initially created
46
     * @var string 
47
     */
48
    private $created;
49
    
50
    /**
51
     * The description or hypothesis for an Experiment
52
     * @var string 
53
     */
54
    private $description;
55
    
56
    /**
57
     * Percentage expressed as a number from 0-10000 to hold back from being 
58
     * included in the experiment
59
     * @var integer 
60
     */
61
    private $holdback;
62
    
63
    /**
64
     * Unique string identifier for this experiment within the project.
65
     * @var string
66
     */
67
    private $key;
68
    
69
    /**
70
     * The last time the experiment was modified
71
     * @var string 
72
     */
73
    private $lastModified;
74
    
75
    /**
76
     * An ordered list of metrics to track for the experiment
77
     * @var array[Metric] 
78
     */
79
    private $metrics;
80
    
81
    /**
82
     * Name of the Experiment
83
     * @var string 
84
     */
85
    private $name;
86
    
87
    /**
88
     * The last time the experiment was modified
89
     * @var Schedule 
90
     */
91
    private $schedule;
92
    
93
    /**
94
     * Current state of the experiment. Can be 'active', 'paused' or 'archived'.
95
     * @var string 
96
     */
97
    private $status;
98
    
99
    /**
100
     * A list of Variations that each define an experience to show in the context 
101
     * of the Experiment for the purpose of comparison against each other
102
     * @var array[Variation]
103
     */
104
    private $variations = array();
105
    
106
    /**
107
     * The unique identifier for the Experiment
108
     * @var integer 
109
     */
110
    private $id;
111
    
112
    /**
113
     * Whether or not the Experiment is a classic Experiment
114
     * @var boolean
115
     */
116
    private $isClassic;
117
    
118
    /**
119
     * Constructor.
120
     */
121 7
    public function __construct($options = array())
122
    {
123 7
        foreach ($options as $name=>$value) {
124
            switch ($name) {
125 6
                case 'project_id': $this->setProjectId($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
126 6
                case 'audience_ids': $this->setAudienceIds($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
127 6
                case 'campaign_id': $this->setCampaignId($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
128 6 View Code Duplication
                case 'changes': {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
129 6
                    $changes = array();
130 6
                    foreach ($value as $changeInfo) {
131 6
                        $changes[] = new Change($changeInfo);
132
                    }
133 6
                    $this->setChanges($changes); 
134 6
                    break;
135
                }
136 6
                case 'created': $this->setCreated($value); break;    
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
137 6
                case 'description': $this->setDescription($value); break;    
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
138 6
                case 'holdback': $this->setHoldback($value); break;    
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
139 6
                case 'key': $this->setKey($value); break;    
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
140 6
                case 'last_modified': $this->setLastModified($value); break;    
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
141 6
                case 'metrics': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
142 6
                    $metrics = array();
143 6
                    foreach ($value as $metricInfo) {
144 6
                        $metrics[] = new Metric($metricInfo);
145
                    }
146 6
                    $this->setMetrics($metrics); 
147 6
                    break;    
148
                }
149 6
                case 'name': $this->setName($value); break;    
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
150 6
                case 'schedule': $this->setSchedule(new Schedule($value)); break;    
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
151 6
                case 'status': $this->setStatus($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
152 6 View Code Duplication
                case 'variations': {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
153 6
                    $variations = array();
154 6
                    foreach ($value as $variationInfo) {
155 6
                        $variations[] = new Variation($variationInfo);
156
                    }
157 6
                    $this->setVariations($variations); 
158 6
                    break;
159
                }
160 4
                case 'id': $this->setId($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
161 4
                case 'is_classic': $this->setIsClassic($value); break;                
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
162
                default:
163 6
                    throw new \Exception('Unknown option: ' . $name);
164
            }
165
        }
166 7
    }
167
    
168
    /**
169
     * Returns this object as array.
170
     */
171 3
    public function toArray()
172
    {
173
        $options = array(
174 3
            'project_id' => $this->getProjectId(),
175 3
            'audience_ids' => $this->getAudienceIds(),
176 3
            'campaign_id' => $this->getCampaignId(),
177
            'changes' => array(),
178 3
            'created' => $this->getCreated(),
179 3
            'description' => $this->getDescription(),
180 3
            'holdback' => $this->getHoldback(),
181 3
            'key' => $this->getKey(),
182 3
            'last_modified' => $this->getLastModified(),
183
            'metrics' => array(),
184 3
            'name' => $this->getName(),
185 3
            'schedule' => $this->getSchedule()?$this->getSchedule()->toArray():null,
186 3
            'status' => $this->getStatus(),
187
            'variations' => array(),
188 3
            'id' => $this->getId(),
189 3
            'is_classic' => $this->getIsClassic(),
190
        );
191
        
192 3
        foreach ($this->getChanges() as $change) {
193 3
            $options['changes'][] = $change->toArray();
194
        }
195
        
196 3
        foreach ($this->getMetrics() as $metric) {
197 3
            $options['metrics'][] = $metric->toArray();
198
        }
199
        
200 3
        foreach ($this->getVariations() as $variation) {
201 3
            $options['variations'][] = $variation->toArray();
202
        }
203
        
204
        // Remove options with empty values
205 3
        $cleanedOptions = array();
206 3
        foreach ($options as $name=>$value) {
207 3
            if ($value!==null)
208 3
                $cleanedOptions[$name] = $value;
209
        }
210
        
211 3
        return $cleanedOptions;
212
    }
213
    
214 5
    public function getProjectId()
215
    {
216 5
        return $this->projectId;
217
    }
218
    
219 7
    public function setProjectId($projectId)
220
    {
221 7
        $this->projectId = $projectId;
222 7
    }
223
    
224 4
    public function getAudienceIds()
225
    {
226 4
        return $this->audienceIds;
227
    }
228
    
229 7
    public function setAudienceIds($audienceIds)
230
    {
231 7
        $this->audienceIds = $audienceIds;
232 7
    }
233
    
234 4
    public function getCampaignId()
235
    {
236 4
        return $this->campaignId;
237
    }
238
    
239 7
    public function setCampaignId($campaignId)
240
    {
241 7
        $this->campaignId = $campaignId;
242 7
    }
243
    
244 4
    public function getChanges()
245
    {
246 4
        return $this->changes;
247
    }
248
    
249 7
    public function setChanges($changes)
250
    {
251 7
        $this->changes = $changes;
252 7
    }
253
    
254 4
    public function getCreated()
255
    {
256 4
        return $this->created;
257
    }
258
    
259 5
    public function setCreated($created)
260
    {
261 5
        $this->created = $created;
262 5
    }
263
    
264 4
    public function getDescription()
265
    {
266 4
        return $this->description;
267
    }
268
    
269 7
    public function setDescription($description)
270
    {
271 7
        $this->description = $description;
272 7
    }
273
    
274 4
    public function getHoldback()
275
    {
276 4
        return $this->holdback;
277
    }
278
    
279 7
    public function setHoldback($holdback)
280
    {
281 7
        $this->holdback = $holdback;
282 7
    }
283
    
284 4
    public function getKey()
285
    {
286 4
        return $this->key;
287
    }
288
    
289 7
    public function setKey($key)
290
    {
291 7
        $this->key = $key;
292 7
    }
293
    
294 4
    public function getLastModified()
295
    {
296 4
        return $this->lastModified;
297
    }
298
    
299 5
    public function setLastModified($lastModified)
300
    {
301 5
        $this->lastModified = $lastModified;
302 5
    }
303
    
304 4
    public function getMetrics()
305
    {
306 4
        return $this->metrics;
307
    }
308
    
309 7
    public function setMetrics($metrics)
310
    {
311 7
        $this->metrics = $metrics;
312 7
    }
313
    
314 7
    public function getName()
315
    {
316 7
        return $this->name;
317
    }
318
    
319 7
    public function setName($name)
320
    {
321 7
        $this->name = $name;
322 7
    }
323
    
324 4
    public function getSchedule()
325
    {
326 4
        return $this->schedule;
327
    }
328
    
329 7
    public function setSchedule($schedule)
330
    {
331 7
        $this->schedule = $schedule;
332 7
    }
333
    
334 4
    public function getStatus()
335
    {
336 4
        return $this->status;
337
    }
338
    
339 7
    public function setStatus($status)
340
    {
341 7
        $this->status = $status;
342 7
    }
343
    
344 4
    public function getVariations()
345
    {
346 4
        return $this->variations;
347
    }
348
    
349 7
    public function setVariations($variations)
350
    {
351 7
        $this->variations = $variations;
352 7
    }
353
    
354 4
    public function getId()
355
    {
356 4
        return $this->id;
357
    }
358
    
359 5
    public function setId($id)
360
    {
361 5
        $this->id = $id;
362 5
    }
363
    
364 4
    public function getIsClassic()
365
    {
366 4
        return $this->isClassic;
367
    }
368
    
369 5
    public function setIsClassic($isClassic)
370
    {
371 5
        $this->isClassic = $isClassic;
372 5
    }
373
}
374
375