Passed
Push — master ( 2f6489...18bddc )
by Dmitry
04:02
created

ConfigGridView::columns()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 80
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 61
c 2
b 0
f 0
dl 0
loc 80
ccs 0
cts 71
cp 0
rs 8.8509
cc 3
nc 1
nop 0
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\grid;
12
13
use hipanel\grid\BoxedGridView;
14
use hipanel\grid\MainColumn;
15
use hipanel\grid\RefColumn;
16
use hipanel\modules\server\menus\ConfigActionsMenu;
17
use hipanel\modules\server\models\Config;
18
use hipanel\modules\server\models\ConfigSearch;
19
use hipanel\modules\server\widgets\combo\ConfigProfileCombo;
20
use hiqdev\yii2\menus\grid\MenuColumn;
21
use Yii;
22
use yii\helpers\Html;
23
24
class ConfigGridView extends BoxedGridView
25
{
26
    /**
27
     * @return array
28
     */
29
    private function getTariffsWithOldPriceColumns(): array
30
    {
31
        $columns = [];
32
        foreach (['nl' => 'EUR', 'us' => 'USD'] as $region => $currency) {
33
            $columns["{$region}_tariff"] = [
34
                'format' => 'raw',
35
                'value' => function (Config $config) use ($region, $currency): string {
36
                    $tariff = Html::tag('span', $config->{$region . '_tariff'});
37
                    $oldPrice = Html::tag(
38
                        'span',
39
                        Yii::t('hipanel:server:config', 'Old price: {price}', [
40
                            'price' => Yii::$app->formatter->asCurrency($config->{$region . '_old_price'}, $currency)
41
                        ]),
42
                        ['class' => 'badge']
43
                    );
44
45
                    return Html::tag('span', $tariff . $oldPrice, [
46
                        'style' => 'display: flex; flex-direction: row; justify-content: space-between; flex-wrap: wrap;'
47
                    ]);
48
                },
49
            ];
50
        }
51
52
        return $columns;
53
    }
54
55
    public function columns()
56
    {
57
        return array_merge(parent::columns(), $this->getTariffsWithOldPriceColumns(), [
58
            'actions' => [
59
                'class' => MenuColumn::class,
60
                'menuClass' => ConfigActionsMenu::class,
61
            ],
62
            'name' => [
63
                'class' => MainColumn::class,
64
                'label' => Yii::t('hipanel', 'Name'),
65
                'filterOptions' => ['class' => 'narrow-filter'],
66
                'format' => 'html',
67
                'value' => function ($model) {
68
                    return Html::a($model->name, ['@config/view', 'id' => $model->id]) .
69
                        '</br>' . Html::tag('span', $model->label);
70
                },
71
            ],
72
            'config' => [
73
                'class' => MainColumn::class,
74
                'format' => 'html',
75
                'value' => function ($model) {
76
                    $value = '';
77
                    foreach (['cpu', 'ram', 'hdd', 'ssd'] as $item) {
78
                        if (empty($model->$item)) {
79
                            continue;
80
                        }
81
                        $value .= '<nobr>' . Html::tag('span', strtoupper($item) . ': ') .
82
                            Html::tag('span', $model->$item) . '</nobr></br>';
83
                    }
84
85
                    return $value;
86
                },
87
            ],
88
            'profiles' => [
89
                'format' => 'raw',
90
                'filterOptions' => ['class' => 'narrow-filter'],
91
                'attribute' => 'profiles',
92
                'filter' => ConfigProfileCombo::widget([
93
                    'attribute' => 'profiles_like',
94
                    'model' => $this->filterModel ?? new ConfigSearch(),
95
                    'formElementSelector' => 'td',
96
                ]),
97
                'enableSorting' => false,
98
                'value' => function (Config $config): string {
99
                    $colors = ['bg-teal', 'bg-green', 'bg-yellow', 'bg-purple', 'bg-aqua', 'bg-red'];
100
                    return Html::tag('ul', implode('<br>', array_map(function ($profile) use (&$colors) {
101
                        return Html::tag('li', $profile, ['class' => 'badge ' . array_pop($colors)]);
102
                    }, array_map('trim', explode(',', $config->profiles)))), ['class' => 'list-unstyled']);
0 ignored issues
show
Bug Best Practice introduced by
The property profiles does not exist on hipanel\modules\server\models\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
103
                },
104
            ],
105
            'tariffs' => [
106
                'class' => MainColumn::class,
107
                'format' => 'html',
108
                'value' => function ($model) {
109
                    return Html::tag('span', 'NL:') .
110
                        Html::a($model->nl_tariff, ['@tariff/view', 'id' => $model->nl_tariff_id]) . '</br>' .
111
                        Html::tag('span', 'US:') .
112
                        Html::a($model->us_tariff, ['@tariff/view', 'id' => $model->us_tariff_id]);
113
                },
114
            ],
115
            'servers' => [
116
                'value' => function (Config $model) {
117
                    return implode(', ', array_unique(explode(', ', $model->servers)));
0 ignored issues
show
Bug Best Practice introduced by
The property servers does not exist on hipanel\modules\server\models\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
118
                },
119
            ],
120
            'cc_servers' => [
121
                'label' => Yii::t('hipanel', 'Servers'),
122
                'format' => 'html',
123
                'value' => function ($model) {
124
                    return Html::tag('span', 'NL:') . $model->nl_servers . '<br/>' .
125
                        Html::tag('span', 'US:') . $model->us_servers;
126
                },
127
            ],
128
            'state' => [
129
                'label' => Yii::t('hipanel', 'State'),
130
                'format' => 'raw',
131
                'class' => RefColumn::class,
132
                'filterOptions' => ['class' => 'narrow-filter'],
133
                'i18nDictionary' => 'hipanel',
134
                'gtype' => 'state,config',
135
            ],
136
        ]);
137
    }
138
}
139