Completed
Push — master ( c6b5de...db50e7 )
by Dmitry
04:45
created

src/grid/DomainServicePriceGridView.php (1 issue)

Upgrade to new PHP Analysis Engine

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\DomainServicePrice;
14
use hipanel\modules\finance\widgets\PriceDifferenceWidget;
15
use hipanel\modules\finance\widgets\ResourcePriceWidget;
16
use yii\helpers\Html;
17
18
class DomainServicePriceGridView extends PriceGridView
19
{
20
    /**
21
     * @var DomainServicePrice[]
22
     */
23
    public $parentPrices;
24
25
    public function columns()
26
    {
27
        return array_merge(parent::columns(), [
28
            'purchase' => $this->getPriceGrid('feature,premium_dns_purchase'),
29
            'renewal' => $this->getPriceGrid('feature,premium_dns_renew'),
30
        ]);
31
    }
32
33
    /**
34
     * @param string $type
35
     * @return array
36
     */
37 View Code Duplication
    private function getPriceGrid(string $type): array
0 ignored issues
show
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...
38
    {
39
        return [
40
            'label' =>  DomainServicePrice::getOperations()[$type],
41
            'contentOptions' => ['class' => 'text-center'],
42
            'format' => 'raw',
43
            'value' => function ($prices) use ($type) {
44
                /** @var DomainServicePrice[] $prices */
45
                if (!isset($prices[$type])) {
46
                    return '';
47
                }
48
                $price = $prices[$type];
49
                $parent = $this->parentPrices[$type] ?? null;
50
                $parentValue = $parent ? PriceDifferenceWidget::widget([
51
                    'new' => $price->getMoney(),
52
                    'old' => $parent->getMoney(),
53
                ]) : '';
54
                $priceValue = floatval($price->price) ||
55
                (!floatval($price->price) && $parentValue) ?
56
                    ResourcePriceWidget::widget([
57
                        'price' => $price->getMoney(),
58
                    ]) : '';
59
                $options = ['class' => 'col-md-6'];
60
61
                return Html::tag('div', $priceValue, $options) .
62
                    Html::tag('div', $parentValue, $options);
63
            },
64
        ];
65
    }
66
}
67