Completed
Push — master ( 3d28fc...d59354 )
by Dmitry
09:20
created

ServerGridView::formatTariff()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * Server module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-server
7
 * @package   hipanel-module-server
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\server\grid;
13
14
use hipanel\grid\ActionColumn;
15
use hipanel\grid\MainColumn;
16
use hipanel\grid\RefColumn;
17
use hipanel\grid\XEditableColumn;
18
use hipanel\helpers\Url;
19
use hipanel\modules\server\widgets\DiscountFormatter;
20
use hipanel\modules\server\widgets\Expires;
21
use hipanel\modules\server\widgets\OSFormatter;
22
use hipanel\modules\server\widgets\State;
23
use hipanel\modules\hosting\controllers\AccountController;
24
use hipanel\modules\hosting\controllers\IpController;
25
use hipanel\widgets\ArraySpoiler;
26
use hipanel\widgets\Label;
27
use Yii;
28
use yii\helpers\ArrayHelper;
29
use yii\helpers\Html;
30
31
class ServerGridView extends \hipanel\grid\BoxedGridView
32
{
33
    public $controllerUrl = '@server';
34
35
    /**
36
     * @var array
37
     */
38
    public static $osImages;
39
40
    public static function setOsImages($osImages)
41
    {
42
        static::$osImages = $osImages;
43
    }
44
45
    public static function formatTariff($model)
46
    {
47
        if (Yii::$app->user->can('manage')) {
48
49
            if ($model->parent_tariff) {
50
                $html[] = Html::tag('abbr', $model->parent_tariff, ['title' => $model->tariff, 'data-toggle' => 'tooltip']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$html was never initialized. Although not strictly required by PHP, it is generally a good practice to add $html = 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...
51
            } else {
52
                $html[] = $model->tariff;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$html was never initialized. Although not strictly required by PHP, it is generally a good practice to add $html = 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...
53
            }
54
55
            $html[] = Html::a('<i class="fa fa-external-link"></i>', ['@tariff/view', 'id' => $model->tariff_id]);
56
57
            return implode(' ', $html);
58
        }
59
60
        return !empty($model->parent_tariff) ? $model->parent_tariff : $model->tariff;
61
    }
62
63
    public static function defaultColumns()
64
    {
65
        $osImages = self::$osImages;
66
67
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('server' =>... => $model->id)); }))); (array<string,array>) is incompatible with the return type of the parent method hipanel\grid\GridView::defaultColumns of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
            'server' => [
69
                'class' => MainColumn::class,
70
                'attribute' => 'name',
71
                'filterAttribute' => 'name_like',
72
                'note' => Yii::$app->user->can('support') ? 'label' : 'note',
73
                'noteOptions' => [
74
                    'url' => Yii::$app->user->can('support') ? Url::to('set-label') : Url::to('set-note'),
75
                ],
76
                'badges' => function ($model) {
77
                    $badges = '';
78
                    if (Yii::$app->user->can('support')) {
79
                        if ($model->wizzarded) {
80
                            $badges .= Label::widget(['label' => 'W', 'tag' => 'sup', 'color' => 'success']);
81
                        }
82
                        /*if ($model->state === 'disabled') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
                            $badges .= ' ' . Label::widget(['label' => 'Panel OFF', 'tag' => 'sup', 'color' => 'danger', 'type' => 'text']);
84
                        }*/
85
                    }
86
87
                    return $badges;
88
                },
89
            ],
90
            'dc' => [
91
                'attribute' => 'dc',
92
                'filter' => false,
93
            ],
94
            'state' => [
95
                'class' => RefColumn::class,
96
                'i18nDictionary' => 'hipanel:server',
97
                'format' => 'raw',
98
                'gtype' => 'state,device',
99
                'value' => function ($model) {
100
                    $html = State::widget(compact('model'));
101
                    if ($model->status_time) {
102
                        $html .= ' '. Html::tag('nobr', Yii::t('hipanel:server', 'since {date}', ['date' => Yii::$app->formatter->asDate($model->status_time)]));
103
                    }
104
                    return $html;
105
                },
106
            ],
