Completed
Push — master ( 055fd1...11841d )
by Klochok
24:15 queued 09:17
created

RequestGridView::columns()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 63
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 63
ccs 0
cts 59
cp 0
rs 9.4347
cc 2
eloc 44
nc 1
nop 0
crap 6

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
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\grid;
12
13
use hipanel\grid\RefColumn;
14
use hipanel\modules\hosting\menus\RequestActionsMenu;
15
use hipanel\modules\server\grid\ServerColumn;
16
use hiqdev\yii2\menus\grid\MenuColumn;
17
use Yii;
18
use yii\helpers\Html;
19
use yii\helpers\UnsetArrayValue;
20
21
class RequestGridView extends \hipanel\grid\BoxedGridView
22
{
23
    public function columns()
24
    {
25
        return array_merge(parent::columns(), [
26
            'classes' => [
27
                'label' => Yii::t('hipanel:hosting', 'Action'),
28
                'filter' => false,
29
                'enableSorting' => false,
30
                'value' => function ($model) {
31
                    return sprintf('%s, %s', $model->object_class, $model->action);
32
                },
33
            ],
34
            'server' => [
35
                'sortAttribute' => 'server',
36
                'attribute' => 'server_id',
37
                'class' => ServerColumn::class,
38
            ],
39
            'account' => [
40
                'enableSorting' => false,
41
                'class' => AccountColumn::class,
42
            ],
43
            'object' => [
44
                'enableSorting' => false,
45
                'filter' => false,
46
                'format' => 'raw',
47
                'value' => function ($model) {
48
                    return Html::a('<i class="fa fa-external-link"></i>&nbsp;' . $model->object_name,
49
                        ['/hosting/' . $model->object_class . '/view', 'id' => $model->object_id],
50
                        ['data-pjax' => 0]
51
                    );
52
                },
53
            ],
54
            'time' => [
55
                'filter' => false,
56
                'value' => function ($model) {
57
                    return Yii::$app->formatter->asDatetime($model->time);
58
                },
59
            ],
60
            'state' => [
61
                'class' => RefColumn::class,
62
                'i18nDictionary' => 'hipanel:hosting',
63
                'gtype' => 'state,request',
64
                'format' => 'raw',
65
                'value' => function ($model) {
66
                    $colors = [
67
                        'error' => 'danger',
68
                        'progress' => 'info',
69
                        'done' => 'success',
70
                    ];
71
72
                    return Html::tag('span', Yii::t('hipanel:hosting', $model->state_label), [
73
                        'class' => 'text-' . (isset($colors[$model->state]) ? $colors[$model->state] : 'default'),
74
                    ]);
75
                },
76
                'filterOverrides' => [
77
                    'done' => new UnsetArrayValue(),
78
                ],
79
            ],
80
            'actions' => [
81
                'class' => MenuColumn::class,
82
                'menuClass' => RequestActionsMenu::class,
83
            ],
84
        ]);
85
    }
86
}
87