Completed
Push — master ( 96059f...fe2237 )
by Oleg
06:07
created

CampaignMetricResults::__construct()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10.3375

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 10
nop 1
dl 0
loc 23
ccs 17
cts 20
cp 0.85
crap 10.3375
rs 5.6534
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\VariantResults;
11
12
/**
13
 * Optimizely campaign metric results.
14
 */
15
class CampaignMetricResults
16
{
17
    /**
18
     * 
19
     * @var string
20
     */
21
    private $event;
22
    
23
    /**
24
     *
25
     * @var string
26
     */
27
    private $eventName;
28
    
29
    /**
30
     * Conversions indicate the total number of visitors or sessions where the 
31
     * event happened. Impressions indicate the total number of times the event 
32
     * happened (possibly multiple per visitor or session). Revenue indicates 
33
     * the sum of all revenue sent from all events in the Campaign. 
34
     * Can be 'conversions', 'impressions' or 'revenue'.
35
     * @var string
36
     */
37
    private $measure;
38
    
39
    /**
40
     *
41
     * @var string
42
     */
43
    private $metricId;
44
    
45
    /**
46
     *
47
     * @var integer
48
     */
49
    private $priority;
50
    
51
    /**
52
     * A map of results for the variants affected by the campaign. Variants may 
53
     * represent aggregated results scoped to the campaign and/or individual 
54
     * experiment results scoped to just that experiment. The special variant 
55
     * 'baseline' represents visitors that have been held back from any change 
56
     * in experience across all Experiments in the Campaign. The special variant 
57
     * 'campaign' represents the aggregated effect of all experiments included in 
58
     * the Campaign.
59
     * @var VariantResults
60
     */
61
    private $results;
62
    
63
    /**
64
     * Can be 'session', 'visitor' or 'event'.
65
     * @var string
66
     */
67
    private $unit;
68
    
69
    /**
70
     * Constructor.
71
     */
72 3
    public function __construct($options = array())
73
    {
74 3
        foreach ($options as $name=>$value) {
75
            switch ($name) {                
76 3
                case 'event': $this->setEvent($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 3
                case 'event_name': $this->setEventName($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 3
                case 'measure': $this->setMeasure($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 3
                case 'metric_id': $this->setMetricId($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 3
                case 'priority': $this->setPriority($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...
81 3
                case 'results': {
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...
82 3
                    $results = array();
83 3
                    foreach ($value as $name=>$info) {
84 3
                        $results[$name] = new VariantResults($info);
85 3
                    }
86 3
                    $this->setResults($results); 
87 3
                    break;
88
                }
89 3
                case 'unit': $this->setUnit($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...
90
                default:
91
                    throw new Exception('Unknown option found in CampaignMetricResults entity: ' . $name);
92
            }
93 3
        }
94 3
    }
95
    
96
    /**
97
     * Returns this object as array.
98
     */
99 1
    public function toArray()
100
    {
101
        $options = array(
102 1
            'event' => $this->getEvent(),
103 1
            'event_name' => $this->getEventName(),
104 1
            'measure' => $this->getMeasure(),
105 1
            'metric_id' => $this->getMetricId(),
106 1
            'priority' => $this->getPriority(),
107 1
            'results' => array(),
108 1
            'unit' => $this->getUnit()
109 1
        );
110
        
111 1
        foreach ($this->getResults() as $name=>$result) {
0 ignored issues
show
Bug introduced by
The expression $this->getResults() of type object<WebMarketingROI\O...urce\v2\VariantResults> is not traversable.
Loading history...
112 1
            $options['results'][$name] = $result->toArray();
113 1
        }
114
        
115
        // Remove options with empty values
116 1
        $cleanedOptions = array();
117 1
        foreach ($options as $name=>$value) {
118 1
            if ($value!==null)
119 1
                $cleanedOptions[$name] = $value;
120 1
        }
121
        
122 1
        return $cleanedOptions;
123
    }
124
    
125 1
    public function getEvent()
126
    {
127 1
        return $this->event;
128
    }
129
    
130 3
    public function setEvent($event)
131
    {
132 3
        $this->event = $event;
133 3
    }
134
    
135 1
    public function getEventName()
136
    {
137 1
        return $this->eventName;
138
    }
139
    
140 3
    public function setEventName($eventName)
141
    {
142 3
        $this->eventName = $eventName;
143 3
    }
144
    
145 1
    public function getMeasure()
146
    {
147 1
        return $this->measure;
148
    }
149
    
150 3
    public function setMeasure($measure)
151
    {
152 3
        $this->measure = $measure;
153 3
    }
154
    
155 1
    public function getMetricId()
156
    {
157 1
        return $this->metricId;
158
    }
159
    
160 3
    public function setMetricId($metricId)
161
    {
162 3
        $this->metricId = $metricId;
163 3
    }
164
    
165 1
    public function getPriority()
166
    {
167 1
        return $this->priority;
168
    }
169
    
170 3
    public function setPriority($priority)
171
    {
172 3
        $this->priority = $priority;
173 3
    }
174
    
175 1
    public function getResults()
176
    {
177 1
        return $this->results;
178
    }
179
    
180 3
    public function setResults($results)
181
    {
182 3
        $this->results = $results;
183 3
    }
184
    
185 1
    public function getUnit()
186
    {
187 1
        return $this->unit;
188
    }
189
    
190 3
    public function setUnit($unit)
191
    {
192 3
        $this->unit = $unit;
193 3
    }
194
}
195
196
197
198
199
200
201