Completed
Push — master ( f79ff7...908aca )
by Dmitry
15:25 queued 10:35
created

Aggregator::aggregateBills()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
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\tools;
12
13
use hiqdev\php\billing\bill\Bill;
14
use hiqdev\php\billing\bill\BillInterface;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\charge\GeneralizerInterface;
17
use hiqdev\php\units\QuantityInterface;
18
use Money\Money;
19
20
/**
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Aggregator implements AggregatorInterface
24
{
25
    /**
26
     * @var GeneralizerInterface
27
     */
28
    protected $generalizer;
29
30 1
    public function __construct(GeneralizerInterface $generalizer)
31
    {
32 1
        $this->generalizer = $generalizer;
33 1
    }
34
35
    /**
36
     * Aggregates given Charges to Bills. Then aggregates them again with DB.
37
     * @param ChargeInterface[]|ChargeInterface[][] $charges
38
     * @return BillInterface[]
39
     */
40 1
    public function aggregateCharges(array $charges): array
41
    {
42 1
        $bills = [];
43 1
        foreach ($charges as $charge) {
44 1
            if (is_array($charge)) {
45 1
                $others = $this->aggregateCharges($charge);
46 1
            } elseif ($charge instanceof ChargeInterface) {
47 1
                $others = [$this->generalizer->createBill($charge)];
48
            } else {
49
                throw new \Exception('not a Charge given to Aggregator');
50
            }
51
52 1
            $bills = $this->aggregateBills($bills, $others);
53
        }
54
55 1
        return $bills;
56
    }
57
58
    /**
59
     * Aggregate arrays of bills.
60
     * @param BillInterface[] $bills
61
     * @param BillInterface[] $others
62
     * @return BillInterface[]
63
     */
64 1
    protected function aggregateBills(array $bills, array $others): array
65
    {
66 1
        foreach ($others as $bill) {
67 1
            $uid = $bill->getUniqueString();
68 1
            if (empty($bills[$uid])) {
69 1
                $bills[$uid] = $bill;
70
            } else {
71 1
                $bills[$uid] = $this->aggregateBill($bills[$uid], $bill);
72
            }
73
        }
74
75 1
        return $bills;
76
    }
77
78
    /**
79
     * @param BillInterface $first
80
     * @param BillInterface $other
81
     * @return BillInterface
82
     */
83 1
    protected function aggregateBill(BillInterface $first, BillInterface $other): BillInterface
84
    {
85 1
        return new Bill(
86 1
            $this->aggregateId($first, $other),
87 1
            $first->getType(),
88 1
            $first->getTime(),
89 1
            $this->aggregateSum($first, $other),
90 1
            $this->aggregateQuantity($first, $other),
91 1
            $first->getCustomer(),
92 1
            $first->getTarget(),
93 1
            $first->getPlan(),
94 1
            array_merge($first->getCharges(), $other->getCharges())
95
        );
96
    }
97
98
    /**
99
     * @param BillInterface $first
100
     * @param BillInterface $other
101
     * @return string|int|null
102
     */
103 1
    protected function aggregateId(BillInterface $first, BillInterface $other)
104
    {
105 1
        if ($first->getId() === null) {
0 ignored issues
show
introduced by
The condition $first->getId() === null is always false.
Loading history...
106 1
            return $other->getId();
107
        }
108
        if ($other->getId() === null) {
0 ignored issues
show
introduced by
The condition $other->getId() === null is always false.
Loading history...
109
            return $first->getId();
110
        }
111
        if ($first->getId() === $other->getId()) {
112
            return $other->getId();
113
        }
114
115
        throw new AggregationException('cannot aggregate bills with different IDs');
0 ignored issues
show
Bug introduced by
The type hiqdev\php\billing\tools\AggregationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
    }
117
118
    /**
119
     * @param BillInterface $first
120
     * @param BillInterface $other
121
     * @param ChargeInterface[] $charges
122
     * @return Money
123
     */
124 1
    protected function aggregateSum(BillInterface $first, BillInterface $other): Money
125
    {
126 1
        return $first->getSum()->add($other->getSum());
127
    }
128
129
    /**
130
     * @param BillInterface $first
131
     * @param BillInterface $other
132
     * @param ChargeInterface[] $charges
133
     * @return QuantityInterface
134
     */
135 1
    protected function aggregateQuantity(BillInterface $first, BillInterface $other): QuantityInterface
136
    {
137 1
        return $first->getQuantity()->add($other->getQuantity());
138
    }
139
}
140