HdomainGridView   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 9
dl 0
loc 150
ccs 0
cts 135
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C columns() 0 147 9
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-2019, 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\models\Hdomain;
18
use hipanel\modules\hosting\widgets\backup\BackupGridRow;
19
use hipanel\modules\hosting\widgets\hdomain\State;
20
use hipanel\modules\server\grid\ServerColumn;
21
use hipanel\widgets\ArraySpoiler;
22
use hipanel\widgets\Label;
23
use hipanel\widgets\XEditable;
24
use Yii;
25
use yii\helpers\Html;
26
27
class HdomainGridView extends \hipanel\grid\BoxedGridView
28
{
29
    public function columns()
30
    {
31
        return array_merge(parent::columns(), [
32
            'hdomain' => [
33
                'class' => MainColumn::class,
34
                'filterAttribute' => 'domain_like',
35
                'attribute' => 'domain',
36
            ],
37
            'hdomain_with_aliases' => [
38
                'format' => 'raw',
39
                'attribute' => 'domain',
40
                'filterAttribute' => 'domain_like',
41
                'value' => function ($model) {
42
                    $aliases = (array) $model->getAttribute('aliases');
43
44
                    $html = Html::a($model->domain, ['view', 'id' => $model->id], ['class' => 'bold']) . '&nbsp;';
45
                    $html .= ArraySpoiler::widget([
46
                        'data' => $aliases,
47
                        'visibleCount' => 0,
48
                        'delimiter' => '<br />',
49
                        'button' => [
50
                            '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...
51
                            'class' => 'badge progress-bar-info',
52
                            'popoverOptions' => ['html' => true],
53
                        ],
54
                        'formatter' => function ($value, $key) {
55
                            return Html::a($value, ['view', 'id' => $key]);
56
                        },
57
                    ]);
58
59
                    return $html;
60
                },
61
            ],
62
            'account' => [
63
                'format' => 'html',
64
                'value' => function (Hdomain $hdomain): string {
65
                    return Html::a($hdomain->account, ['@account/view', 'id' => $hdomain->account_id]);
0 ignored issues
show
Documentation introduced by
The property account does not exist on object<hipanel\modules\hosting\models\Hdomain>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property account_id does not exist on object<hipanel\modules\hosting\models\Hdomain>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

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