MailGridView::columns()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 0
cts 82
cp 0
rs 8.2181
c 0
b 0
f 0
cc 4
nc 1
nop 0
crap 20

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-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\modules\hosting\models\Mail;
17
use hipanel\modules\hosting\widgets\mail\State;
18
use hipanel\modules\hosting\widgets\mail\Type;
19
use hipanel\modules\server\grid\ServerColumn;
20
use hipanel\widgets\ArraySpoiler;
21
use hipanel\widgets\Label;
22
use Yii;
23
use yii\helpers\Html;
24
25
class MailGridView extends \hipanel\grid\BoxedGridView
26
{
27
    public function columns()
28
    {
29
        return array_merge(parent::columns(), [
30
            'mail' => [
31
                'class' => MainColumn::class,
32
                'filterAttribute' => 'mail_like',
33
            ],
34
            'state' => [
35
                'class' => RefColumn::class,
36
                'i18nDictionary' => 'hipanel:hosting',
37
                'format' => 'raw',
38
                'value' => function ($model) {
39
                    return State::widget(compact('model'));
40
                },
41
                'gtype' => 'state,mail',
42
            ],
43
            'server' => [
44
                'class' => ServerColumn::class,
45
            ],
46
            'du_limit' => [
47
                'attribute' => 'du_limit',
48
                'format' => 'html',
49
                'value' => function ($model) {
50
                    return !empty($model->du_limit) ? $model->du_limit . 'MB' : '';
51
                },
52
            ],
53
            'domain' => [
54
                'attribute' => 'hdomain_id',
55
                'format' => 'raw',
56
                'value' => function ($model) {
57
                    return Html::a($model->domain, ['@hdomain/view', 'id' => $model->hdomain_id]);
58
                },
59
            ],
60
            'type' => [
61
                'format' => 'raw',
62
                'filter' => function ($column, $model, $attribute) {
63
                    return Html::activeDropDownList($model, $attribute, ['' => '----------'] + Mail::getTypes(), [
64
                        'class' => 'form-control',
65
                    ]);
66
                },
67
                'value' => function ($model) {
68
                    return Type::widget(['model' => $model, 'label' => $model->type]);
69
                },
70
            ],
71
            'forwards' => [
72
                'format' => 'raw',
73
                'value' => function ($model) {
74
                    return ArraySpoiler::widget([
75
                        'delimiter' => '<br>',
76
                        'visibleCount' => 2,
77
                        'data' => $model->forwards,
78
                        'button' => [
79
                            'label' => '+{count}',
80
                            'popoverOptions' => ['html' => true],
81
                        ],
82
                    ]);
83
                },
84
            ],
85
            'account' => ['class' => AccountColumn::class],
86
            'spam_action' => [
87
                'format' => 'raw',
88
                'value' => function ($model) {
89
                    /** @var $model Mail */
90
                    if ($model->spam_action === $model::SPAM_ACTION_DELETE) {
0 ignored issues
show
Documentation introduced by
The property spam_action does not exist on object<hipanel\modules\hosting\models\Mail>. 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...
91
                        return Label::widget([
92
                            'color' => 'danger',
93
                            'label' => Yii::t('hipanel', 'Delete'),
94
                        ]);
95
                    } elseif ($model->spam_action === '') {
0 ignored issues
show
Documentation introduced by
The property spam_action does not exist on object<hipanel\modules\hosting\models\Mail>. 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...
96
                        return Label::widget([
97
                            'color' => 'info',
98
                            'label' => Yii::t('hipanel:hosting', 'Do nothing'),
99
                        ]);
100
                    } else {
101
                        return Label::widget([
102
                                'color' => 'primary',
103
                                'label' => Yii::t('hipanel:hosting', 'Forward to'),
104
                            ]) . ' ' . ArraySpoiler::widget([
105
                                'data' => $model->spam_action,
0 ignored issues
show
Documentation introduced by
The property spam_action does not exist on object<hipanel\modules\hosting\models\Mail>. 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...
106
                                'visibleCount' => 2,
107
                            ]);
108
                    }
109
                },
110
            ],
111
            'actions' => [
112
                'class' => ActionColumn::class,
113
                'template' => '{view} {delete}',
114
            ],
115
        ]);
116
    }
117
}
118