Completed
Push — master ( a29fc9...07f98c )
by Andrii
06:20
created

Aggregator::aggregateSum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    public function __construct(GeneralizerInterface $generalizer)
34
    {
35
        $this->generalizer = $generalizer;
36
    }
37
38
    public function aggregateCharges(array $charges)
39
    {
40
        $bills = [];
41
        foreach ($charges as $charge) {
42
            if (is_array($charge)) {
43
                $others = $this->aggregateCharges($charge);
44
                $bills = $this->aggregateBills($bills, $others);
45
            } elseif ($charge instanceof ChargeInterface) {
46
                $bill = $this->generalizer->createBill($charge);
47
                $bills = $this->aggregateBills($bills, [$bill]);
48
            } else {
49
                throw new \Exception('not a Charge given to Aggregator');
50
            }
51
        }
52
53
        return $bills;
0 ignored issues
show
introduced by
The expression return $bills returns an array which contains values of type hiqdev\php\billing\bill\BillInterface which are incompatible with the return type hiqdev\php\billing\charge\Bill mandated by hiqdev\php\billing\charg...ace::aggregateCharges().
Loading history...
54
    }
55
56
    /**
57
     * Aggregate arrays of bills.
58
     * @param BillInterface[] $bills
59
     * @param BillInterface[] $others
60
     * @return BillInterface[]
61
     */
62
    public function aggregateBills(array $bills, array $others)
63
    {
64
        foreach ($others as $bill) {
65
            $uid = $bill->getUniqueString();
0 ignored issues
show
Bug introduced by
The method getUniqueString() does not exist on hiqdev\php\billing\bill\BillInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\bill\BillInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            /** @scrutinizer ignore-call */ 
66
            $uid = $bill->getUniqueString();
Loading history...
66
            if (empty($bills[$uid])) {
67
                $bills[$uid] = $bill;
68
            } else {
69
                $bills[$uid] = $this->aggregateBill($bills[$uid], $bill);
70
            }
71
        }
72
73
        return $bills;
74
    }
75
76
    public function aggregateBill(BillInterface $first, BillInterface $other)
77
    {
78
        return new Bill(
79
            null,
80
            $first->getType(),
81
            $first->getTime(),
0 ignored issues
show
Bug introduced by
The method getTime() does not exist on hiqdev\php\billing\bill\BillInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\bill\BillInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            $first->/** @scrutinizer ignore-call */ 
82
                    getTime(),
Loading history...
82
            $this->aggregateSum($first->getSum(), $other->getSum()),
83
            $this->aggregateQuantity($first->getQuantity(), $other->getQuantity()),
84
            $first->getCustomer(),
85
            $first->getTarget(),
86
            $first->getPlan(),
87
            array_merge($first->getCharges(), $other->getCharges())
88
        );
89
    }
90
91
    public function aggregateSum(Money $first, Money $other)
92
    {
93
        return $first->add($other);
94
    }
95
96
    public function aggregateQuantity(QuantityInterface $first, QuantityInterface $other)
97
    {
98
        return $first->add($other);
99
    }
100
}
101