-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAggregator.php
134 lines (116 loc) · 3.85 KB
/
Aggregator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* PHP Billing Library
*
* @link https://github.com/hiqdev/php-billing
* @package php-billing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
*/
namespace hiqdev\php\billing\tools;
use hiqdev\php\billing\action\UsageInterval;
use hiqdev\php\billing\bill\Bill;
use hiqdev\php\billing\bill\BillInterface;
use hiqdev\php\billing\charge\ChargeInterface;
use hiqdev\php\billing\charge\GeneralizerInterface;
use hiqdev\php\units\QuantityInterface;
use Money\Money;
/**
* @author Andrii Vasyliev <[email protected]>
*/
class Aggregator implements AggregatorInterface
{
/**
* @var GeneralizerInterface
*/
protected $generalizer;
public function __construct(GeneralizerInterface $generalizer)
{
$this->generalizer = $generalizer;
}
/**
* Aggregates given Charges to Bills. Then aggregates them again with DB.
* @param ChargeInterface[]|ChargeInterface[][] $charges
* @return BillInterface[]
*/
public function aggregateCharges(array $charges): array
{
$bills = [];
foreach ($charges as $charge) {
if (is_array($charge)) {
$others = $this->aggregateCharges($charge);
} elseif ($charge instanceof ChargeInterface) {
$others = [$this->generalizer->createBill($charge)];
} else {
throw new AggregationException('Not a Charge given to Aggregator');
}
$bills = $this->aggregateBills($bills, $others);
}
return $bills;
}
/**
* Aggregate arrays of bills.
* @param BillInterface[] $bills
* @param BillInterface[] $others
* @return BillInterface[]
*/
protected function aggregateBills(array $bills, array $others): array
{
foreach ($others as $bill) {
$uid = $bill->getUniqueString();
if (empty($bills[$uid])) {
$bills[$uid] = $bill;
} else {
$bills[$uid] = $this->aggregateBill($bills[$uid], $bill);
}
}
return $bills;
}
protected function aggregateBill(BillInterface $first, BillInterface $other): BillInterface
{
$bill = new Bill(
$this->aggregateId($first, $other),
$first->getType(),
$first->getTime(),
$this->aggregateSum($first, $other),
$this->aggregateQuantity($first, $other),
$first->getCustomer(),
$first->getTarget(),
$first->getPlan(),
array_merge($first->getCharges(), $other->getCharges())
);
$bill->setUsageInterval($this->aggregateUsageInterval($first, $other));
return $bill;
}
protected function aggregateUsageInterval(BillInterface $first, BillInterface $other): UsageInterval
{
return $first->getUsageInterval()->extend($other->getUsageInterval());
}
/**
* @return string|int|null
*/
protected function aggregateId(BillInterface $first, BillInterface $other)
{
if ($first->getId() === null) {
return $other->getId();
}
if ($other->getId() === null) {
return $first->getId();
}
if ($first->getId() === $other->getId()) {
return $other->getId();
}
throw new AggregationException('cannot aggregate bills with different IDs');
}
protected function aggregateSum(BillInterface $first, BillInterface $other): Money
{
return $first->getSum()->add($other->getSum());
}
protected function aggregateQuantity(BillInterface $first, BillInterface $other): QuantityInterface
{
if ($first->getQuantity()->isConvertible($other->getQuantity()->getUnit())) {
return $first->getQuantity()->add($other->getQuantity());
}
return $first->getQuantity();
}
}