Completed
Push — master ( ed63c5...c53d03 )
by Andrii
04:27
created

ActionRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
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\action;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\action\ActionQuery;
15
use hiqdev\yii\DataMapper\components\ConnectionInterface;
16
use hiqdev\yii\DataMapper\components\EntityManagerInterface;
17
use hiqdev\yii\DataMapper\expressions\CallExpression;
18
use hiqdev\yii\DataMapper\expressions\HstoreExpression;
19
use hiqdev\yii\DataMapper\repositories\BaseRepository;
20
use yii\db\Query;
21
22
class ActionRepository extends BaseRepository
23
{
24
    public $queryClass = ActionQuery::class;
25
26
    public function save(ActionInterface $action)
27
    {
28
        $sale = $action->getSale();
29
        $time = $action->getTime();
30
        $hstore = new HstoreExpression(array_filter([
31
            'id'        => $action->getId(),
32
            'parent_id' => $action->hasParent() ? $action->getParent()->getId() : null,
33
            'object_id' => $action->getTarget()->getId(),
34
            'type'      => $action->getType()->getName(),
35
            'type_id'   => $action->getType()->getId(),
36
            'amount'    => $action->getQuantity()->getQuantity(),
37
            'sale_id'   => $sale ? $this->em->findId($sale) : null,
38
            'time'      => $time ? $time->format('c') : null,
39
        ]));
40
        $call = new CallExpression('set_action', [$hstore]);
41
        $command = (new Query())->select($call);
42
        $action->setId($command->scalar($this->db));
0 ignored issues
show
Bug introduced by
It seems like $this->db can also be of type object<hiqdev\yii\DataMa...ts\ConnectionInterface>; however, yii\db\Query::scalar() does only seem to accept object<yii\db\Connection>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
    }
44
}
45