Completed
Push — master ( d26128...b08c4e )
by Andrii
03:02
created

Aggregator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 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
                $bills = $this->aggregateBills($bills, $others);
45 1
            } elseif ($charge instanceof ChargeInterface) {
46 1
                $bill = $this->generalizer->createBill($charge);
47 1
                $bills = $this->aggregateBills($bills, [$bill]);
48
            } else {
49 1
                throw new \Exception('not a Charge given to Aggregator');
50
            }
51
        }
52
53 1
        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 1
    public function aggregateBills(array $bills, array $others)
63
    {
64 1
        foreach ($others as $bill) {
65 1
            $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 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(),
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 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)
97
    {
98 1
        return $first->add($other);
99
    }
100
}
101