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 |
|
|
|
|
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
|
|
|
|
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.