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\action; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
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\EntityInterface; |
18
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
19
|
|
|
use hiqdev\php\billing\sale\SaleInterface; |
20
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
21
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
22
|
|
|
use hiqdev\php\units\QuantityInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Chargeable Action. |
26
|
|
|
* |
27
|
|
|
* @see ActionInterface |
28
|
|
|
* |
29
|
|
|
* @author Andrii Vasyliev <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractAction implements ActionInterface, EntityInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $id; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var TypeInterface |
40
|
|
|
*/ |
41
|
|
|
protected $type; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var TargetInterface |
45
|
|
|
*/ |
46
|
|
|
protected $target; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var QuantityInterface |
50
|
|
|
*/ |
51
|
|
|
protected $quantity; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var CustomerInterface |
55
|
|
|
*/ |
56
|
|
|
protected $customer; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var SaleInterface |
60
|
|
|
*/ |
61
|
|
|
protected $sale; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var DateTimeImmutable |
65
|
|
|
*/ |
66
|
|
|
protected $time; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var ActionInterface |
70
|
|
|
*/ |
71
|
|
|
protected $parent; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param TypeInterface $type |
75
|
|
|
* @param TargetInterface $target |
76
|
|
|
* @param QuantityInterface $quantity |
77
|
|
|
* @param CustomerInterface $customer |
78
|
|
|
* @param SaleInterface $sale |
79
|
|
|
* @param DateTimeImmutable $time |
80
|
|
|
* @param ActionInterface $parent |
81
|
|
|
*/ |
82
|
17 |
|
public function __construct( |
83
|
|
|
$id, |
84
|
|
|
TypeInterface $type, |
85
|
|
|
TargetInterface $target, |
86
|
|
|
QuantityInterface $quantity, |
87
|
|
|
CustomerInterface $customer, |
88
|
|
|
DateTimeImmutable $time, |
89
|
|
|
SaleInterface $sale = null, |
90
|
|
|
ActionInterface $parent = null |
91
|
|
|
) { |
92
|
17 |
|
$this->id = $id; |
93
|
17 |
|
$this->type = $type; |
94
|
17 |
|
$this->target = $target; |
95
|
17 |
|
$this->quantity = $quantity; |
96
|
17 |
|
$this->customer = $customer; |
97
|
17 |
|
$this->time = $time; |
98
|
17 |
|
$this->sale = $sale; |
99
|
17 |
|
$this->parent = $parent; |
100
|
17 |
|
} |
101
|
|
|
|
102
|
|
|
public function createSubaction(CustomerInterface $customer) |
103
|
|
|
{ |
104
|
|
|
return new static(null, $this->type, $this->target, $this->quantity, $customer, $this->time, $this->sale, $this); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getId() |
111
|
|
|
{ |
112
|
|
|
return $this->id; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function getCustomer(): CustomerInterface |
119
|
|
|
{ |
120
|
1 |
|
return $this->customer; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
17 |
|
public function getTarget(): TargetInterface |
127
|
|
|
{ |
128
|
17 |
|
return $this->target; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
17 |
|
public function getType(): TypeInterface |
135
|
|
|
{ |
136
|
17 |
|
return $this->type; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
17 |
|
public function getQuantity(): QuantityInterface |
143
|
|
|
{ |
144
|
17 |
|
return $this->quantity; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
2 |
|
public function getSale() |
151
|
|
|
{ |
152
|
2 |
|
return $this->sale; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
6 |
|
public function getTime(): DateTimeImmutable |
159
|
|
|
{ |
160
|
6 |
|
return $this->time; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function setTime(DateTimeImmutable $time) |
164
|
|
|
{ |
165
|
|
|
$this->time = $time; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function getParent() |
172
|
|
|
{ |
173
|
|
|
return $this->parent; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function hasParent() |
180
|
|
|
{ |
181
|
|
|
return $this->parent !== null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function hasId() |
185
|
|
|
{ |
186
|
|
|
return $this->id !== null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function setId($id) |
190
|
|
|
{ |
191
|
|
|
if ((string) $this->id === (string) $id) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
if ($this->hasId()) { |
195
|
|
|
throw new \Exception('cannot reassign action id'); |
196
|
|
|
} |
197
|
|
|
$this->id = $id; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function hasSale() |
201
|
|
|
{ |
202
|
|
|
return $this->sale !== null; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function setSale(SaleInterface $sale) |
206
|
|
|
{ |
207
|
|
|
if ($this->hasSale()) { |
208
|
|
|
throw new \Exception('cannot reassign sale for action'); |
209
|
|
|
} |
210
|
|
|
$this->sale = $sale; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
|
|
public function jsonSerialize() |
217
|
|
|
{ |
218
|
|
|
return get_object_vars($this); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param PriceInterface $price |
223
|
|
|
* @return ChargeInterface|Charge|null |
224
|
|
|
*/ |
225
|
17 |
|
public function calculateCharge(PriceInterface $price): ?ChargeInterface |
226
|
|
|
{ |
227
|
17 |
|
if (!$this->isApplicable($price)) { |
228
|
4 |
|
return null; |
229
|
|
|
} |
230
|
|
|
|
231
|
17 |
|
$usage = $price->calculateUsage($this->getQuantity()); |
232
|
17 |
|
if ($usage === null) { |
233
|
4 |
|
return null; |
234
|
|
|
} |
235
|
|
|
|
236
|
13 |
|
$sum = $price->calculateSum($this->getQuantity()); |
237
|
13 |
|
if ($sum === null) { |
238
|
|
|
return null; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/* sorry, debugging facility |
|
|
|
|
242
|
|
|
* var_dump([ |
243
|
|
|
'unit' => $usage->getUnit()->getName(), |
244
|
|
|
'quantity' => $usage->getQuantity(), |
245
|
|
|
'price' => $price->calculatePrice($usage)->getAmount(), |
246
|
|
|
'sum' => $sum->getAmount(), |
247
|
|
|
]);*/ |
248
|
|
|
|
249
|
13 |
|
return new Charge(null, $this, $price, $usage, $sum); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
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.