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

DomainZonePriceGridView::columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 9
cp 0
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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