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\order; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\bill\BillRepositoryInterface; |
14
|
|
|
use hiqdev\php\billing\tools\AggregatorInterface; |
15
|
|
|
use hiqdev\php\billing\tools\MergerInterface; |
16
|
|
|
use hiqdev\php\billing\tools\DbMergingAggregator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Billing calculates and saves bills for given order. |
20
|
|
|
* |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Billing implements BillingInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var CalculatorInterface |
27
|
|
|
*/ |
28
|
|
|
protected $calculator; |
29
|
|
|
/** |
30
|
|
|
* @var AggregatorInterface |
31
|
|
|
*/ |
32
|
|
|
protected $aggregator; |
33
|
|
|
/** |
34
|
|
|
* @var AggregatorInterface |
35
|
|
|
*/ |
36
|
|
|
protected $repoAggregator; |
37
|
|
|
/** |
38
|
|
|
* @var MergerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $merger; |
41
|
|
|
/** |
42
|
|
|
* @var BillRepositoryInterface |
43
|
|
|
*/ |
44
|
|
|
protected $repository; |
45
|
|
|
/** |
46
|
|
|
* @var CollectorInterface |
47
|
|
|
*/ |
48
|
|
|
protected $collector; |
49
|
|
|
|
50
|
|
|
public function __construct( |
51
|
|
|
CalculatorInterface $calculator, |
52
|
|
|
AggregatorInterface $aggregator, |
53
|
|
|
MergerInterface $merger, |
54
|
|
|
?BillRepositoryInterface $repository, |
55
|
|
|
?CollectorInterface $collector |
56
|
|
|
) { |
57
|
|
|
$this->calculator = $calculator; |
58
|
|
|
$this->aggregator = $aggregator; |
59
|
|
|
$this->merger = $merger; |
60
|
|
|
$this->repository = $repository; |
61
|
|
|
$this->collector = $collector ?? new Collector(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function calculate($source, DateTimeImmutable $time = null): array |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$charges = $this->calculateCharges($source, $time); |
67
|
|
|
$bills = $this->aggregator->aggregateCharges($charges); |
68
|
|
|
|
69
|
|
|
return $this->merger->mergeBills($bills); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function perform($source, DateTimeImmutable $time = null): array |
73
|
|
|
{ |
74
|
|
|
$charges = $this->calculateCharges($source, $time); |
75
|
|
|
$bills = $this->getRepoAggregator()->aggregateCharges($charges); |
76
|
|
|
|
77
|
|
|
return $this->saveBills($bills); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function calculateCharges($source, DateTimeImmutable $time = null): array |
81
|
|
|
{ |
82
|
|
|
$order = $this->collector->collect($source, $time); |
83
|
|
|
|
84
|
|
|
return $this->calculator->calculateOrder($order); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getRepoAggregator(): AggregatorInterface |
88
|
|
|
{ |
89
|
|
|
if ($this->repoAggregator === null) { |
90
|
|
|
$this->repoAggregator = new DbMergingAggregator($this->aggregator, $this->repository, $this->merger); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->repoAggregator; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param BillInterface[] $bills |
98
|
|
|
* @return BillInterface[] |
99
|
|
|
*/ |
100
|
|
|
private function saveBills(array $bills): array |
101
|
|
|
{ |
102
|
|
|
$res = []; |
103
|
|
|
foreach ($bills as $key => $bill) { |
104
|
|
|
$res[$key] = $this->repository->save($bill); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $res; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|