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, 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\customer\CustomerInterface; |
16
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
17
|
|
|
use hiqdev\php\billing\sale\SaleInterface; |
18
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
19
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
20
|
|
|
use hiqdev\php\units\QuantityInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Chargeable Action. |
24
|
|
|
* |
25
|
|
|
* @see ActionInterface |
26
|
|
|
* |
27
|
|
|
* @author Andrii Vasyliev <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractAction implements ActionInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $id; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var TypeInterface |
38
|
|
|
*/ |
39
|
|
|
protected $type; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TargetInterface |
43
|
|
|
*/ |
44
|
|
|
protected $target; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var QuantityInterface |
48
|
|
|
*/ |
49
|
|
|
protected $quantity; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var CustomerInterface |
53
|
|
|
*/ |
54
|
|
|
protected $customer; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var SaleInterface |
58
|
|
|
*/ |
59
|
|
|
protected $sale; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var DateTimeImmutable |
63
|
|
|
*/ |
64
|
|
|
protected $time; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var ActionInterface |
68
|
|
|
*/ |
69
|
|
|
protected $parent; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param TypeInterface $type |
73
|
|
|
* @param TargetInterface $target |
74
|
|
|
* @param QuantityInterface $quantity |
75
|
|
|
* @param CustomerInterface $customer |
76
|
|
|
* @param SaleInterface $sale |
77
|
|
|
* @param DateTimeImmutable $time |
78
|
|
|
* @param ActionInterface $parent |
79
|
|
|
*/ |
80
|
5 |
View Code Duplication |
public function __construct( |
|
|
|
|
81
|
|
|
$id, |
82
|
|
|
TypeInterface $type, |
83
|
|
|
TargetInterface $target, |
84
|
|
|
QuantityInterface $quantity, |
85
|
|
|
CustomerInterface $customer = null, |
86
|
|
|
SaleInterface $sale = null, |
87
|
|
|
DateTimeImmutable $time = null, |
88
|
|
|
ActionInterface $parent = null |
89
|
|
|
) { |
90
|
5 |
|
$this->id = $id; |
91
|
5 |
|
$this->type = $type; |
92
|
5 |
|
$this->target = $target; |
93
|
5 |
|
$this->quantity = $quantity; |
94
|
5 |
|
$this->customer = $customer; |
95
|
5 |
|
$this->sale = $sale; |
96
|
5 |
|
$this->time = $time; |
97
|
5 |
|
$this->parent = $parent; |
98
|
5 |
|
} |
99
|
|
|
|
100
|
|
|
public function createSubaction(CustomerInterface $customer) |
101
|
|
|
{ |
102
|
|
|
return new static(null, $this->type, $this->target, $this->quantity, $customer, $this->sale, $this->time, $this); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getId() |
109
|
|
|
{ |
110
|
|
|
return $this->id; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
1 |
|
public function getCustomer() |
117
|
|
|
{ |
118
|
1 |
|
return $this->customer; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
4 |
|
public function getTarget() |
125
|
|
|
{ |
126
|
4 |
|
return $this->target; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
2 |
|
public function getType() |
133
|
|
|
{ |
134
|
2 |
|
return $this->type; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
5 |
|
public function getQuantity() |
141
|
|
|
{ |
142
|
5 |
|
return $this->quantity; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function getSale() |
149
|
|
|
{ |
150
|
|
|
return $this->sale; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function getTime() |
157
|
|
|
{ |
158
|
|
|
return $this->time; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function getParent() |
165
|
|
|
{ |
166
|
|
|
return $this->parent; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function hasParent() |
173
|
|
|
{ |
174
|
|
|
return $this->parent !== null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
View Code Duplication |
public function setId($id) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
if ($this->id === $id) { |
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
if ($this->id !== null) { |
183
|
|
|
throw new \Exception('cannot reassign action id'); |
184
|
|
|
} |
185
|
|
|
$this->id = $id; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function setSale(SaleInterface $sale) |
189
|
|
|
{ |
190
|
|
|
if ($this->sale !== null) { |
191
|
|
|
throw new \Exception('cannot reassign sale for action'); |
192
|
|
|
} |
193
|
|
|
$this->sale = $sale; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function jsonSerialize() |
200
|
|
|
{ |
201
|
|
|
return get_object_vars($this); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
5 |
|
*/ |
208
|
|
|
public function calculateCharge(PriceInterface $price) |
209
|
5 |
|
{ |
210
|
3 |
|
if (!$this->isApplicable($price)) { |
211
|
|
|
return null; |
212
|
|
|
} |
213
|
5 |
|
|
214
|
5 |
|
$usage = $price->calculateUsage($this->getQuantity()); |
215
|
1 |
|
if ($usage === null) { |
216
|
|
|
return null; |
217
|
|
|
} |
218
|
4 |
|
|
219
|
4 |
|
$sum = $price->calculateSum($this->getQuantity()); |
220
|
|
|
if ($sum === null) { |
221
|
|
|
return null; |
222
|
|
|
} |
223
|
4 |
|
|
224
|
|
|
return new Charge(null, $this, $price, $this->getTarget(), $usage, $sum); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
abstract public function isApplicable(PriceInterface $price); |
231
|
|
|
} |
232
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.