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

ChargeRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
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\charge;
12
13
use hiqdev\php\billing\charge\Charge;
14
use hiqdev\php\billing\charge\GeneralizerInterface;
15
use hiqdev\php\billing\sale\Sale;
16
use hiqdev\yii\DataMapper\components\ConnectionInterface;
17
use hiqdev\yii\DataMapper\components\EntityManagerInterface;
18
use hiqdev\yii\DataMapper\expressions\CallExpression;
19
use hiqdev\yii\DataMapper\expressions\HstoreExpression;
20
use hiqdev\yii\DataMapper\repositories\BaseRepository;
21
use yii\db\Query;
22
23
class ChargeRepository extends BaseRepository
24
{
25
    /** {@inheritdoc} */
26
    public $queryClass = ChargeQuery::class;
27
28
    public function save(Charge $charge)
29
    {
30
        $action = $charge->getAction();
31
        $tariff_id = null;
32
        if ($action->hasSale($action)) {
33
            $tariff_id = $action->getSale()->getPlan()->getId();
34
            $this->em->save($action);
35
        }
36
        $hstore = new HstoreExpression(array_filter([
37
            'id'            => $charge->getId(),
38
            'object_id'     => $charge->getTarget()->getId(),
39
            'tariff_id'     => $tariff_id,
40
            'action_id'     => $action->getId(),
41
            'buyer_id'      => $action->getCustomer()->getId(),
42
            'buyer'         => $action->getCustomer()->getLogin(),
43
            'parent_id'     => $charge->getParent() !== null ? $charge->getParent()->getId() : null,
44
            'type_id'       => $charge->getType()->getId(),
45
            'type'          => $charge->getType()->getName(),
46
            'currency'      => $charge->getSum()->getCurrency()->getCode(),
47
            'sum'           => $charge->getSum()->getAmount(),
48
            'unit'          => $charge->getUsage()->getUnit()->getName(),
49
            'quantity'      => $charge->getUsage()->getQuantity(),
50
            'bill_id'       => $charge->getBill()->getId(),
51
            'parent_id'     => $charge->getParent() !== null ? $charge->getParent()->getId() : null,
52
            'time'          => $charge->getAction()->getTime()->format('c'),
53
            'is_finished'   => $charge->isFinished(),
54
            'label'         => $charge->getComment(),
55
        ]));
56
        $call = new CallExpression('set_charge', [$hstore]);
57
        $command = (new Query())->select($call);
58
        $charge->setId($command->scalar($this->db));
59
    }
60
}
61