107
            'panel' => [
108
                'attribute' => 'panel',
109
                'format' => 'text',
110
                'contentOptions' => ['class' => 'text-uppercase'],
111
                'value' => function ($model) {
112
                    return $model->panel ? Yii::t('hipanel:server:panel', $model->panel) : Yii::t('hipanel:server:panel', 'No control panel');
113
                },
114
            ],
115
            'os' => [
116
                'attribute' => 'os',
117
                'format' => 'raw',
118
                'value' => function ($model) use ($osImages) {
119
                    return OSFormatter::widget([
120
                        'osimages' => $osImages,
121
                        'imageName' => $model->osimage,
122
                    ]);
123
                },
124
            ],
125
            'os_and_panel' => [
126
                'attribute' => 'os',
127
                'format' => 'raw',
128
                'value' => function ($model) use ($osImages) {
129
                    $html = OSFormatter::widget([
130
                        'osimages' => $osImages,
131
                        'imageName' => $model->osimage,
132
                    ]);
133
                    $html .= ' ' . $model->panel ?: '';
134
                    return $html;
135
                },
136
            ],
137
            'discount' => [
138
                'attribute' => 'discount',
139
                'label' => Yii::t('hipanel:server', 'Discount'),
140
                'format' => 'raw',
141
                'headerOptions' => ['style' => 'width: 1em'],
142 View Code Duplication
                'value' => function ($model) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
143
                    return DiscountFormatter::widget([
144
                        'current' => $model->discounts['fee']['current'],
145
                        'next' => $model->discounts['fee']['next'],
146
                    ]);
147
                },
148
            ],
149
            'expires' => [
150
                'filter' => false,
151
                'format' => 'raw',
152
                'headerOptions' => ['style' => 'width: 1em'],
153
                'value' => function ($model) {
154
                    return Expires::widget(compact('model'));
155
                },
156
            ],
157
            'tariff' => [
158
                'format' => 'raw',
159
                'filterAttribute' => 'tariff_like',
160
                'value' => function ($model) {
161
                    return self::formatTariff($model);
162
                },
163
            ],
164
            'tariff_and_discount' => [
165
                'attribute' => 'tariff',
166
                'filterAttribute' => 'tariff_like',
167
                'format' => 'raw',
168 View Code Duplication
                'value' => function ($model) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
169
                    return self::formatTariff($model) . ' ' . DiscountFormatter::widget([
170
                        'current' => $model->discounts['fee']['current'],
171
                        'next' => $model->discounts['fee']['next'],
172
                    ]);
173
                },
174
            ],
175
            'ip' => [
176
                'filter' => false,
177
            ],
178
            'mac' => [
179
                'filter' => false,
180
            ],
181
            'ips' => [
182
                'format' => 'raw',
183
                'attribute' => 'ips',
184
                'filter' => false,
185
                'value' => function ($model) {
186
                    return ArraySpoiler::widget([
187
                        'data' => ArrayHelper::getColumn($model->ips, 'ip'),
188
                        'delimiter' => '<br />',
189
                        'visibleCount' => 3,
190
                        'button' => ['popoverOptions' => ['html' => true]],
191
                    ]);
192
                },
193
            ],
194
            'sale_time' => [
195
                'attribute' => 'sale_time',
196
                'format' => 'datetime',
197
            ],
198
            'note' => [
199
                'class' => XEditableColumn::class,
200
                'pluginOptions' => [
201
                    'url'       => Url::to('set-note'),
202
                ],
203
                'widgetOptions' => [
204
                    'linkOptions' => [
205
                        'data-type' => 'textarea',
206
                    ],
207
                ],
208
            ],
209
            'label' => [
210
                'class' => XEditableColumn::class,
211
                'visible' => Yii::$app->user->can('support'),
212
                'pluginOptions' => [
213
                    'url'       => Url::to('set-label'),
214
                ],
215
                'widgetOptions' => [
216
                    'linkOptions' => [
217
                        'data-type' => 'textarea',
218
                    ],
219
                ],
220
            ],
221
            'type' => [
222
                'format' => 'html',
223
                'filter' => false,
224
                'value'  => function ($model) {
225
                    return $model->type_label;
226
                },
227
            ],
228
            'rack' => [
229
                'format' => 'html',
230
                'filter' => false,
231
                'value'  => function ($model) {
232
                    return $model->switches['rack']['switch'];
233
                },
234
            ],
