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

Variation::__construct()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.0296

Importance

Changes 0
Metric Value
cc 10
eloc 16
nc 10
nop 1
dl 0
loc 22
ccs 14
cts 15
cp 0.9333
crap 10.0296
rs 6.1368
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 12 October 2016
5
 * @copyright (c) 2016, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Resource\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Exception;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Action;
11
12
/**
13
 * Optimizely variation.
14
 */
15
class Variation
16
{
17
    /**
18
     * A set of actions to take to apply the experiment when running
19
     * @var array[Actions]
20
     */
21
    private $actions;
22
    
23
    /**
24
     * Whether the variation is archived
25
     * @var boolean
26
     */
27
    private $archived;
28
    
29
    /**
30
     * Unique string identifier for this variation within the experiment
31
     * @var string
32
     */
33
    private $key;
34
    
35
    /**
36
     * The name of the variation
37
     * @var string
38
     */
39
    private $name;
40
    
41
    /**
42
     * The ID of the variation
43
     * @var type 
44
     */
45
    private $variationId;
46
    
47
    /**
48
     * The weight of the variation expressed as an integer between 0 and 10000. 
49
     * The weights of all varitions MUST add up to 10000 total (i.e. 100%)
50
     * @var integer 
51
     */
52
    private $weight;
53
    
54
    /**
55
     * Current status of the variation
56
     * @var string 
57
     */
58
    private $status;
59
    
60
    /**
61
     * Constructor.
62
     */
63 7
    public function __construct($options = array())
64
    {
65 7
        foreach ($options as $name=>$value) {
66
            switch ($name) {                
67 6
                case 'actions': {
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...
68 6
                    $actions = array();
69 6
                    foreach ($value as $actionInfo) {
70 6
                        $actions[] = new Action($actionInfo);
71
                    }
72 6
                    $this->setActions($actions); break;
73
                }
74 6
                case 'archived': $this->setArchived($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...
75 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...
76 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...
77 6
                case 'variation_id': $this->setVariationId($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...
78 6
                case 'weight': $this->setWeight($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...
79
                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...
80
                default:
81 6
                    throw new Exception('Unknown option: ' . $name);
82
            }
83
        }
84 7
    }
85
    
86
    /**
87
     * Returns this object as array.
88
     */
89 3
    public function toArray()
90
    {
91
        $options = array(
92 3
            'actions' => array(),
93 3
            'archived' => $this->getArchived(),
94 3
            'key' => $this->getKey(),
95 3
            'name' => $this->getName(),
96 3
            'variation_id' => $this->getVariationId(),
97 3
            'weight' => $this->getWeight(),
98 3
            'status' => $this->getStatus(),
99
        );
100
        
101 3
        foreach ($this->getActions() as $action) {
102 3
            $options['actions'][] = $action->toArray();
103
        }
104
        
105
        // Remove options with empty values
106 3
        $cleanedOptions = array();
107 3
        foreach ($options as $name=>$value) {
108 3
            if ($value!==null)
109 3
                $cleanedOptions[$name] = $value;
110
        }
111
        
112 3
        return $cleanedOptions;
113
    }
114
    
115 3
    public function getActions()
116
    {
117 3
        return $this->actions;
118
    }
119
    
120 7
    public function setActions($actions)
121
    {
122 7
        $this->actions = $actions;
123 7
    }
124
    
125 3
    public function getArchived()
126
    {
127 3
        return $this->archived;
128
    }
129
    
130 7
    public function setArchived($archived)
131
    {
132 7
        $this->archived = $archived;
133 7
    }
134
    
135 4
    public function getKey()
136
    {
137 4
        return $this->key;
138
    }
139
    
140 7
    public function setKey($key)
141
    {
142 7
        $this->key = $key;
143 7
    }
144
    
145 3
    public function getName()
146
    {
147 3
        return $this->name;
148
    }
149
    
150 7
    public function setName($name)
151
    {
152 7
        $this->name = $name;
153 7
    }
154
    
155 3
    public function getVariationId()
156
    {
157 3
        return $this->variationId;
158
    }
159
    
160 7
    public function setVariationId($variationId)
161
    {
162 7
        $this->variationId = $variationId;
163 7
    }
164
    
165 3
    public function getWeight()
166
    {
167 3
        return $this->weight;
168
    }
169
    
170 7
    public function setWeight($weight)
171
    {
172 7
        $this->weight = $weight;
173 7
    }
174
    
175 3
    public function getStatus()
176
    {
177 3
        return $this->status;
178
    }
179
    
180
    public function setStatus($status)
181
    {
182
        $this->status = $status;
183
    }
184
}
185
186
187
188
189
190
191
192
193
194