Completed
Push — master ( 6ed56d...9a6247 )
by Andrii
03:57
created

DbMergingAggregator::aggregateCharges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
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\bill\BillRepositoryInterface;
16
use hiqdev\php\billing\charge\ChargeInterface;
17
use hiqdev\php\billing\charge\GeneralizerInterface;
18
use hiqdev\php\units\QuantityInterface;
19
use Money\Money;
20
21
/**
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class DbMergingAggregator extends Aggregator
25
{
26
    /**
27
     * @var BillRepositoryInterface
28
     */
29
    protected $billRepository;
30
31
    /**
32
     * @var MergerInterface
33
     */
34
    protected $merger;
35
36
    public function __construct(
37
        GeneralizerInterface $generalizer,
38
        BillRepositoryInterface $billRepository,
39
        MergerInterface $merger
40
    ) {
41
        parent::__construct($generalizer);
42
        $this->billRepository = $billRepository;
43
        $this->merger = $merger;
44
    }
45
46
    /**
47
     * Aggregates given Charges to Bills.
48
     * Then merges them with Bills from DB.
49
     * @param ChargeInterface[]|ChargeInterface[][] $charges
50
     * @return BillInterface[]
51
     */
52
    public function aggregateCharges(array $charges): array
53
    {
54
        $bills  = parent::aggregateCharges($charges);
55
        $ids    = $this->billRepository->findIds($bills);
56
        $fromdb = $this->billRepository->findByIds($ids);
57
58
        return $this->merger->mergeBills(array_merge($bills, $fromdb));
59
    }
60
}
61