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\price; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
14
|
|
|
use hiqdev\php\billing\charge\ChargeModifier; |
15
|
|
|
use hiqdev\php\billing\EntityInterface; |
16
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
17
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
18
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
19
|
|
|
use hiqdev\php\units\QuantityInterface; |
20
|
|
|
use Money\Money; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Price. |
24
|
|
|
* @see PriceInterface |
25
|
|
|
* By default Price is applicable when same target and same type as Action. |
26
|
|
|
* But it can be different e.g. same price for all targets when certain type. |
27
|
|
|
* |
28
|
|
|
* @author Andrii Vasyliev <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
abstract class AbstractPrice implements PriceInterface, EntityInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
protected $id; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TypeInterface |
39
|
|
|
*/ |
40
|
|
|
protected $type; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var TargetInterface |
44
|
|
|
*/ |
45
|
|
|
protected $target; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var PlanInterface |
49
|
|
|
*/ |
50
|
|
|
protected $plan; |
51
|
|
|
|
52
|
26 |
|
public function __construct( |
53
|
|
|
$id, |
54
|
|
|
TypeInterface $type, |
55
|
|
|
TargetInterface $target, |
56
|
|
|
PlanInterface $plan = null |
57
|
|
|
) { |
58
|
26 |
|
$this->id = $id; |
59
|
26 |
|
$this->type = $type; |
60
|
26 |
|
$this->target = $target; |
61
|
26 |
|
$this->plan = $plan; |
62
|
26 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
2 |
|
public function getId() |
68
|
|
|
{ |
69
|
2 |
|
return $this->id; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
19 |
|
public function getType() |
76
|
|
|
{ |
77
|
19 |
|
return $this->type; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
19 |
|
public function getTarget() |
84
|
|
|
{ |
85
|
19 |
|
return $this->target; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function getPlan(): ?PlanInterface |
92
|
|
|
{ |
93
|
1 |
|
return $this->plan; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function hasPlan() |
100
|
|
|
{ |
101
|
|
|
return $this->plan !== null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function setPlan(PlanInterface $plan) |
108
|
|
|
{ |
109
|
|
|
if ($this->hasPlan()) { |
110
|
|
|
throw new \Exception('cannot reassign plan for price'); |
111
|
|
|
} |
112
|
|
|
$this->plan = $plan; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
* Default sum calculation method: sum = price * usage. |
118
|
|
|
*/ |
119
|
9 |
|
public function calculateSum(QuantityInterface $quantity): ?Money |
120
|
|
|
{ |
121
|
9 |
|
$usage = $this->calculateUsage($quantity); |
122
|
9 |
|
if ($usage === null) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
9 |
|
$price = $this->calculatePrice($quantity); |
127
|
9 |
|
if ($price === null) { |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/// TODO add configurable rounding mode later |
132
|
9 |
|
return $price->multiply($usage->getQuantity(), Money::ROUND_UP); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function jsonSerialize() |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
'id' => $this->id, |
139
|
|
|
'type' => $this->type, |
140
|
|
|
'target' => $this->target, |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
17 |
|
public function isApplicable(ActionInterface $action): bool |
148
|
|
|
{ |
149
|
|
|
/* sorry, debugging facility |
|
|
|
|
150
|
|
|
* var_dump([ |
151
|
|
|
'action.target' => $action->getTarget(), |
152
|
|
|
'this.target' => $this->getTarget(), |
153
|
|
|
'action.type' => $action->getType(), |
154
|
|
|
'this.type' => $this->getType(), |
155
|
|
|
'target matches' => $action->getTarget()->matches($this->getTarget()), |
156
|
|
|
'type matches' => $action->getType()->matches($this->getType()), |
157
|
|
|
]);*/ |
158
|
17 |
|
return $action->getTarget()->matches($this->getTarget()) && |
159
|
17 |
|
$action->getType()->matches($this->getType()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** {@inheritdoc} */ |
163
|
4 |
|
public function calculateCharges(ActionInterface $action): array |
164
|
|
|
{ |
165
|
4 |
|
$charge = $action->calculateCharge($this); |
166
|
4 |
|
if ($this instanceof ChargeModifier) { |
167
|
|
|
$charges = $this->modifyCharge($charge, $action); |
168
|
|
|
} else { |
169
|
|
|
$charges = $charge ? [$charge] : []; |
170
|
4 |
|
} |
171
|
|
|
|
172
|
|
|
if ($action->isFinished()) { |
173
|
|
|
foreach ($charges as $charge) { |
174
|
|
|
$charge->setFinished(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $charges; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.