Completed
Push — master ( 9ea30c...5bd098 )
by Dmitry
15:07
created

BackupingGridView::columns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 78
ccs 0
cts 71
cp 0
rs 8.9019
cc 1
eloc 56
nc 1
nop 0
crap 2

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
/**
12
 * @see    http://hiqdev.com/hipanel-module-hosting
13
 * @license http://hiqdev.com/hipanel-module-hosting/license
14
 * @copyright Copyright (c) 2015 HiQDev
15
 */
16
17
namespace hipanel\modules\hosting\grid;
18
19
use hipanel\grid\MainColumn;
20
use hipanel\helpers\Url;
21
use hipanel\modules\hosting\widgets\backup\ObjectLabelWidget;
22
use hipanel\modules\server\grid\ServerColumn;
23
use hiqdev\xeditable\widgets\XEditable;
24
use Yii;
25
use yii\helpers\Html;
26
27
class BackupingGridView extends \hipanel\grid\BoxedGridView
28
{
29
    public $typeOptions;
30
31
    public function getTypeOptions()
32
    {
33
        $result = [];
34
        foreach ($this->typeOptions as $key => $value) {
35
            $result[$key] = Yii::t('hipanel:hosting:backuping:periodicity', $value);
36
        }
37
38
        return $result;
39
    }
40
41
    public function columns()
42
    {
43
        $typeOptions = $this->getTypeOptions();
44
45
        return array_merge(parent::columns(), [
46
            'name' => [
47
                'class' => MainColumn::class,
48
                'filterAttribute' => 'name_like',
49
                'format' => 'html',
50
                'value' => function ($model) {
51
                    return Html::a($model->name, ['@' . $model->object . '/view', 'id' => $model->id], ['class' => 'bold']);
52
                },
53
            ],
54
            'main' => [
55
                'attribute' => 'name',
56
                'filterAttribute' => 'name_like',
57
                'format' => 'raw',
58
                'value' => function ($model) {
59
                    return  Html::a($model->name, ['@backuping/view', 'id' => $model->id], ['class' => 'bold']) . ' ' .
60
                            Html::a(ObjectLabelWidget::widget(compact('model')), ['@' . $model->object . '/view', 'id' => $model->id]);
61
                },
62
            ],
63
            'account' => [
64
                'attribute' => 'account_id',
65
                'class' => AccountColumn::class,
66
            ],
67
            'server' => [
68
                'attribute' => 'server_id',
69
                'class' => ServerColumn::class,
70
            ],
71
            'object' => [
72
                'filter' => false,
73
                'format' => 'raw',
74
                'value' => function ($model) {
75
                    return ObjectLabelWidget::widget(compact('model'));
76
                },
77
            ],
78
            'backup_count' => [
79
                'filter' => false,
80
            ],
81
            'type' => [
82
                'attribute' => 'type',
83
                'format' => 'raw',
84
                'filter' => false,
85
                'enableSorting' => false,
86
                'value' => function ($model) use ($typeOptions) {
87
                    return XEditable::widget([
88
                        'model' => $model,
89
                        'attribute' => 'type',
90
                        'pluginOptions' => [
91
                            'type' => 'select',
92
                            'source' => $typeOptions,
93
                            'url' => Url::to('update'),
94
                        ],
95
                    ]);
96
                },
97
            ],
98
            'state_label' => [
99
                'filter' => false,
100
                'enableSorting' => false,
101
            ],
102
            'backup_last' => [
103
                'filter' => false,
104
                'format' => 'raw',
105
                'value' => function ($model) {
106
                    return Html::tag('nobr', Yii::$app->formatter->asDate($model->backup_last)) . ' ' .
107
                           Html::tag('nobr', Yii::$app->formatter->asTime($model->backup_last));
108
                },
109
            ],
110
            'total_du' => [
111
                'filter' => false,
112
                'format' => 'html',
113
                'value' => function ($model) {
114
                    return Yii::$app->formatter->asShortSize($model->total_du, 2);
115
                },
116
            ],
117
        ]);
118
    }
119
}
120