Completed
Push — master ( 8debe9...7ec561 )
by Dmitry
02:47
created

Aggregator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 83
rs 10
c 0
b 0
f 0
ccs 34
cts 35
cp 0.9714
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A aggregateBills() 0 12 3
A aggregateCharges() 0 16 4
A aggregateQuantity() 0 3 1
A aggregateSum() 0 3 1
A aggregateBill() 0 12 1
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;
12
13
use hiqdev\php\billing\bill\Bill;
14
use hiqdev\php\billing\bill\BillInterface;
15
use hiqdev\php\units\QuantityInterface;
16
use Money\Money;
17
18
/**
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
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)
39
    {
40 1
        $bills = [];
41 1
        foreach ($charges as $charge) {
42 1
            if (is_array($charge)) {
43 1
                $others = $this->aggregateCharges($charge);
44 1
            } elseif ($charge instanceof ChargeInterface) {
45 1
                $others = [$this->generalizer->createBill($charge)];
46
            } else {
47
                throw new \Exception('not a Charge given to Aggregator');
48
            }
49
50 1
            $bills = $this->aggregateBills($bills, $others);
51
        }
52
53 1
        return $bills;
54
    }
55
56
    /**
57
     * Aggregate arrays of bills.
58
     * @param BillInterface[] $bills
59
     * @param BillInterface[] $others
60
     * @return BillInterface[]
61
     */
62 1
    protected function aggregateBills(array $bills, array $others): array
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
    /**
77
     * @param BillInterface $first
78
     * @param BillInterface $other
79
     * @return BillInterface
80
     */
81 1
    protected function aggregateBill(BillInterface $first, BillInterface $other): BillInterface
82
    {
83 1
        return new Bill(
84 1
            null,
85 1
            $first->getType(),
86 1
            $first->getTime(),
87 1
            $this->aggregateSum($first, $other),
88 1
            $this->aggregateQuantity($first, $other),
89 1
            $first->getCustomer(),
90 1
            $first->getTarget(),
91 1
            $first->getPlan(),
92 1
            array_merge($first->getCharges(), $other->getCharges())
93
        );
94
    }
95
96 1
    protected function aggregateSum(BillInterface $first, BillInterface $other): Money
97
    {
98 1
        return $first->getSum()->add($other->getSum());
99
    }
100
101 1
    protected function aggregateQuantity(BillInterface $first, BillInterface $other): QuantityInterface
102
    {
103 1
        return $first->getQuantity()->add($other->getQuantity());
104
    }
105
}
106