|
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
|
25 |
|
public function __construct( |
|
53
|
|
|
$id, |
|
54
|
|
|
TypeInterface $type, |
|
55
|
|
|
TargetInterface $target, |
|
56
|
|
|
PlanInterface $plan = null |
|
57
|
|
|
) { |
|
58
|
25 |
|
$this->id = $id; |
|
59
|
25 |
|
$this->type = $type; |
|
60
|
25 |
|
$this->target = $target; |
|
61
|
25 |
|
$this->plan = $plan; |
|
62
|
25 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function getId() |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $this->id; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
17 |
|
public function getType() |
|
76
|
|
|
{ |
|
77
|
17 |
|
return $this->type; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
17 |
|
public function getTarget() |
|
84
|
|
|
{ |
|
85
|
17 |
|
return $this->target; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getPlan() |
|
92
|
|
|
{ |
|
93
|
|
|
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 static function create(array $data) |
|
136
|
|
|
{ |
|
137
|
|
|
return new SinglePrice($data['id'], $data['target']); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function jsonSerialize() |
|
141
|
|
|
{ |
|
142
|
|
|
return [ |
|
143
|
|
|
'id' => $this->id, |
|
144
|
|
|
'type' => $this->type, |
|
145
|
|
|
'target' => $this->target, |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
15 |
|
public function isApplicable(ActionInterface $action): bool |
|
153
|
|
|
{ |
|
154
|
|
|
/* sorry, debugging facility |
|
|
|
|
|
|
155
|
|
|
* var_dump([ |
|
156
|
|
|
'action.target' => $action->getTarget(), |
|
157
|
|
|
'this.target' => $this->getTarget(), |
|
158
|
|
|
'action.type' => $action->getType(), |
|
159
|
|
|
'this.type' => $this->getType(), |
|
160
|
|
|
'target matches' => $action->getTarget()->matches($this->getTarget()), |
|
161
|
|
|
'type matches' => $action->getType()->matches($this->getType()), |
|
162
|
|
|
]);*/ |
|
163
|
15 |
|
return $action->getTarget()->matches($this->getTarget()) && |
|
164
|
15 |
|
$action->getType()->matches($this->getType()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** {@inheritdoc} */ |
|
168
|
2 |
|
public function calculateCharges(ActionInterface $action): array |
|
169
|
|
|
{ |
|
170
|
2 |
|
$charge = $action->calculateCharge($this); |
|
171
|
2 |
|
if ($this instanceof ChargeModifier) { |
|
172
|
|
|
return $this->modifyCharge($charge, $action); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
2 |
|
return $charge ? [$charge] : []; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.