These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Finance module for HiPanel |
||
4 | * |
||
5 | * @link https://github.com/hiqdev/hipanel-module-finance |
||
6 | * @package hipanel-module-finance |
||
7 | * @license BSD-3-Clause |
||
8 | * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hipanel\modules\finance\grid; |
||
12 | |||
13 | use hipanel\modules\finance\models\DomainZonePrice; |
||
14 | use hipanel\modules\finance\widgets\PriceDifferenceWidget; |
||
15 | use hipanel\modules\finance\widgets\ResourcePriceWidget; |
||
16 | use yii\helpers\Html; |
||
17 | |||
18 | class DomainZonePriceGridView extends PriceGridView |
||
19 | { |
||
20 | /** |
||
21 | * @var DomainZonePrice[][] |
||
22 | */ |
||
23 | public $parentPrices; |
||
24 | |||
25 | public function columns() |
||
26 | { |
||
27 | return array_merge(parent::columns(), [ |
||
28 | 'name' => [ |
||
29 | 'label' => '', |
||
30 | 'contentOptions'=> ['style' => 'font-weight:bold'], |
||
31 | 'value' => function ($prices) { |
||
32 | /** @var DomainZonePrice[] $prices */ |
||
33 | return current($prices)->object->name; |
||
34 | }, |
||
35 | ], |
||
36 | 'registration' => $this->getPriceGrid('domain,dregistration'), |
||
37 | 'transfer' => $this->getPriceGrid('domain,dtransfer'), |
||
38 | 'renewal' => $this->getPriceGrid('domain,drenewal'), |
||
39 | 'deleteInAgp' => $this->getPriceGrid('domain,ddelete_agp'), |
||
40 | 'restoringExpired' => $this->getPriceGrid('domain,drestore_expired'), |
||
41 | 'restoringDeleted' => $this->getPriceGrid('domain,drestore_deleted'), |
||
42 | ]); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param string $type |
||
47 | * @return array |
||
48 | */ |
||
49 | View Code Duplication | private function getPriceGrid(string $type): array |
|
0 ignored issues
–
show
|
|||
50 | { |
||
51 | return [ |
||
52 | 'label' => DomainZonePrice::getTypes()[$type], |
||
53 | 'contentOptions' => ['class' => 'text-center'], |
||
54 | 'format' => 'raw', |
||
55 | 'value' => function ($prices) use ($type) { |
||
56 | /** @var DomainZonePrice[] $prices */ |
||
57 | if (!isset($prices[$type])) { |
||
58 | return ''; |
||
59 | } |
||
60 | $price = $prices[$type]; |
||
61 | $parent = $this->parentPrices[$price->object_id][$type] ?? null; |
||
62 | |||
63 | $parentValue = ''; |
||
64 | if ($parent) { |
||
65 | $parentValue = PriceDifferenceWidget::widget([ |
||
66 | 'new' => $price->getMoney(), |
||
67 | 'old' => $parent->getMoney(), |
||
68 | ]); |
||
69 | } |
||
70 | $priceValue = floatval($price->price) || |
||
71 | (!floatval($price->price) && $parent) ? |
||
72 | ResourcePriceWidget::widget([ |
||
73 | 'price' => $price->getMoney(), |
||
74 | ]) : ''; |
||
75 | $options = ['class' => 'col-md-6']; |
||
76 | |||
77 | return Html::tag('div', $priceValue, $options) . |
||
78 | Html::tag('div', $parentValue, $options); |
||
79 | }, |
||
80 | ]; |
||
81 | } |
||
82 | } |
||
83 |
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.