Completed
Push — master ( 9cb945...b81036 )
by Andrii
06:01
created

PlanRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 77
ccs 0
cts 43
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A findAll() 0 14 1
B extendWithPrices() 0 27 2
1
<?php
2
3
namespace hiqdev\billing\hiapi\repositories;
4
5
use hiapi\components\ConnectionInterface;
6
use hiapi\query\QueryMutator;
7
use hiapi\query\Specification;
8
use yii\db\Query;
9
10
class PlanRepository extends \hiapi\repositories\BaseRepository
11
{
12
    /**
13
     * @var ConnectionInterface
14
     */
15
    private $db;
16
    /**
17
     * @var PlanFactory
18
     */
19
    private $planFactory;
20
    /**
21
     * @var PlanHydrator
22
     */
23
    private $planHydrator;
24
    /**
25
     * @var PriceHydrator
26
     */
27
    private $priceHydrator;
28
29
    public function __construct(
30
        ConnectionInterface $db,
31
        PlanFactory $planFactory,
32
        PlanHydrator $planHydrator,
33
        PriceHydrator $priceHydrator,
34
        array $config = []
35
    ) {
36
        parent::__construct($config);
37
38
        $this->db = $db;
39
        $this->planFactory = $planFactory;
40
        $this->planHydrator = $planHydrator;
41
        $this->priceHydrator = $priceHydrator;
42
    }
43
44
    public function findAll(Specification $specification)
45
    {
46
        $mutator = (new QueryMutator((new Query())
47
            ->select(['p.obj_id as id', 'p.name'])
48
            ->from('tariff p')
49
        ))->apply($specification);
50
51
        $rows = $mutator->getQuery()->createCommand($this->db)->queryAll();
52
        $this->extendWithPrices($rows);
53
54
        $plans = $this->planHydrator->hydrateMultiple($this->planFactory->createPrototype(), $rows);
55
56
        return $plans;
57
    }
58
59
    private function extendWithPrices(&$rows)
60
    {
61
        $tariff_ids = array_column($rows, 'id');
62
63
        $pricesRows = (new Query())
64
            ->select([
65
                'tr.id', 'tr.tariff_id',
66
                'tr.object_id as target_id', 'ob.label as target_name', 'tt.obj_id as target_type_id', 'tt.name as target_type_name',
67
                'rt.obj_id as type_id', 'rt.name as type',
68
                'tr.quantity', 'tu.name as unit',
69
                'cu.name as currency', 'round(tr.price)' // todo: do not round
70
            ])
71
            ->from('tariff_resource tr')
72
            ->leftJoin('zref rt', 'rt.obj_id = tr.type_id')
73
            ->leftJoin('obj ob', 'ob.obj_id = tr.object_id')
74
            ->leftJoin('zref tt', 'tt.obj_id = ob.class_id')
75
            ->leftJoin('zref tu', 'tu.obj_id = tr.unit_id')
76
            ->leftJoin('zref cu', 'cu.obj_id = tr.currency_id')
77
            ->where(['tr.tariff_id' => $tariff_ids])
78
            ->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...
79
80
        foreach ($rows as &$plan) {
81
            $plan['prices'] = $this->priceHydrator->hydrateMultiple(array_filter($pricesRows, function ($row) use ($plan) {
82
                return $row['tariff_id'] === $plan['id'];
83
            }));
84
        }
85
    }
86
}
87