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

DomainServicePriceGridView::getPriceGrid()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 29

Duplication

Lines 29
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 29
loc 29
ccs 0
cts 22
cp 0
rs 8.8337
c 0
b 0
f 0
cc 6
nc 1
nop 1
crap 42
1
<?php
2
3
namespace hipanel\modules\finance\grid;
4
5
use hipanel\modules\finance\models\DomainServicePrice;
6
use hipanel\modules\finance\widgets\PriceDifferenceWidget;
7
use hipanel\modules\finance\widgets\ResourcePriceWidget;
8
use yii\helpers\Html;
9
10
class DomainServicePriceGridView extends PriceGridView
11
{
12
    /**
13
     * @var DomainServicePrice[]
14
     */
15
    public $parentPrices;
16
17
    public function columns()
18
    {
19
        return array_merge(parent::columns(), [
20
            'purchase' => $this->getPriceGrid('feature,premium_dns_purchase'),
21
            'renewal' => $this->getPriceGrid('feature,premium_dns_renew'),
22
        ]);
23
    }
24
25
    /**
26
     * @param string $type
27
     * @return array
28
     */
29 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...
30
    {
31
        return [
32
            'label' =>  DomainServicePrice::getOperations()[$type],
33
            'contentOptions' => ['class' => 'text-center'],
34
            'format' => 'raw',
35
            'value' => function($prices) use ($type) {
36
                /** @var DomainServicePrice[] $prices */
37
                if (!isset($prices[$type])) {
38
                    return '';
39
                }
40
                $price = $prices[$type];
41
                $parent = $this->parentPrices[$type] ?? null;
42
                $parentValue = $parent ? PriceDifferenceWidget::widget([
43
                    'new' => $price->price,
44
                    'old' => $parent->price,
45
                ]) : '';
46
                $priceValue = floatval($price->price) ||
47
                (!floatval($price->price) && $parentValue) ?
48
                    ResourcePriceWidget::widget([
49
                        'price' => $price->price,
50
                        'currency' => $price->currency
51
                    ]) : '';
52
                $options = ['class' => 'col-md-6'];
53
                return Html::tag('div', $priceValue, $options) .
54
                    Html::tag('div', $parentValue, $options);
55
            }
56
        ];
57
    }
58
}
59