DbMergingAggregator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 77
ccs 0
cts 16
cp 0
rs 10
c 6
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
B excludeLocalOnlyZeroBills() 0 24 7
A __construct() 0 8 1
A mergeBill() 0 3 1
A mergeBills() 0 6 1
A aggregateCharges() 0 6 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-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tools;
12
13
use hiqdev\php\billing\bill\BillInterface;
14
use hiqdev\php\billing\bill\BillRepositoryInterface;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
17
/**
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class DbMergingAggregator implements AggregatorInterface, MergerInterface
21
{
22
    protected BillRepositoryInterface $billRepository;
23
    protected MergerInterface $merger;
24
    private AggregatorInterface $localAggregator;
25
26
    public function __construct(
27
        AggregatorInterface $localAggregator,
28
        BillRepositoryInterface $billRepository,
29
        MergerInterface $merger
30
    ) {
31
        $this->billRepository = $billRepository;
32
        $this->merger = $merger;
33
        $this->localAggregator = $localAggregator;
34
    }
35
36
    public function mergeBills(array $bills): array
37
    {
38
        $dbBills = $this->billRepository->findByUniqueness($bills);
39
        $filteredLocalBills = $this->excludeLocalOnlyZeroBills($bills, $dbBills);
40
41
        return $this->merger->mergeBills(array_merge($filteredLocalBills, $dbBills));
42
    }
43
44
    public function mergeBill(BillInterface $first, BillInterface $other): BillInterface
45
    {
46
        return $this->merger->mergeBill($first, $other);
47
    }
48
49
    /**
50
     * Aggregates given Charges to Bills.
51
     * Then merges them with Bills from DB.
52
     *
53
     * @param ChargeInterface[]|ChargeInterface[][] $charges
54
     * @return BillInterface[]
55
     * @throws \Exception
56
     */
57
    public function aggregateCharges(array $charges): array
58
    {
59
        $localBills = $this->localAggregator->aggregateCharges($charges);
60
        $bills = $this->mergeBills($localBills);
61
62
        return $bills;
63
    }
64
65
    /**
66
     * When a new Zero bill is being produced, it should not be persisted
67
     * unless there is already a bill of this uniqueness in the DBMS.
68
     *
69
     * @param BillInterface[] $localBills
70
     * @param BillInterface[] $dbBills
71
     * @return BillInterface[]
72
     */
73
    private function excludeLocalOnlyZeroBills(array $localBills, array $dbBills): array
74
    {
75
        foreach ($localBills as $i => $localBill) {
76
            foreach ($localBill->getCharges() as $charge) {
77
                if ($charge->hasEvents()) {
0 ignored issues
show
Bug introduced by
The method hasEvents() does not exist on hiqdev\php\billing\charge\ChargeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\charge\ChargeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
                if ($charge->/** @scrutinizer ignore-call */ hasEvents()) {
Loading history...
78
                    continue 2;
79
                }
80
            }
81
            $isZeroSum = $localBill->getSum()->getAmount() === "0";
82
            if (!$isZeroSum) {
83
                continue;
84
            }
85
86
            $localUniqueString = $localBill->getUniqueString();
87
            foreach ($dbBills as $dbBill) {
88
                if ($dbBill->getUniqueString() === $localUniqueString) {
89
                    continue 2;
90
                }
91
            }
92
93
            unset($localBills[$i]);
94
        }
95
96
        return $localBills;
97
    }
98
99
}
100