Total Complexity | 11 |
Total Lines | 78 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
21 | class Aggregator implements AggregatorInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var BillInterface[] |
||
25 | */ |
||
26 | protected $bills = []; |
||
27 | |||
28 | /** |
||
29 | * @var GeneralizerInterface |
||
30 | */ |
||
31 | protected $generalizer; |
||
32 | |||
33 | 1 | public function __construct(GeneralizerInterface $generalizer) |
|
34 | { |
||
35 | 1 | $this->generalizer = $generalizer; |
|
36 | 1 | } |
|
37 | |||
38 | 1 | public function aggregateCharges(array $charges) |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Aggregate arrays of bills. |
||
58 | * @param BillInterface[] $bills |
||
59 | * @param BillInterface[] $others |
||
60 | * @return BillInterface[] |
||
61 | */ |
||
62 | 1 | public function aggregateBills(array $bills, array $others) |
|
63 | { |
||
64 | 1 | foreach ($others as $bill) { |
|
65 | 1 | $uid = $bill->getUniqueString(); |
|
66 | 1 | if (empty($bills[$uid])) { |
|
67 | 1 | $bills[$uid] = $bill; |
|
68 | } else { |
||
69 | 1 | $bills[$uid] = $this->aggregateBill($bills[$uid], $bill); |
|
70 | } |
||
71 | } |
||
72 | |||
73 | 1 | return $bills; |
|
74 | } |
||
75 | |||
76 | 1 | public function aggregateBill(BillInterface $first, BillInterface $other) |
|
77 | { |
||
78 | 1 | return new Bill( |
|
79 | 1 | null, |
|
80 | 1 | $first->getType(), |
|
81 | 1 | $first->getTime(), |
|
82 | 1 | $this->aggregateSum($first->getSum(), $other->getSum()), |
|
83 | 1 | $this->aggregateQuantity($first->getQuantity(), $other->getQuantity()), |
|
84 | 1 | $first->getCustomer(), |
|
85 | 1 | $first->getTarget(), |
|
86 | 1 | $first->getPlan(), |
|
87 | 1 | array_merge($first->getCharges(), $other->getCharges()) |
|
88 | ); |
||
89 | } |
||
90 | |||
91 | 1 | public function aggregateSum(Money $first, Money $other) |
|
92 | { |
||
93 | 1 | return $first->add($other); |
|
94 | } |
||
95 | |||
96 | 1 | public function aggregateQuantity(QuantityInterface $first, QuantityInterface $other) |
|
99 | } |
||
100 | } |
||
101 |