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\price; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
14
|
|
|
use hiqdev\php\billing\EntityInterface; |
15
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
16
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
17
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
18
|
|
|
use hiqdev\php\units\QuantityInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Price. |
22
|
|
|
* @see PriceInterface |
23
|
|
|
* By default Price is applicable when same target and same type as Action. |
24
|
|
|
* But it can be different e.g. same price for all targets when certain type. |
25
|
|
|
* |
26
|
|
|
* @author Andrii Vasyliev <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractPrice implements PriceInterface, EntityInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var integer |
32
|
|
|
*/ |
33
|
|
|
protected $id; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TypeInterface |
37
|
|
|
*/ |
38
|
|
|
protected $type; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TargetInterface |
42
|
|
|
*/ |
43
|
|
|
protected $target; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var PlanInterface |
47
|
|
|
*/ |
48
|
|
|
protected $plan; |
49
|
|
|
|
50
|
7 |
View Code Duplication |
public function __construct( |
|
|
|
|
51
|
|
|
$id, |
52
|
|
|
TypeInterface $type, |
53
|
|
|
TargetInterface $target, |
54
|
|
|
PlanInterface $plan = null |
55
|
|
|
) { |
56
|
7 |
|
$this->id = $id; |
57
|
7 |
|
$this->type = $type; |
58
|
7 |
|
$this->target = $target; |
59
|
7 |
|
$this->plan = $plan; |
60
|
7 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
2 |
|
public function getId() |
66
|
|
|
{ |
67
|
2 |
|
return $this->id; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
6 |
|
public function getType() |
74
|
|
|
{ |
75
|
6 |
|
return $this->type; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
3 |
|
public function getTarget() |
82
|
|
|
{ |
83
|
3 |
|
return $this->target; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function getPlan() |
90
|
|
|
{ |
91
|
1 |
|
return $this->plan; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
* Default sum calculation method: sum = price * usage. |
97
|
|
|
*/ |
98
|
1 |
View Code Duplication |
public function calculateSum(QuantityInterface $quantity) |
|
|
|
|
99
|
|
|
{ |
100
|
1 |
|
$usage = $this->calculateUsage($quantity); |
101
|
1 |
|
if ($usage === null) { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$price = $this->calculatePrice($quantity); |
106
|
1 |
|
if ($price === null) { |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $price->multiply($usage->getQuantity()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function create(array $data) |
114
|
|
|
{ |
115
|
|
|
return new SinglePrice($data['id'], $data['target']); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function jsonSerialize() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
'id' => $this->id, |
122
|
|
|
'type' => $this->type, |
123
|
|
|
'target' => $this->target, |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
5 |
|
public function isApplicable(ActionInterface $action) |
131
|
|
|
{ |
132
|
5 |
|
return $this->target->equals($action->getTarget()) && |
133
|
5 |
|
$this->type->equals($action->getType()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
abstract public function calculateUsage(QuantityInterface $quantity); |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
abstract public function calculatePrice(QuantityInterface $action); |
145
|
|
|
} |
146
|
|
|
|
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.