Completed
Push — master ( f8a59f...796b11 )
by Dmitry
02:31
created

PlanRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 100 %

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 77
loc 77
ccs 0
cts 43
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 1
A findAll() 14 14 1
B extendWithPrices() 27 27 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class PlanRepository extends \hiapi\repositories\BaseRepository
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
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