Completed
Push — master ( 291758...cb1c4d )
by Dmitry
02:39
created

Aggregator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 8
dl 0
loc 127
ccs 0
cts 94
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A aggregateCharges() 0 17 4
A aggregateBills() 0 13 3
A aggregateBill() 0 14 1
A aggregateSum() 0 4 1
A aggregateQuantity() 0 4 1
A createBill() 0 14 1
A generalizeType() 0 4 1
A generalizeTime() 0 6 1
A generalizeSum() 0 4 1
A generalizeQuantity() 0 4 1
A generalizeCustomer() 0 4 1
A generalizeTarget() 0 9 2
A generalizePlan() 0 4 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 DateTimeImmutable;
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
    public function aggregateCharges(array $charges)
30
    {
31
        $bills = [];
32
        foreach ($charges as $charge) {
33
            if (is_array($charge)) {
34
                $others = $this->aggregateCharges($charge);
35
                $bills = $this->aggregateBills($bills, $others);
36
            } elseif ($charge instanceof ChargeInterface) {
37
                $bill = $this->createBill($charge);
38
                $bills = $this->aggregateBills($bills, [$bill]);
39
            } else {
40
                throw new \Exception('not a Charge given to Aggregator');
41
            }
42
        }
43
44
        return $bills;
45
    }
46
47
    /**
48
     * Aggregate arrays of bills.
49
     * @param BillInterface[] $bills
50
     * @param BillInterface[] $others
51
     * @return BillInterface[]
52
     */
53
    public function aggregateBills(array $bills, array $others)
54
    {
55
        foreach ($others as $bill) {
56
            $uid = $bill->getUniqueString();
57
            if (empty($bills[$uid])) {
58
                $bills[$uid] = $bill;
59
            } else {
60
                $bills[$uid] = $this->aggregateBill($bills[$uid], $bill);
61
            }
62
        }
63
64
        return $bills;
65
    }
66
67
    public function aggregateBill(BillInterface $first, BillInterface $other)
68
    {
69
        return new Bill(
70
            null,
71
            $first->getType(),
72
            $first->getTime(),
73
            $this->aggregateSum($first->getSum(), $other->getSum()),
74
            $this->aggregateQuantity($first->getQuantity(), $other->getQuantity()),
75
            $first->getCustomer(),
76
            $first->getTarget(),
77
            $first->getPlan(),
78
            array_merge($first->getCharges(), $other->getCharges())
79
        );
80
    }
81
82
    public function aggregateSum(Money $first, Money $other)
83
    {
84
        return $first->add($other);
85
    }
86
87
    public function aggregateQuantity(QuantityInterface $first, QuantityInterface $other)
88
    {
89
        return $first->add($other);
0 ignored issues
show
Documentation introduced by
$other is of type object<hiqdev\php\units\QuantityInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
92
    public function createBill(ChargeInterface $charge)
93
    {
94
        return new Bill(
95
            null,
96
            $this->generalizeType($charge),
97
            $this->generalizeTime($charge),
98
            $this->generalizeSum($charge),
99
            $this->generalizeQuantity($charge),
100
            $this->generalizeCustomer($charge),
101
            $this->generalizeTarget($charge),
102
            $this->generalizePlan($charge),
103
            [$charge]
104
        );
105
    }
106
107
    public function generalizeType(ChargeInterface $charge)
108
    {
109
        return $charge->getPrice()->getType();
110
    }
111
112
    public function generalizeTime(ChargeInterface $charge)
113
    {
114
        $date = new DateTimeImmutable($charge->getTime());
115
116
        return $date->modify('first day of this month midnight');
117
    }
118
119
    public function generalizeSum(ChargeInterface $charge)
120
    {
121
        return $charge->getSum();
122
    }
123
124
    public function generalizeQuantity(ChargeInterface $charge)
125
    {
126
        return $charge->getUsage();
127
    }
128
129
    public function generalizeCustomer(ChargeInterface $charge)
130
    {
131
        return $charge->getAction()->getCustomer();
132
    }
133
134
    public function generalizeTarget(ChargeInterface $charge)
135
    {
136
        $priceTarget = $charge->getPrice()->getTarget();
137
138
        return $priceTarget->getId() ? $priceTarget : $charge->getTarget();
139
140
        /// think of this way
141
        /// return $priceTarget->getId() ? $priceTarget : new Target($charge->getSale()->getPlan()->getId(), 'plan');
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
142
    }
143
144
    public function generalizePlan(ChargeInterface $charge)
145
    {
146
        return $charge->getPrice()->getPlan();
147
    }
148
}
149