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

HdomainGridView::columns()   D

Complexity

Conditions 9
Paths 1

Size

Total Lines 142
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 142
ccs 0
cts 131
cp 0
rs 4.8196
cc 9
eloc 102
nc 1
nop 0
crap 90

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\ActionColumn;
14
use hipanel\grid\MainColumn;
15
use hipanel\grid\RefColumn;
16
use hipanel\helpers\Url;
17
use hipanel\modules\hosting\widgets\backup\BackupGridRow;
18
use hipanel\modules\hosting\widgets\hdomain\State;
19
use hipanel\modules\server\grid\ServerColumn;
20
use hipanel\widgets\ArraySpoiler;
21
use hipanel\widgets\Label;
22
use hipanel\widgets\XEditable;
23
use Yii;
24
use yii\helpers\Html;
25
26
class HdomainGridView extends \hipanel\grid\BoxedGridView
27
{
28
    public function columns()
29
    {
30
        return array_merge(parent::columns(), [
31
            'hdomain' => [
32
                'class' => MainColumn::class,
33
                'filterAttribute' => 'domain_like',
34
                'attribute' => 'domain',
35
            ],
36
            'hdomain_with_aliases' => [
37
                'format' => 'raw',
38
                'attribute' => 'domain',
39
                'filterAttribute' => 'domain_like',
40
                'value' => function ($model) {
41
                    $aliases = (array) $model->getAttribute('aliases');
42
43
                    $html = Html::a($model->domain, ['view', 'id' => $model->id], ['class' => 'bold']) . '&nbsp;';
44
                    $html .= ArraySpoiler::widget([
45
                        'data' => $aliases,
46
                        'visibleCount' => 0,
47
                        'delimiter' => '<br />',
48
                        'button' => [
49
                            'label' => Yii::t('hipanel', '+{0, plural, one{# alias} other{# aliases}}', count($aliases)),
0 ignored issues
show
Documentation introduced by
count($aliases) is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
                            'class' => 'badge progress-bar-info',
51
                            'popoverOptions' => ['html' => true],
52
                        ],
53
                        'formatter' => function ($value, $key) {
54
                            return Html::a($value, ['view', 'id' => $key]);
55
                        },
56
                    ]);
57
58
                    return $html;
59
                },
60
            ],
61
            'account' => [
62
                'class' => AccountColumn::class,
63
            ],
64
            'server' => [
65
                'class' => ServerColumn::class,
66
            ],
67
            'ip' => [
68
                'enableSorting' => false,
69
                'filter' => false,
70
                'format' => 'raw',
71
                'value' => function ($model) {
72
                    $vhost = $model->getAttribute('vhost');
73
74
                    $html = $vhost['ip'];
75
                    if (isset($vhost['port']) && $vhost['port'] !== 80) {
76
                        $html .= ':' . $vhost['port'];
77
                    }
78
                    if ($model->isProxied) {
79
                        $backend = $vhost['backend'];
80
                        $html .= ' ' . Html::tag('i', '', ['class' => 'fa fa-long-arrow-right']) . ' ' . $backend['ip'];
81
                        if ($backend['port'] !== 80) {
82
                            $html .= ':' . $backend['port'];
83
                        }
84
                    }
85
                    return $html;
86
                },
87
            ],
88
            'service' => [
89
                'label' => Yii::t('hipanel', 'Service'),
90
                'value' => function ($model) {
91
                    return $model->getAttribute('vhost')['service'];
92
                },
93
            ],
94
            'state' => [
95
                'class' => RefColumn::class,
96
                'i18nDictionary' => 'hipanel:hosting',
97
                'format' => 'raw',
98
                'value' => function ($model) {
99
                    $html = '';
100
                    if ($model->dns_on && empty($model->dns_hdomain_id)) {
101
                        $html .= Label::widget([
102
                            'color' => 'success',
103
                            'label' => Yii::t('hipanel', 'DNS'),
104
                            'tag' => 'span',
105
                            'labelOptions' => ['title' => Yii::t('hipanel:hosting', 'DNS is enabled')],
106
                        ]);
107
                    }
108
                    $html .= ' ' . State::widget(compact('model'));
109
                    return $html;
110
                },
111
                'gtype' => 'state,hdomain',
112
            ],
113
            'dns_on' => [
114
                'format' => 'raw',
115
                'value' => function ($model) {
116
                    return $model->dns_on ? Yii::t('hipanel', 'Enabled') : Yii::t('hipanel', 'Disabled');
117
                },
118
            ],
119
            'dns_switch' => [
120
                'attribute' => 'dns_on',
121
                'label' => Yii::t('hipanel:hosting', 'DNS'),
122
                'format' => 'raw',
123
                'value' => function ($model) {
124
                    if (empty($model->dns_hdomain_id)) {
125
                        return XEditable::widget([
126
                            'model' => $model,
127
                            'attribute' => 'dns_on',
128
                            'pluginOptions' => [
129
                                'type' => 'select',
130
                                'title' => Yii::t('hipanel:hosting', 'Toggles DNS records upload on NS server'),
131
                                'source' => ['' => Yii::t('hipanel', 'Disabled'), '1' => Yii::t('hipanel', 'Enabled')],
132
                                'url' => Url::to('set-dns-on'),
133
                                'placement' => 'bottom',
134
                            ],
135
                            'linkOptions' => [
136
                                'style' => ['word-break' => 'break-all'],
137
                            ],
138
                        ]);
139
                    } else {
140
                        return Yii::t('hipanel:hosting', 'Belongs to {link}', [
141
                            'link' => Html::a($model->dns_hdomain_domain, Url::to(['@hdomain/view', 'id' => $model->dns_hdomain_id])),
142
                        ]);
143
                    }
144
                },
145
            ],
146
            'aliases' => [
147
                'label' => Yii::t('hipanel', 'Aliases'),
148
                'format' => 'raw',
149
                'value' => function ($model) {
150
                    return ArraySpoiler::widget([
151
                        'data' => (array) $model->getAttribute('aliases'),
152
                        'delimiter' => '<br />',
153
                        'button' => ['popoverOptions' => ['html' => true]],
154
                    ]);
155
                },
156
            ],
157
            'backups_widget' => [
158
                'label' => Yii::t('hipanel:hosting', 'Backups'),
159
                'format' => 'raw',
160
                'value' => function ($model) {
161
                    return BackupGridRow::widget(['model' => $model]);
162
                },
163
            ],
164
            'actions' => [
165
                'class' => ActionColumn::class,
166
                'template' => '{view} {delete}',
167
            ],
168
        ]);
169
    }
170
}
171