Completed
Push — master ( 8b1836...4d4488 )
by Andrii
12:59
created

ActionRepository::save()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 16
nc 1
nop 1
crap 30
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
            'state'     => $action->getState() ? $action->getState()->getName() : null,
0 ignored issues
show
Bug introduced by
The method getState() does not seem to exist on object<hiqdev\php\billing\action\ActionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
            'time'      => $time ? $time->format('c') : null,
40
        ]));
41
        $call = new CallExpression('set_action', [$hstore]);
42
        $command = (new Query())->select($call);
43
        $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...
44
    }
45
}
46