Passed
Push — master ( e44e2f...549938 )
by Andrii
05:32
created

Merger   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 21
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fixBills() 0 9 3
A mergeBills() 0 5 1
A calculateMonthlyQuantity() 0 17 6
A __construct() 0 6 1
1
<?php
2
3
namespace hiqdev\billing\hiapi\tools;
4
5
use hiqdev\billing\hiapi\type\TypeSemantics;
6
use hiqdev\php\billing\bill\BillInterface;
7
use hiqdev\php\billing\charge\GeneralizerInterface;
8
use hiqdev\php\units\Unit;
9
use hiqdev\php\units\Quantity;
10
use hiqdev\php\units\QuantityInterface;
11
12
/**
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
class Merger extends \hiqdev\php\billing\tools\Merger
16
{
17
    /**
18
     * @var Generalizer
0 ignored issues
show
Bug introduced by
The type hiqdev\billing\hiapi\tools\Generalizer 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...
19
     */
20
    private $generalizer;
21
22
    /**
23
     * @var TypeSemantics
24
     */
25
    private $typeSemantics;
26
27
    public function __construct(
28
        GeneralizerInterface $generalizer,
29
        TypeSemantics $typeSemantics
30
    ) {
31
        $this->generalizer = $generalizer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $generalizer of type hiqdev\php\billing\charge\GeneralizerInterface is incompatible with the declared type hiqdev\billing\hiapi\tools\Generalizer of property $generalizer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        $this->typeSemantics = $typeSemantics;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function mergeBills(array $bills): array
39
    {
40
        $bills = parent::mergeBills($bills);
41
42
        return $this->fixBills($bills);
43
    }
44
45
    /**
46
     * @param BillInterface[] $bills
47
     * @return BillInterface[]
48
     */
49
    protected function fixBills(array $bills): array
50
    {
51
        foreach ($bills as $bill) {
52
            if ($this->typeSemantics->isMonthly($bill->getType())) {
53
                $bill->setQuantity($this->calculateMonthlyQuantity($bill));
54
            }
55
        }
56
57
        return $bills;
58
    }
59
60
    protected function calculateMonthlyQuantity(BillInterface $bill): QuantityInterface
61
    {
62
        $res = null;
63
        foreach ($bill->getCharges() as $charge) {
64
            $amount = $this->generalizer->generalizeQuantity($charge);
65
            if (!$amount->getUnit()->isConvertible(Unit::days())) {
66
                continue;
67
            }
68
            if ($res === null || $amount->compare($res)>0) {
69
                $res = $amount;
70
            }
71
        }
72
        if ($res === null) {
73
            return Quantity::create('days', 1);
74
        }
75
76
        return $res;
77
    }
78
}
79