Completed
Push — master ( 00bad6...c7c8a3 )
by Dmitry
06:29
created

DomainZonePriceGridView   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 47.54 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 29
loc 61
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A columns() 0 19 1
B getPriceGrid() 29 29 6

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 hipanel\modules\finance\grid;
4
5
use hipanel\modules\finance\models\DomainZonePrice;
6
use hipanel\modules\finance\widgets\PriceDifferenceWidget;
7
use hipanel\modules\finance\widgets\ResourcePriceWidget;
8
use yii\helpers\Html;
9
10
class DomainZonePriceGridView extends PriceGridView
11
{
12
    /**
13
     * @var DomainZonePrice[][]
14
     */
15
    public $parentPrices;
16
17
    public function columns()
18
    {
19
        return array_merge(parent::columns(), [
20
            'name' => [
21
                'label' => '',
22
                'contentOptions'=> ['style' => 'font-weight:bold'],
23
                'value' => function($prices) {
24
                    /** @var DomainZonePrice[] $prices  */
25
                    return current($prices)->object->name;
26
                }
27
            ],
28
            'registration' => $this->getPriceGrid('domain,dregistration'),
29
            'transfer' => $this->getPriceGrid('domain,dtransfer'),
30
            'renewal' => $this->getPriceGrid('domain,drenewal'),
31
            'deleteInAgp' => $this->getPriceGrid('domain,ddelete_agp'),
32
            'restoringExpired' => $this->getPriceGrid('domain,drestore_expired'),
33
            'restoringDeleted' => $this->getPriceGrid('domain,drestore_deleted'),
34
        ]);
35
    }
36
37
    /**
38
     * @param string $type
39
     * @return array
40
     */
41 View Code Duplication
    private function getPriceGrid(string $type): array
0 ignored issues
show
Duplication introduced by
This method 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...
42
    {
43
        return [
44
            'label' =>  DomainZonePrice::getTypes()[$type],
45
            'contentOptions' => ['class' => 'text-center'],
46
            'format' => 'raw',
47
            'value' => function($prices) use ($type) {
48
                /** @var DomainZonePrice[] $prices */
49
                if (!isset($prices[$type])) {
50
                    return '';
51
                }
52
                $price = $prices[$type];
53
                $parent = $this->parentPrices[$price->object_id][$type] ?? null;
54
                $parentValue = $parent ? PriceDifferenceWidget::widget([
55
                    'new' => $price->price,
56
                    'old' => $parent->price,
57
                ]) : '';
58
                $priceValue = floatval($price->price) ||
59
                (!floatval($price->price) && $parent) ?
60
                    ResourcePriceWidget::widget([
61
                        'price' => $price->price,
62
                        'currency' => $price->currency
63
                    ]) : '';
64
                $options = ['class' => 'col-md-6'];
65
                return Html::tag('div', $priceValue, $options) .
66
                    Html::tag('div', $parentValue, $options);
67
            }
68
        ];
69
    }
70
}
71