Completed
Push — master ( 0ca99d...7c70a3 )
by Oleg
02:52
created

Campaign::__construct()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 35
Code Lines 27

Duplication

Lines 7
Ratio 20 %

Code Coverage

Tests 28
CRAP Score 18.2938

Importance

Changes 0
Metric Value
cc 18
eloc 27
nc 18
nop 1
dl 7
loc 35
ccs 28
cts 31
cp 0.9032
crap 18.2938
rs 4.947
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 06 October 2016
5
 * @copyright (c) 2016, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Resource\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\Change;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Metric;
11
12
/**
13
 * An Optimizely campaign.
14
 */
15
class Campaign
16
{
17
    /**
18
     * The Project ID the Campaign is in
19
     * @var integer
20
     */
21
    private $projectId;
22
    
23
    /**
24
     * A list of changes to apply to the Campaign
25
     * @var array 
26
     */
27
    private $changes;
28
    
29
    /**
30
     * The time the Experiment was initially created
31
     * @var string
32
     */
33
    private $created;
34
    
35
    /**
36
     * The first time the Experiment was activated
37
     * @var string
38
     */
39
    private $earliest;
40
    
41
    /**
42
     * An ordered list of Experiment IDs used by the Campaign
43
     * @var array
44
     */
45
    private $experimentIds;
46
    
47
    /**
48
     * Percentage expressed as a number from 0-10000 to holdback from being 
49
     * included in the experiment.
50
     * @var integer
51
     */
52
    private $holdback;
53
    
54
    /**
55
     * The last time the Experiment was modified
56
     * @var string
57
     */
58
    private $lastModified;
59
    
60
    /**
61
     * The last time the Experiment was activated (not present if it is still activated)
62
     * @var string 
63
     */
64
    private $latest;
65
    
66
    /**
67
     * An ordered list of Metrics to track for the Campaign
68
     * @var array
69
     */
70
    private $metrics;
71
    
72
    /**
73
     * The name of the Campaign
74
     * @var string
75
     */
76
    private $name;
77
    
78
    /**
79
     * A list of Page IDs used in the Campaign.
80
     * @var array
81
     */
82
    private $pageIds;
83
    
84
    /**
85
     * Current state of the Campaign. Can be 'active', 'paused' or 'archived'
86
     * @var string
87
     */
88
    private $status;
89
    
90
    /**
91
     * The type of the Campaign. Can be a/b or personalization.
92
     * @var string
93
     */
94
    private $type;
95
    
96
    /**
97
     * The unique identifier for the Campaign
98
     * @var integer
99
     */
100
    private $id;
101
    
102
    /**
103
     * Constructor.
104
     */
105 7
    public function __construct($options = array())
106
    {
107 7
        foreach ($options as $name=>$value) {
108
            switch ($name) {                
109 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...
110 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...
111 6
                    $changes = array();
112 6
                    foreach ($value as $changeInfo) {
113 6
                        $changes[] = new Change($changeInfo);
114 6
                    }
115 6
                    $this->setChanges($changes); break;
116
                }
117 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...
118 6
                case 'earliest': $this->setEarliest($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...
119 6
                case 'experiment_ids': $this->setExperimentIds($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...
120 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...
121 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...
122 6
                case 'latest': $this->setLatest($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...
123 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...
124 6
                    $metrics = array();
125 6
                    foreach ($value as $metricInfo) {
126 6
                        $metrics[] = new Metric($metricInfo);
127 6
                    }
128 6
                    $this->setMetrics($metrics); break;
129
                }
130 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...
131 6
                case 'page_ids': $this->setPageIds($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...
132 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...
133 6
                case 'type': $this->setType($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...
134 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...
135
                default:
136
                    throw new \Exception('Unknown option: ' . $name);
137
            }
138 7
        }
139 7
    }
140
    
141
    /**
142
     * Returns this object as array.
143
     */
144 3
    public function toArray()
145
    {
146
        $options = array(
147 3
            'project_id' => $this->getProjectId(),
148 3
            'changes' => array(),
149 3
            'created' => $this->getCreated(),
150 3
            'earliest' => $this->getEarliest(),
151 3
            'experiment_ids' => $this->getExperimentIds(),
152 3
            'holdback' => $this->getHoldback(),
153 3
            'last_modified' => $this->getLastModified(),
154 3
            'latest' => $this->getLatest(),
155 3
            'metrics' => array(),
156 3
            'name' => $this->getName(),
157 3
            'page_ids' => $this->getPageIds(),
158 3
            'status' => $this->getStatus(),
159 3
            'type' => $this->getType(),
160 3
            'id' => $this->getId()
161 3
        );
162
        
163 3
        foreach ($this->getChanges() as $change) {
164 3
            $options['changes'][] = $change->toArray();
165 3
        }
166
        
167 3
        foreach ($this->getMetrics() as $metric) {
168 3
            $options['metrics'][] = $metric->toArray();
169 3
        }
170
        
171
        // Remove options with empty values
172 3
        $cleanedOptions = array();
173 3
        foreach ($options as $name=>$value) {
174 3
            if ($value!==null)
175 3
                $cleanedOptions[$name] = $value;
176 3
        }
177
        
178 3
        return $cleanedOptions;
179
    }
180
    
181 5
    public function getProjectId()
182
    {
183 5
        return $this->projectId;
184
    }
185
    
186 7
    public function setProjectId($projectId)
187
    {
188 7
        $this->projectId = $projectId;
189 7
    }
190
    
191 5
    public function getChanges()
192
    {
193 5
        return $this->changes;
194
    }
195
    
196 7
    public function setChanges($changes)
197
    {
198 7
        $this->changes = $changes;
199 7
    }
200
    
201 4
    public function getCreated()
202
    {
203 4
        return $this->created;
204
    }
205
    
206 7
    public function setCreated($created)
207
    {
208 7
        $this->created = $created;
209 7
    }
210
    
211 3
    public function getEarliest()
212
    {
213 3
        return $this->earliest;
214
    }
215
    
216 7
    public function setEarliest($earliest)
217
    {
218 7
        $this->earliest = $earliest;
219 7
    }
220
    
221 3
    public function getExperimentIds()
222
    {
223 3
        return $this->experimentIds;
224
    }
225
    
226 7
    public function setExperimentIds($experimentIds)
227
    {
228 7
        $this->experimentIds = $experimentIds;
229 7
    }
230
    
231 3
    public function getHoldback()
232
    {
233 3
        return $this->holdback;
234
    }
235
    
236 7
    public function setHoldback($holdback)
237
    {
238 7
        $this->holdback = $holdback;
239 7
    }
240
    
241 3
    public function getLastModified()
242
    {
243 3
        return $this->lastModified;
244
    }
245
    
246 7
    public function setLastModified($lastModified)
247
    {
248 7
        $this->lastModified = $lastModified;
249 7
    }
250
    
251 3
    public function getLatest()
252
    {
253 3
        return $this->latest;
254
    }
255
    
256 7
    public function setLatest($latest)
257
    {
258 7
        $this->latest = $latest;
259 7
    }
260
    
261 5
    public function getMetrics()
262
    {
263 5
        return $this->metrics;
264
    }
265
    
266 7
    public function setMetrics($metrics)
267
    {
268 7
        $this->metrics = $metrics;
269 7
    }
270
    
271 6
    public function getName()
272
    {
273 6
        return $this->name;
274
    }
275
    
276 7
    public function setName($name)
277
    {
278 7
        $this->name = $name;
279 7
    }
280
    
281 3
    public function getPageIds()
282
    {
283 3
        return $this->pageIds;
284
    }
285
    
286 7
    public function setPageIds($pageIds)
287
    {
288 7
        $this->pageIds = $pageIds;
289 7
    }
290
    
291 3
    public function getStatus()
292
    {
293 3
        return $this->status;
294
    }
295
    
296 7
    public function setStatus($status)
297
    {
298 7
        $this->status = $status;
299 7
    }
300
    
301 4
    public function getType()
302
    {
303 4
        return $this->type;
304
    }
305
    
306 7
    public function setType($type)
307
    {
308 7
        $this->type = $type;
309 7
    }
310
    
311 4
    public function getId()
312
    {
313 4
        return $this->id;
314
    }
315
    
316 5
    public function setId($id)
317
    {
318 5
        $this->id = $id;
319 5
    }
320
}
321
322
323
324
325