Passed
Pull Request — master (#6)
by
unknown
04:14
created

BillRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
eloc 30
dl 0
loc 50
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 27 4
A getBillChargesIds() 0 10 3
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\bill;
12
13
use hiqdev\php\billing\bill\BillInterface;
14
use hiqdev\yii\DataMapper\expressions\CallExpression;
15
use hiqdev\yii\DataMapper\expressions\HstoreExpression;
16
use yii\db\Query;
17
18
class BillRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository
19
{
20
    /**
21
     * @param BillInterface $bill
22
     */
23
    public function save(BillInterface $bill)
24
    {
25
        $hstore = new HstoreExpression([
26
            'id'            => $bill->getId(),
27
            'object_id'     => $bill->getTarget()->getId(),
28
            'tariff_id'     => $bill->getPlan() ? $bill->getPlan()->getId() : null,
29
            'type_id'       => $bill->getType()->getId(),
30
            'type'          => $bill->getType()->getName(),
31
            'buyer_id'      => $bill->getCustomer()->getId(),
32
            'buyer'         => $bill->getCustomer()->getLogin(),
33
            'currency'      => $bill->getSum()->getCurrency()->getCode(),
34
            'sum'           => $bill->getSum()->getAmount(),
35
            'quantity'      => $bill->getQuantity()->getQuantity(),
36
            'unit'          => $bill->getQuantity()->getUnit()->getName(),
37
            'time'          => $bill->getTime()->format('c'),
38
            'label'         => $bill->getComment() ?: null,
39
            'is_finished'   => $bill->isFinished(),
40
            'increment'     => true,
41
            'charge_ids'    => $this->getBillChargesIds($bill)
42
        ]);
43
        $this->db->transaction(function() use ($bill, $hstore) {
44
            $call = new CallExpression('set_bill', [$hstore]);
45
            $command = (new Query())->select($call);
46
            $bill->setId($command->scalar($this->db));
47
            foreach ($bill->getCharges() as $charge) {
48
                $charge->setBill($bill);
49
                $this->em->save($charge);
50
            }
51
        });
52
    }
53
54
    /**
55
     * @param BillInterface $bill
56
     * @return string - comma separated charges ids
57
     */
58
    private function getBillChargesIds(BillInterface $bill): string
59
    {
60
        $ids = [];
61
62
        foreach ($bill->getCharges() as $charge) {
63
            if ($charge->getId() !== null) {
64
                $ids[] = $charge->getId();
65
            }
66
        }
67
        return implode(',', $ids);
68
    }
69
}
70