Completed
Push — master ( f18361...e6ca4e )
by Andrii
09:00 queued 02:15
created

ConfigGridView::getTariffsWithOldPriceColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
ccs 0
cts 22
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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\modules\server\menus\ConfigActionsMenu;
16
use hipanel\modules\server\models\Config;
17
use hiqdev\yii2\menus\grid\MenuColumn;
18
use Yii;
19
use yii\helpers\Html;
20
21
class ConfigGridView extends BoxedGridView
22
{
23
    /**
24
     * @return array
25
     */
26
    private function getTariffsWithOldPriceColumns(): array
27
    {
28
        $columns = [];
29
        foreach (['nl' => 'EUR', 'us' => 'USD'] as $region => $currency) {
30
            $columns["{$region}_tariff"] = [
31
                'format' => 'raw',
32
                'value' => function (Config $config) use ($region, $currency): string {
33
                    $tariff = Html::tag('span', $config->{$region . '_tariff'});
34
                    $oldPrice = Html::tag(
35
                        'span',
36
                        Yii::t('hipanel:server:config', 'Old price: {price}', [
37
                            'price' => Yii::$app->formatter->asCurrency($config->{$region . '_old_price'}, $currency)
38
                        ]),
39
                        ['class' => 'badge']
40
                    );
41
42
                    return Html::tag('span', $tariff . $oldPrice, [
43
                        'style' => 'display: flex; flex-direction: row; justify-content: space-between; flex-wrap: wrap;'
44
                    ]);
45
                },
46
            ];
47
        }
48
49
        return $columns;
50
    }
51
52
    public function columns()
53
    {
54
        return array_merge(parent::columns(), $this->getTariffsWithOldPriceColumns(), [
55
            'actions' => [
56
                'class' => MenuColumn::class,
57
                'menuClass' => ConfigActionsMenu::class,
58
            ],
59
            'name' => [
60
                'class' => MainColumn::class,
61
                'label' => Yii::t('hipanel', 'Name'),
62
                'format' => 'html',
63
                'value' => function ($model) {
64
                    return Html::a($model->name, ['@config/view', 'id' => $model->id]) .
65
                        '</br>' . Html::tag('span', $model->label);
66
                },
67
            ],
68
            'config' => [
69
                'class' => MainColumn::class,
70
                'format' => 'html',
71
                'value' => function ($model) {
72
                    $value = '';
73
                    foreach (['cpu', 'ram', 'hdd'] as $item) {
74
                        $value .= '<nobr>' . Html::tag('span', strtoupper($item) . ': ') .
75
                            Html::tag('span', $model->$item) . '</nobr></br>';
76
                    }
77
78
                    return $value;
79
                },
80
            ],
81
            'tariffs' => [
82
                'class' => MainColumn::class,
83
                'format' => 'html',
84
                'value' => function ($model) {
85
                    return Html::tag('span', 'NL:') .
86
                        Html::a($model->nl_tariff, ['@tariff/view', 'id' => $model->nl_tariff_id]) . '</br>' .
87
                        Html::tag('span', 'US:') .
88
                        Html::a($model->us_tariff, ['@tariff/view', 'id' => $model->us_tariff_id]);
89
                },
90
            ],
91
            'servers' => [
92
                'value' => function (Config $model) {
93
                    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...
94
                },
95
            ],
96
            'cc_servers' => [
97
                'label' => Yii::t('hipanel', 'Servers'),
98
                'format' => 'html',
99
                'value' => function ($model) {
100
                    return Html::tag('span', 'NL:') . $model->nl_servers . '<br/>' .
101
                        Html::tag('span', 'US:') . $model->us_servers;
102
                },
103
            ],
104
        ]);
105
    }
106
}
107