Completed
Push — master ( ffb209...577704 )
by Andrii
05:01
created

PlanRepository::old___findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\billing\hiapi\plan;
4
5
use hiapi\components\ConnectionInterface;
6
use hiapi\query\Specification;
7
use hiapi\repositories\BaseRepository;
8
use hiqdev\php\billing\customer\Customer;
9
use hiqdev\php\billing\plan\PlanFactoryInterface;
10
use hiqdev\php\billing\plan\PlanRepositoryInterface;
11
use hiqdev\php\billing\action\ActionInterface;
12
use hiqdev\php\billing\order\OrderInterface;
13
use Yii;
14
use yii\db\Query;
15
16
class PlanRepository extends BaseRepository implements PlanRepositoryInterface
17
{
18
    public $queryClass = PlanQuery::class;
19
20
    /**
21
     * @var PlanFactory
22
     */
23
    protected $factory;
24
25
    public function __construct(
26
        ConnectionInterface $db,
27
        PlanFactoryInterface $factory,
28
        array $config = []
29
    ) {
30
        parent::__construct($config);
31
32
        $this->db = $db;
33
        $this->factory = $factory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory of type object<hiqdev\php\billin...n\PlanFactoryInterface> is incompatible with the declared type object<hiqdev\billing\hiapi\plan\PlanFactory> of property $factory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
    }
35
36
    public function create(array $row)
37
    {
38
        $row['seller'] = $this->createEntity(Customer::class, $row['seller']);
39
40
        return parent::create($row);
41
    }
42
43
    public function old___findAll(Specification $specification)
44
    {
45
        $mutator = (new QueryMutator((new Query())
46
            ->select(['p.obj_id as id', 'p.name'])
47
            ->from('tariff p')
48
        ))->apply($specification);
49
50
        $rows = $mutator->getQuery()->createCommand($this->db)->queryAll();
51
        $this->extendWithPrices($rows);
52
53
        $plans = $this->planHydrator->hydrateMultiple($this->planFactory->createPrototype(), $rows);
0 ignored issues
show
Documentation introduced by
The property planHydrator does not exist on object<hiqdev\billing\hiapi\plan\PlanRepository>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
The property planFactory does not seem to exist. Did you mean factory?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
55
        return $plans;
56
    }
57
58
    private function extendWithPrices(&$rows)
59
    {
60
        $tariff_ids = array_column($rows, 'id');
61
62
        $pricesRows = (new Query())
63
            ->select([
64
                'tr.id', 'tr.tariff_id',
65
                'tr.object_id as target_id', 'ob.label as target_name', 'tt.obj_id as target_type_id', 'tt.name as target_type_name',
66
                'rt.obj_id as type_id', 'rt.name as type',
67
                'tr.quantity', 'tu.name as unit',
68
                'cu.name as currency', 'round(tr.price)' // todo: do not round
69
            ])
70
            ->from('tariff_resource tr')
71
            ->leftJoin('zref rt', 'rt.obj_id = tr.type_id')
72
            ->leftJoin('obj ob', 'ob.obj_id = tr.object_id')
73
            ->leftJoin('zref tt', 'tt.obj_id = ob.class_id')
74
            ->leftJoin('zref tu', 'tu.obj_id = tr.unit_id')
75
            ->leftJoin('zref cu', 'cu.obj_id = tr.currency_id')
76
            ->where(['tr.tariff_id' => $tariff_ids])
77
            ->createCommand($this->db)->queryAll();
0 ignored issues
show
Documentation introduced by
$this->db is of type object<hiapi\components\ConnectionInterface>, but the function expects a object<yii\db\Connection>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
79
        foreach ($rows as &$plan) {
80
            $plan['prices'] = $this->priceHydrator->hydrateMultiple(array_filter($pricesRows, function ($row) use ($plan) {
0 ignored issues
show
Documentation introduced by
The property priceHydrator does not exist on object<hiqdev\billing\hiapi\plan\PlanRepository>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
                return $row['tariff_id'] === $plan['id'];
82
            }));
83
        }
84
    }
85
86
    public function findByAction(ActionInterface $action)
87
    {
88
        $client_id = $action->getCustomer()->getId();
89
        $seller = $action->getCustomer()->getSeller()->getLogin();
90
        $type = $action->getTarget()->getType();
91
92
        $spec = Yii::createObject(Specification::class)->where([
93
            'type-name' => $type,
94
            'available_for' => [
95
                'client_id' => $client_id,
96
                'seller'    => $seller,
97
            ],
98
        ]);
99
100
        return $this->findOne($spec);
101
    }
102
103
    public function findByOrder(OrderInterface $order)
104
    {
105
        return array_map([$this, 'findByAction'], $order->getActions());
106
    }
107
}
108