235
            'net' => [
236
                'format' => 'html',
237
                'filter' => false,
238
                'value'  => function ($model) {
239
                    return static::renderSwitchPort($model->switches['net']);
240
                },
241
            ],
242
            'kvm' => [
243
                'format' => 'html',
244
                'filter' => false,
245
                'value'  => function ($model) {
246
                    return static::renderSwitchPort($model->switches['kvm']);
247
                },
248
            ],
249
            'pdu' => [
250
                'format' => 'html',
251
                'filter' => false,
252
                'value'  => function ($model) {
253
                    return static::renderSwitchPort($model->switches['pdu']);
254
                },
255
            ],
256
            'ipmi' => [
257
                'format' => 'raw',
258
                'filter' => false,
259
                'value'  => function ($model) {
260
                    $ipmi = $model->switches['ipmi']['device_ip'];
261
                    $link = $ipmi ? Html::a($ipmi, "http://$ipmi/", ['target' => '_blank']) . ' ' : '';
262
                    return $link . static::renderSwitchPort($model->switches['ipmi']);
263
                },
264
            ],
265
            'nums' => [
266
                'label' => '',
267
                'format' => 'raw',
268
                'value' => function ($model) {
269
                    $ips_num = $model->ips_num;
270
                    $ips = $ips_num ? Html::a("$ips_num ips", IpController::getSearchUrl(['server' => $model->name])) : 'no ips';
271
                    $act_acs_num = $model->acs_num - $model->del_acs_num;
272
                    $del_acs_num = $model->del_acs_num;
273
                    $acs_num = $act_acs_num . ($del_acs_num ? "+$del_acs_num" : '');
274
                    $acs = $acs_num ? Html::a("$acs_num acc", AccountController::getSearchUrl(['server' => $model->name])) : 'no acc';
275
                    return Html::tag('nobr', $ips) . ' ' . Html::tag('nobr', $acs);
276
                },
277
            ],
278
            'actions' => [
279
                'class' => ActionColumn::class,
280
                'template' => '{view} {rrd} {switch-graph}',
281
                'buttons' => [
282
                    'switch-graph' => function ($url, $model) {
283
                        return Html::a('<i class="fa fa-fw fa-area-chart"></i>' . Yii::t('hipanel:server', 'Switch graphs'), ['@switch-graph/view', 'id' => $model->id]);
284
                    },
285
                    'rrd' => function ($url, $model) {
286
                        return Html::a('<i class="fa fa-fw fa-signal"></i>' . Yii::t('hipanel:server', 'Resources usage graphs'), ['@rrd/view', 'id' => $model->id]);
287
                    },
288
                ],
289
            ],
290
        ];
291
    }
292
293
    public static function renderSwitchPort($data)
294
    {
295
        $label  = $data['switch_label'];
296
        $inn    = $data['switch_inn'];
297
        $name   = $data['switch'];
298
        $port   = $data['port'];
299
300
        $inn    = $inn ? "($inn)" : '';
301
        $main   = $port ? "$name:$port" : $name;
302
        $main   = $main ? "<b>$main</b>" : '';
303
304
        return "$inn $main $label";
305
    }
306
307
    public static function defaultRepresentations()
308
    {
309
        return [
310
            'common' => [
311
                'label'   => Yii::t('hipanel', 'common'),
312
                'columns' => [
313
                    'checkbox',
314
                    'server', 'client_id', 'seller_id',
315
                    'ips', 'state', 'expires',
316
                    'tariff_and_discount',
317
                ],
318
            ],
319
            'manager' => Yii::$app->user->can('support') ? [
320
                'label'   => Yii::t('hipanel:server', 'manager'),
321
                'columns' => [
322
                    'checkbox', 'client_id',
323
                    'rack', 'server', 'tariff',
324
                    'hwsummary', 'nums', 'actions',
325
                ],
326
            ] : null,
327
            'admin' => Yii::$app->user->can('support') ? [
328
                'label'   => Yii::t('hipanel:server', 'admin'),
329
                'columns' => [
330
                    'checkbox', 'dc', 'server', 'type',
331
                    'net', 'kvm', 'ipmi', 'pdu', 'ip', 'mac',
332
                ],
333
            ] : null,
334
        ];
335
    }
336
}
337