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

Action::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 7
Ratio 38.89 %

Code Coverage

Tests 11
CRAP Score 6.6829

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 1
dl 7
loc 18
ccs 11
cts 15
cp 0.7332
crap 6.6829
rs 8.8571
c 0
b 0
f 0
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\Change;
11
12
/**
13
 * Optimizely action.
14
 */
15
class Action
16
{
17
    /**
18
     * The list of changes to apply to the Page
19
     * @var array[Change] 
20
     */
21
    private $changes;
22
    
23
    /**
24
     * The Page ID to apply changes to
25
     * @var integer 
26
     */
27
    private $pageId;
28
    
29
    /**
30
     * The share link for the provided Variation and Page combination
31
     * @var string 
32
     */
33
    private $shareLink;
34
    
35
    /**
36
     * Constructor.
37
     */
38 7
    public function __construct($options = array())
39
    {
40 7
        foreach ($options as $name=>$value) {
41
            switch ($name) {                
42 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...
43 6
                    $changes = array();
44 6
                    foreach ($value as $changeInfo) {
45 6
                        $changes[] = new Change($changeInfo);
46 6
                    }
47 6
                    $this->setChanges($changes); break;
48
                }
49 6
                case 'page_id': $this->setPageId($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...
50
                case 'share_link': $this->setShareLink($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...
51
                default:
52
                    throw new Exception('Unknown option found in the Action entity: ' . $name);
53
            }
54 7
        }
55 7
    }
56
    
57
    /**
58
     * Returns this object as array.
59
     */
60 3
    public function toArray()
61
    {
62
        $options = array(
63 3
            'changes' => array(),
64 3
            'page_id' => $this->getPageId(),   
65 3
            'share_link' => $this->getShareLink(),
66 3
        );
67
        
68 3
        foreach ($this->getChanges() as $change) {
69 3
            $options['changes'][] = $change->toArray();
70 3
        }
71
        
72
        // Remove options with empty values
73 3
        $cleanedOptions = array();
74 3
        foreach ($options as $name=>$value) {
75 3
            if ($value!==null)
76 3
                $cleanedOptions[$name] = $value;
77 3
        }
78
        
79 3
        return $cleanedOptions;
80
    }
81
    
82 3
    public function getChanges()
83
    {
84 3
        return $this->changes;
85
    }
86
    
87 7
    public function setChanges($changes)
88
    {
89 7
        $this->changes = $changes;
90 7
    }
91
    
92 3
    public function getPageId()
93
    {
94 3
        return $this->pageId;
95
    }
96
    
97 7
    public function setPageId($pageId)
98
    {
99 7
        $this->pageId = $pageId;
100 7
    }
101
    
102 3
    public function getShareLink()
103
    {
104 3
        return $this->shareLink;
105
    }
106
    
107
    public function setShareLink($shareLink)
108
    {
109
        $this->shareLink = $shareLink;
110
    }
111
}
112
113
114
115
116
117
118
119
120
121
122