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

src/grid/TariffProfileGridView.php (4 issues)

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\menus\ProfileActionsMenu;
14
use hipanel\modules\finance\models\Tariff;
15
use hipanel\modules\finance\models\TariffProfile;
16
use hiqdev\yii2\menus\grid\MenuColumn;
17
use Yii;
18
use yii\helpers\Html;
19
20
class TariffProfileGridView extends \hipanel\grid\BoxedGridView
21
{
22
    public function columns()
23
    {
24
        return array_merge(parent::columns(), [
25
            'name' => [
26
                'class' => 'hipanel\grid\MainColumn',
27
                'filterAttribute' => 'name_like',
28
                'note' => null,
29
                'value' => function (TariffProfile $model) {
30
                    if (empty($model->name) || $model->isDefault()) {
31
                        return Yii::t('hipanel.finance.tariffprofile', 'Default');
32
                    }
33
34
                    return $model->name;
35
                },
36
            ],
37
            'tariff_names' => [
38
                'filter' => false,
39
                'format' => 'raw',
40
                'value' => function (TariffProfile $model) {
41
                    if (empty($model->tariffs)) {
42
                        return '';
43
                    }
44
45
                    foreach ($model->tariffs as $type => $values) {
46
                        if (empty($values)) {
47
                            continue;
48
                        }
49
50
                        $links = [];
51
                        foreach ($values as $id => $name) {
52
                            $links[$id] = $this->tariffLink($id, $name);
53
                        }
54
                        $tariffs[$type] = $model->getAttributeLabel($type) . ': ' . implode(', ', $links);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tariffs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tariffs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
55
                    }
56
57
                    return implode('<br>', $tariffs);
58
                },
59
            ],
60
            'domain_tariff' => [
61
                'attribute' => 'domain',
62
                'format' => 'raw',
63 View Code Duplication
                'value' => function (TariffProfile $model) {
64
                    if (empty($model->domain)) {
65
                        return '';
66
                    }
67
68
                    return $this->tariffLink($model->domain, $model->tariff_names[$model->domain]);
69
                },
70
            ],
71
            'certificate_tariff' => [
72
                'attribute' => 'certificate',
73
                'format' => 'raw',
74 View Code Duplication
                'value' => function (TariffProfile $model) {
75
                    if (empty($model->certificate)) {
76
                        return '';
77
                    }
78
79
                    return $this->tariffLink($model->certificate, $model->tariff_names[$model->certificate]);
80
                },
81
            ],
82
            'svds_tariff' => [
83
                'attribute' => 'svds',
84
                'format' => 'raw',
85 View Code Duplication
                'value' => function (TariffProfile $model) {
86
                    if (empty($model->tariffs)) {
87
                        return '';
88
                    }
89
90
                    if (empty($model->tariffs[Tariff::TYPE_XEN])) {
91
                        return '';
92
                    }
93
94
                    foreach ($model->tariffs[Tariff::TYPE_XEN] as $id => $name) {
95
                        $links[$id] = $this->tariffLink($id, $name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$links was never initialized. Although not strictly required by PHP, it is generally a good practice to add $links = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
96
                    }
97
98
                    return implode(', ', $links);
99
                },
100
            ],
101
            'ovds_tariff' => [
102
                'attribute' => 'ovds',
103
                'format' => 'raw',
104 View Code Duplication
                'value' => function (TariffProfile $model) {
105
                    if (empty($model->tariffs)) {
106
                        return '';
107
                    }
108
109
                    if (empty($model->tariffs[Tariff::TYPE_OPENVZ])) {
110
                        return '';
111
                    }
112
113
                    foreach ($model->tariffs[Tariff::TYPE_OPENVZ] as $id => $name) {
114
                        $links[$id] = $this->tariffLink($id, $name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$links was never initialized. Although not strictly required by PHP, it is generally a good practice to add $links = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
115
                    }
116
117
                    return implode(', ', $links);
118
                },
119
            ],
120
            'server_tariff' => [
121
                'attribute' => 'server',
122
                'format' => 'raw',
123 View Code Duplication
                'value' => function (TariffProfile $model) {
124
                    if (empty($model->tariffs)) {
125
                        return '';
126
                    }
127
128
                    if (empty($model->tariffs[Tariff::TYPE_SERVER])) {
129
                        return '';
130
                    }
131
132
                    foreach ($model->tariffs[Tariff::TYPE_SERVER] as $id => $name) {
133
                        $links[$id] = $this->tariffLink($id, $name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$links was never initialized. Although not strictly required by PHP, it is generally a good practice to add $links = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
134
                    }
135
136
                    return implode(', ', $links);
137
                },
138
            ],
139
            'actions' => [
140
                'class' => MenuColumn::class,
141
                'menuClass' => ProfileActionsMenu::class,
142
            ],
143
        ]);
144
    }
145
146
    protected function tariffLink($id, $name)
147
    {
148
        return Html::a($name, ['@tariff/view', 'id' => $id]);
149
    }
150
}
151