Completed
Push — master ( 3d080b...428142 )
by Andrii
02:09
created

Plan::getUniqueId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function getUniqueId()
66
    {
67
        return $this->getId();
68
    }
69
70
    /**
71
     * @return int|string
72
     */
73
    public function getId()
74
    {
75
        return $this->id;
76
    }
77
78
    /**
79
     * @return PriceInterface[]
80
     */
81
    public function getPrices()
82
    {
83
        return $this->prices;
84
    }
85
86
    /**
87
     * @param PriceInterface[] $prices
88
     */
89
    public function setPrices(array $prices)
90
    {
91
        if ($this->prices !== []) {
92
            throw new \Exception('cannot reassign prices for plan');
93
        }
94
        $this->prices = $prices;
95
    }
96
97
    /**
98
     * Calculate charges for given action.
99
     * @param ActionInterface $action
100
     * @return Charge[]
101
     */
102 2
    public function calculateCharges(ActionInterface $action)
103
    {
104 2
        $charges = [];
105 2
        foreach ($this->prices as $price) {
106 2
            $charge = $action->calculateCharge($price);
107 2
            if ($charge !== null) {
108 2
                $charges[] = $charge;
109
            }
110
        }
111
112 2
        return $charges;
113
    }
114
115
    public function jsonSerialize()
116
    {
117
        return get_object_vars($this);
118
    }
119
}
120