Completed
Push — master ( f2d606...ed3e7f )
by Oleg
02:59
created

Plan::setPlanName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
/**
3
 * @author Oleg Krivtsov <[email protected]>
4
 * @date 15 July 2019
5
 * @copyright (c) 2019, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Resource\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Exception;
10
11
/**
12
 * An Optimizely plan.
13
 */
14
class Plan
15
{
16
    /**
17
     * The account ID of the account that this Plan & Usage information is associated with
18
     * @var integer
19
     */
20
    private $accountId;
21
    
22
    /**
23
     * The name of the plan for the current account
24
     * @var string
25
     */
26
    private $planName;
27
    
28
    /**
29
     * Array of products under this account
30
     * @var ProductUsage[]
31
     */
32
    private $productUsages;
33
    
34
    /**
35
     * The status of the plan for the current account
36
     * @var string 
37
     */
38
    private $status;
39
    
40
    /**
41
     * The unit by which we measure the usage of this account
42
     * @var string
43
     */
44
    private $unitOfMeasurement;
45
    
46
    /**
47
     * Constructor.
48
     */
49 4
    public function __construct($options = array())
50
    {
51 4
        foreach ($options as $name=>$value) {
52
            switch ($name) {                
53 3
                case 'account_id': $this->setAccountId($value); break;
54 3
                case 'plan_name': $this->setPlanName($value); break;
55 3
                case 'product_usages': {
56 3
                    $productUsages = array();
57 3
                    foreach ($value as $productUsageInfo) {
58 3
                        $productUsages[] = new ProductUsage($productUsageInfo);
59
                    }
60 3
                    $this->setProductUsages($productUsages); break;
61
                }
62 3
                case 'status': $this->setStatus($value); break;
63 3
                case 'unit_of_measurement': $this->setUnitOfMeasurement($value); break;
64
                default:
65 3
                    throw new Exception('Unknown option found in the Plan entity: ' . $name);
66
            }
67
        }
68 4
    }
69
    
70
    /**
71
     * Returns this object as array.
72
     */
73 1
    public function toArray()
74
    {
75
        $options = array(
76 1
            'account_id' => $this->getAccountId(),
77 1
            'plan_name' => $this->getPlanName(),
78
            'product_usages' => array(),
79 1
            'status' => $this->getStatus(),
80 1
            'unit_of_measurement' => $this->getUnitOfMeasurement(),
81
        );
82
        
83 1
        foreach ($this->getProductUsages() as $productUsage) {
84 1
            $options['product_usages'][] = $productUsage->toArray();
85
        }
86
        
87
        // Remove options with empty values
88 1
        $cleanedOptions = array();
89 1
        foreach ($options as $name=>$value) {
90 1
            if ($value!==null)
91 1
                $cleanedOptions[$name] = $value;
92
        }
93
        
94 1
        return $cleanedOptions;
95
    }
96
    
97 3
    public function getAccountId()
98
    {
99 3
        return $this->accountId;
100
    }
101
    
102 4
    public function setAccountId($accountId)
103
    {
104 4
        $this->accountId = $accountId;
105 4
    }
106
    
107 4
    public function getPlanName()
108
    {
109 4
        return $this->planName;
110
    }
111
    
112 4
    public function setPlanName($planName)
113
    {
114 4
        $this->planName = $planName;
115 4
    }
116
    
117 3
    public function getProductUsages()
118
    {
119 3
        return $this->productUsages;
120
    }
121
    
122 4
    public function setProductUsages($productUsages)
123
    {
124 4
        $this->productUsages = $productUsages;
125 4
    }
126
    
127 3
    public function getStatus()
128
    {
129 3
        return $this->status;
130
    }
131
    
132 4
    public function setStatus($status)
133
    {
134 4
        $this->status = $status;
135 4
    }
136
    
137 3
    public function getUnitOfMeasurement()
138
    {
139 3
        return $this->unitOfMeasurement;
140
    }
141
    
142 4
    public function setUnitOfMeasurement($unitOfMeasurement)
143
    {
144 4
        $this->unitOfMeasurement = $unitOfMeasurement;
145 4
    }
146
}
147
148
149
150
151
152
153
154
155
156