|
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\charge\modifiers; |
|
12
|
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
|
14
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
|
15
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Maximum; |
|
16
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Minimum; |
|
17
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\MonthPeriod; |
|
18
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Step; |
|
19
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\YearPeriod; |
|
20
|
|
|
use Money\Money; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Growing discount. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class GrowingDiscount extends FixedDiscount |
|
28
|
|
|
{ |
|
29
|
|
|
const PERIOD = 'period'; |
|
30
|
|
|
const STEP = 'step'; |
|
31
|
|
|
const MIN = 'min'; |
|
32
|
|
|
const MAX = 'max'; |
|
33
|
|
|
|
|
34
|
5 |
|
public function __construct($step, $min = null, array $addons = []) |
|
35
|
|
|
{ |
|
36
|
5 |
|
Modifier::__construct($addons); |
|
37
|
5 |
|
$this->addAddon(self::STEP, new Step($step)); |
|
38
|
5 |
|
if ($min) { |
|
39
|
|
|
$this->min($min); |
|
40
|
|
|
} |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
public function isAbsolute() |
|
44
|
|
|
{ |
|
45
|
4 |
|
return $this->getStep()->isAbsolute(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
public function getStep() |
|
49
|
|
|
{ |
|
50
|
4 |
|
return $this->getAddon(self::STEP); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
public function getMin() |
|
54
|
|
|
{ |
|
55
|
4 |
|
return $this->getAddon(self::MIN); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
public function getMax() |
|
59
|
|
|
{ |
|
60
|
4 |
|
return $this->getAddon(self::MAX); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function min($min) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->addExtremum(self::MIN, new Minimum($min)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function max($max) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->addExtremum(self::MAX, new Maximum($max)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function addExtremum($name, AddonInterface $addon) |
|
74
|
|
|
{ |
|
75
|
|
|
$value = $addon->getValue(); |
|
76
|
|
|
if ($value instanceof Money) { |
|
77
|
|
|
if ($this->isRelative()) { |
|
78
|
|
|
throw new \Exception("'$name' must be given as percentage because step is percentage"); |
|
79
|
|
|
} |
|
80
|
|
|
} elseif ($this->isAbsolute()) { |
|
81
|
|
|
throw new \Exception("'$name' must be money because step is money"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->addAddon($name, $addon); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
public function getPeriod() |
|
88
|
|
|
{ |
|
89
|
4 |
|
return $this->getAddon(self::PERIOD); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
public function everyMonth($num = 1) |
|
93
|
|
|
{ |
|
94
|
4 |
|
return $this->addAddon(self::PERIOD, new MonthPeriod($num)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function everyYear($num = 1) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->addAddon(self::PERIOD, new YearPeriod($num)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
4 |
|
public function getValue(ChargeInterface $charge = null) |
|
103
|
|
|
{ |
|
104
|
4 |
|
$time = $charge ? $charge->getAction()->getTime() : new DateTimeImmutable(); |
|
105
|
4 |
|
$num = $this->countPeriodsPassed($time); |
|
106
|
|
|
|
|
107
|
4 |
|
return $this->getStep()->calculateFor($num, $this->getMin(), $this->getMax()); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
protected function countPeriodsPassed(DateTimeImmutable $time) |
|
111
|
|
|
{ |
|
112
|
4 |
|
$since = $this->getSince(); |
|
113
|
4 |
|
if ($since === null) { |
|
114
|
|
|
throw new \Exception('no since given for growing discount'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
4 |
|
$period = $this->getPeriod(); |
|
118
|
4 |
|
if ($period === null) { |
|
119
|
|
|
throw new \Exception('no period given for growing discount'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
4 |
|
return $period->countPeriodsPassed($since->getValue(), $time); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: