Completed
Push — master ( d26128...b08c4e )
by Andrii
03:02
created

Plan::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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-2018, 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\charge\Charge;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\customer\CustomerInterface;
17
use hiqdev\php\billing\price\PriceInterface;
18
19
/**
20
 * Tariff Plan.
21
 *
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class Plan implements PlanInterface
25
{
26
    /**
27
     * @var int
28
     */
29
    protected $id;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * @var Plan|null
38
     * XXX not sure to implement
39
     */
40
    protected $parent;
41
42
    /**
43
     * @var CustomerInterface
44
     */
45
    protected $seller;
46
47
    /**
48
     * @var PriceInterface[]
49
     */
50
    protected $prices;
51
52
    /**
53
     * @param int $id
54
     * @param string $name
55
     * @param CustomerInterface|null $seller
56
     * @param PriceInterface[] $prices
57
     */
58 1
    public function __construct(
59
                            $id,
60
                            $name,
61
        CustomerInterface   $seller = null,
62
                            $prices = null
63
    ) {
64 1
        $this->id = $id;
65 1
        $this->name = $name;
66 1
        $this->seller = $seller;
67 1
        $this->prices = $prices;
68 1
    }
69
70
    public function getUniqueId()
71
    {
72
        return $this->getId();
73
    }
74
75
    /**
76
     * @return int|string
77
     */
78
    public function getId()
79
    {
80
        return $this->id;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getName()
87
    {
88
        return $this->name;
89
    }
90
91
    /**
92
     * @return CustomerInterface
93
     */
94
    public function getSeller(): ?CustomerInterface
95
    {
96
        return $this->seller;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getParent(): ?PlanInterface
103
    {
104
        return $this->parent;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parent also could return the type hiqdev\php\billing\plan\Plan which is incompatible with the documented return type string.
Loading history...
105
    }
106
107
    /**
108
     * @return bool
109
     */
110 2
    public function hasPrices()
111
    {
112 2
        return $this->prices !== null;
113
    }
114
115
    /**
116
     * @return PriceInterface[]
117
     */
118
    public function getPrices()
119
    {
120
        return $this->prices;
121
    }
122
123
    /**
124
     * @param PriceInterface[] $prices
125
     */
126
    public function setPrices(array $prices)
127
    {
128
        if ($this->hasPrices()) {
129
            throw new \Exception('cannot reassign prices for plan');
130
        }
131
        $this->prices = $prices;
132
    }
133
134
    /**
135
     * Calculate charges for given action.
136
     * @param ActionInterface $action
137
     * @return Charge[]|ChargeInterface[]
138
     */
139 4
    public function calculateCharges(ActionInterface $action)
140
    {
141 4
        $result = [];
142 4
        foreach ($this->prices as $price) {
143 4
            $charges = $price->calculateCharges($action);
144 4
            if ($charges) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $charges of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145 4
                $result = array_merge($result, $charges);
146
            }
147
        }
148
149 4
        return $result;
150
    }
151
152
    public function jsonSerialize()
153
    {
154
        return get_object_vars($this);
155
    }
156
}
157