Completed
Push — master ( 201ba3...2ef26d )
by Andrii
03:13
created

BillRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B save() 0 24 5
A saveReal() 0 3 1
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 DateTime;
14
use hiqdev\php\billing\bill\BillInterface;
15
use hiqdev\php\billing\customer\Customer;
16
use hiqdev\php\billing\target\Target;
17
use hiqdev\php\billing\type\Type;
18
use hiqdev\php\units\Quantity;
19
use hiqdev\yii\DataMapper\expressions\CallExpression;
20
use hiqdev\yii\DataMapper\expressions\HstoreExpression;
21
use Money\Currency;
22
use Money\Money;
23
use yii\db\Query;
24
25
class BillRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository
26
{
27
    /**
28
     * XXX TO BE REMOVED.
29
     */
30
    public function saveReal(BillInterface $bill)
31
    {
32
        return $this->save($bill, true);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->save($bill, true) targeting hiqdev\billing\hiapi\bill\BillRepository::save() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33
    }
34
35
    /**
36
     * @param BillInterface $bill
37
     * @param bool $isReal XXX TO BE REMOVED
38
     */
39
    public function save(BillInterface $bill, $isReal = false)
40
    {
41
        $hstore = new HstoreExpression([
42
            'id'            => $bill->getId(),
43
            'object_id'     => $bill->getTarget()->getId(),
44
            'tariff_id'     => $bill->getPlan() ? $bill->getPlan()->getId() : null,
45
            'type_id'       => $bill->getType()->getId(),
46
            'type'          => $bill->getType()->getName(),
47
            'buyer_id'      => $bill->getCustomer()->getId(),
48
            'buyer'         => $bill->getCustomer()->getLogin(),
49
            'currency'      => $bill->getSum()->getCurrency()->getCode(),
50
            'sum'           => $bill->getSum()->getAmount() * -1,
51
            'quantity'      => $bill->getQuantity()->getQuantity(),
52
            'time'          => $bill->getTime()->format('c'),
53
            'label'         => $bill->getComment() ?: null,
54
            'is_finished'   => $bill->isFinished(),
55
            'increment'     => true,
56
        ]);
57
        $call = new CallExpression('set_bill' . ($isReal ? '' : '2'), [$hstore]);
58
        $command = (new Query())->select($call);
59
        $bill->setId($command->scalar($this->db));
60
        foreach ($bill->getCharges() as $charge) {
61
            $charge->setBill($bill);
62
            $this->em->save($charge);
63
        }
64
    }
65
}
66