|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BillingBoss; |
|
4
|
|
|
|
|
5
|
|
|
use BillingBoss\Interpreter\FlatRateBillInterpreter; |
|
6
|
|
|
use BillingBoss\Interpreter\PercentageBillInterpreter; |
|
7
|
|
|
use BillingBoss\Interpreter\CappedBillInterpreter; |
|
8
|
|
|
use BillingBoss\Interpreter\ProgressiveBillInterpreter; |
|
9
|
|
|
use BillingBoss\Interpreter\SteppedBillInterpreter; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* A billing interpreter aggregator |
|
13
|
|
|
* |
|
14
|
|
|
* @package BillingBoss |
|
15
|
|
|
* @link https://github.com/ranskills/billing-boss-php |
|
16
|
|
|
* @copyright Copyright (c) 2018 Ransford Ako Okpoti |
|
17
|
|
|
* @license Refer to the LICENSE distributed with this library |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
final class BillingBoss |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var BillInterpreter[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $interpreters = []; |
|
26
|
|
|
|
|
27
|
5 |
|
public static function getInterpreters() |
|
28
|
|
|
{ |
|
29
|
5 |
|
if (!empty(self::$interpreters)) { |
|
30
|
4 |
|
return self::$interpreters; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
self::addInterpreter(new CappedBillInterpreter()); |
|
34
|
1 |
|
self::addInterpreter(new FlatRateBillInterpreter()); |
|
35
|
1 |
|
self::addInterpreter(new PercentageBillInterpreter()); |
|
36
|
1 |
|
self::addInterpreter(new ProgressiveBillInterpreter()); |
|
37
|
1 |
|
self::addInterpreter(new SteppedBillInterpreter()); |
|
38
|
|
|
|
|
39
|
1 |
|
return self::$interpreters; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public static function addInterpreter(BillInterpreter $interpreter) |
|
43
|
|
|
{ |
|
44
|
2 |
|
self::$interpreters[get_class($interpreter)] = $interpreter; |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public static function removeInterpreter(BillInterpreter $interpreter) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$key = get_class($interpreter); |
|
50
|
1 |
|
if (isset(self::$interpreters[$key])) { |
|
51
|
1 |
|
unset(self::$interpreters[$key]); |
|
52
|
|
|
} |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param BillContext $context |
|
57
|
|
|
* @return float |
|
58
|
|
|
* @throws Exception\Exception |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public static function bill(BillContext $context): float |
|
61
|
|
|
{ |
|
62
|
3 |
|
$interpreters = self::getInterpreters(); |
|
63
|
|
|
|
|
64
|
3 |
|
foreach ($interpreters as $interpreter) { |
|
65
|
3 |
|
if ($interpreter->isValid($context)) { |
|
66
|
3 |
|
return $interpreter->interpret($context); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return 0.0; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|