Completed
Push — master ( 104946...6683dc )
by Dmitry
02:26
created

ChargeRepository::joinParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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