Completed
Push — master ( a5de38...845fca )
by Andrii
06:07
created

Plan::setPrices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\plan;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
use hiqdev\php\billing\price\PriceInterface;
16
17
/**
18
 * Tariff Plan.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class Plan implements PlanInterface
23
{
24
    /**
25
     * @var int
26
     */
27
    protected $id;
28
29
    /**
30
     * @var string
31
     */
32
    protected $name;
33
34
    /**
35
     * @var Plan|null
36
     * XXX not sure to implement
37
     */
38
    protected $parent;
39
40
    /**
41
     * @var CustomerInterface
42
     */
43
    protected $seller;
44
45
    /**
46
     * @var PriceInterface[]
47
     */
48
    protected $prices = [];
49
50
    /**
51
     * @param PriceInterface[] $prices
52
     */
53 1
    public function __construct(
54
                            $id,
55
                            $name,
56
        CustomerInterface   $seller = null,
57
        array               $prices = []
58
    ) {
59 1
        $this->id = $id;
60 1
        $this->name = $name;
61 1
        $this->seller = $seller;
62 1
        $this->prices = $prices;
63 1
    }
64
65
    /**
66
     * @return int|string
67
     */
68
    public function getId()
69
    {
70
        return $this->id;
71
    }
72
73
    /**
74
     * @return PriceInterface[]
75
     */
76
    public function getPrices()
77
    {
78
        return $this->prices;
79
    }
80
81
    /**
82
     * @param PriceInterface[] $prices
83
     */
84
    public function setPrices(array $prices)
85
    {
86
        if ($this->prices !== []) {
87
            throw new \Exception('cannot reassign prices for plan');
88
        }
89
        $this->prices = $prices;
90
    }
91
92
    /**
93
     * Calculate charges for given action.
94
     * @param ActionInterface $action
95
     * @return Charge[]
96
     */
97 2
    public function calculateCharges(ActionInterface $action)
98
    {
99 2
        $charges = [];
100 2
        foreach ($this->prices as $price) {
101 2
            $charge = $action->calculateCharge($price);
102 2
            if ($charge !== null) {
103 2
                $charges[] = $charge;
104
            }
105
        }
106
107 2
        return $charges;
108
    }
109
110
    public function jsonSerialize()
111
    {
112
        return get_object_vars($this);
113
    }
114
}
115