-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAbstractPrice.php
executable file
·182 lines (159 loc) · 4.33 KB
/
AbstractPrice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* PHP Billing Library
*
* @link https://github.com/hiqdev/php-billing
* @package php-billing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
*/
namespace hiqdev\php\billing\price;
use hiqdev\php\billing\action\ActionInterface;
use hiqdev\php\billing\charge\ChargeModifier;
use hiqdev\php\billing\charge\SettableChargeModifierTrait;
use hiqdev\php\billing\Exception\CannotReassignException;
use hiqdev\php\billing\plan\PlanInterface;
use hiqdev\php\billing\target\TargetInterface;
use hiqdev\php\billing\type\TypeInterface;
use hiqdev\php\units\QuantityInterface;
use Money\Money;
/**
* Price.
* @see PriceInterface
* By default Price is applicable when same target and same type as Action.
* But it can be different e.g. same price for all targets when certain type.
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
abstract class AbstractPrice implements PriceInterface, ChargeModifier
{
use SettableChargeModifierTrait;
/**
* @var integer
*/
protected $id;
/**
* @var TypeInterface
*/
protected $type;
/**
* @var TargetInterface
*/
protected $target;
/**
* @var PlanInterface|null
*/
protected $plan;
public function __construct(
$id,
TypeInterface $type,
TargetInterface $target,
PlanInterface $plan = null
) {
$this->id = $id;
$this->type = $type;
$this->target = $target;
$this->plan = $plan;
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return $this->type;
}
/**
* {@inheritdoc}
*/
public function getTarget()
{
return $this->target;
}
/**
* {@inheritdoc}
*/
public function getPlan(): ?PlanInterface
{
return $this->plan;
}
/**
* {@inheritdoc}
*/
public function hasPlan()
{
return $this->plan !== null;
}
/**
* {@inheritdoc}
*/
public function setPlan(PlanInterface $plan)
{
if ($this->hasPlan()) {
throw new CannotReassignException('price plan');
}
$this->plan = $plan;
}
/**
* {@inheritdoc}
* Default sum calculation method: sum = price * usage.
*/
public function calculateSum(QuantityInterface $quantity): ?Money
{
$usage = $this->calculateUsage($quantity);
if ($usage === null) {
return null;
}
$price = $this->calculatePrice($quantity);
if ($price === null) {
return null;
}
if ($price->isZero()) {
return $price;
}
$stringQty = sprintf('%.14F', $usage->getQuantity());
$sum = $price->multiply($stringQty, Money::ROUND_HALF_UP);
if ($sum->isZero() && !$quantity->isZero()) {
// If there is any usage, but sum is zero, we should charge at least 1 cent
$sum = $price->multiply($stringQty, Money::ROUND_UP);
}
return $sum;
}
/**
* What purpose of this method? Because it looks like duplicate of PriceHydrator::extract()
* Where we are using the result of this method?
* Magic calls can't be determined and I don't know what can be broken if we change the method result.
* Which structure must have the result, because array can contain anything?
*
* @return array
*/
public function jsonSerialize(): array
{
$res = array_filter(get_object_vars($this));
unset($res['plan']);
return $res;
}
/**
* {@inheritdoc}
*/
public function isApplicable(ActionInterface $action): bool
{
/* sorry, debugging facility
var_dump([
'action.target' => $action->getTarget(),
'price.target' => $this->getTarget(),
'action.type' => $action->getType(),
'price.type' => $this->getType(),
'target matches' => $action->getTarget()->matches($this->getTarget()),
'type matches' => $action->getType()->matches($this->getType()),
]); */
return $action->getTarget()->matches($this->getTarget()) &&
$action->getType()->matches($this->getType());
}
}