Completed
Push — master ( 5bd098...f52782 )
by Klochok
14:11
created

MailGridView::defaultColumns()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 89
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 0
cts 82
cp 0
rs 8.5731
c 0
b 0
f 0
cc 3
eloc 62
nc 1
nop 0
crap 12

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\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 $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(compact('model'));
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
            'spam_action' => [
86
                'format' => 'raw',
87
                'value' => function ($model) {
88
                    /** @var $model Mail */
89
                    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...
90
                        return Label::widget([
91
                            'color' => 'danger',
92
                            'label' => Yii::t('hipanel', 'Delete'),
93
                        ]);
94
                    } 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...
95
                        return Label::widget([
96
                            'color' => 'info',
97
                            'label' => Yii::t('hipanel:hosting', 'Do nothing'),
98
                        ]);
99
                    } else {
100
                        return Label::widget([
101
                                'color' => 'primary',
102
                                'label' => Yii::t('hipanel:hosting', 'Forward to'),
103
                            ]) . ' ' . ArraySpoiler::widget([
104
                                '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...
105
                                'visibleCount' => 2,
106
                            ]);
107
                    }
108
                },
109
            ],
110
            'actions' => [
111
                'class' => ActionColumn::class,
112
                'template' => '{view} {delete}',
113
            ],
114
        ]);
115
    }
116
}
117