Completed
Push — master ( 428142...9f94b9 )
by Andrii
02:14
created

Aggregator::generalizeCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge;
12
13
use DateTime;
14
use hiqdev\php\billing\bill\Bill;
15
use hiqdev\php\billing\bill\BillInterface;
16
use hiqdev\php\units\QuantityInterface;
17
use Money\Money;
18
19
/**
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class Aggregator implements AggregatorInterface
23
{
24
    /**
25
     * @var BillInterface[]
26
     */
27
    protected $bills = [];
28
29 1
    public function aggregateCharges(array $charges)
30
    {
31 1
        $bills = [];
32 1
        foreach ($charges as $charge) {
33 1
            if (is_array($charge)) {
34 1
                $others = $this->aggregateCharges($charge);
35 1
                $bills = $this->aggregateBills($bills, $others);
36 1
            } elseif ($charge instanceof ChargeInterface) {
37 1
                $bill = $this->createBill($charge);
38 1
                $bills = $this->aggregateBills($bills, [$bill]);
39
            } else {
40 1
                throw new \Exception('not a Charge given to Aggregator');
41
            }
42
        }
43
44 1
        return $bills;
45
    }
46
47
    /**
48
     * Aggregate arrays of bills.
49
     * @param BillInterface[] $bills
50
     * @param BillInterface[] $others
51
     * @return BillInterface[]
52
     */
53 1
    public function aggregateBills(array $bills, array $others)
54
    {
55 1
        foreach ($others as $bill) {
56 1
            $uid = $bill->getUniqueId();
57 1
            if (empty($bills[$uid])) {
58 1
                $bills[$uid] = $bill;
59
            } else {
60 1
                $bills[$uid] = $this->aggregateBill($bills[$uid], $bill);
61
            }
62
        }
63
64 1
        return $bills;
65
    }
66
67 1
    public function aggregateBill(BillInterface $first, BillInterface $other)
68
    {
69 1
        return new Bill(
70 1
            null,
71 1
            $first->getType(),
72 1
            $first->getTime(),
73 1
            $this->aggregateSum($first->getSum(), $other->getSum()),
74 1
            $this->aggregateQuantity($first->getQuantity(), $other->getQuantity()),
75 1
            $first->getCustomer(),
76 1
            $first->getTarget(),
77 1
            $first->getPlan(),
78 1
            array_merge($first->getCharges(), $other->getCharges())
79
        );
80
    }
81
82 1
    public function aggregateSum(Money $first, Money $other)
83
    {
84 1
        return $first->add($other);
85
    }
86
87 1
    public function aggregateQuantity(QuantityInterface $first, QuantityInterface $other)
88
    {
89 1
        return $first->add($other);
90
    }
91
92 1
    public function createBill(ChargeInterface $charge)
93
    {
94 1
        return new Bill(
95 1
            null,
96 1
            $this->generalizeType($charge),
97 1
            $this->generalizeTime($charge),
98 1
            $this->generalizeSum($charge),
99 1
            $this->generalizeQuantity($charge),
100 1
            $this->generalizeCustomer($charge),
101 1
            $this->generalizeTarget($charge),
102 1
            $this->generalizePlan($charge),
103 1
            [$charge]
104
        );
105
    }
106
107 1
    public function generalizeType(ChargeInterface $charge)
108
    {
109 1
        return $charge->getPrice()->getType();
110
    }
111
112 1
    public function generalizeTime(ChargeInterface $charge)
113
    {
114 1
        $date = new DateTime($charge->getTime());
115
116 1
        return $date->modify('first day of this month midnight');
117
    }
118
119 1
    public function generalizeSum(ChargeInterface $charge)
120
    {
121 1
        return $charge->getSum();
122
    }
123
124 1
    public function generalizeQuantity(ChargeInterface $charge)
125
    {
126 1
        return $charge->getUsage();
127
    }
128
129 1
    public function generalizeCustomer(ChargeInterface $charge)
130
    {
131 1
        return $charge->getAction()->getCustomer();
132
    }
133
134 1
    public function generalizeTarget(ChargeInterface $charge)
135
    {
136 1
        return $charge->getPrice()->getTarget();
137
    }
138
139 1
    public function generalizePlan(ChargeInterface $charge)
140
    {
141 1
        return $charge->getPrice()->getPlan();
142
    }
143
}
144