Completed
Push — master ( 78c2e1...01635f )
by Andrii
19:30 queued 15:45
created

ServerActionsMenu::items()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 80
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 80
ccs 0
cts 77
cp 0
rs 8.7143
c 0
b 0
f 0
cc 5
nc 12
nop 0
crap 30

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
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\menus;
12
13
use hipanel\modules\server\models\Server;
14
use hipanel\widgets\AjaxModalWithTemplatedButton;
15
use hiqdev\yii2\menus\Menu;
16
use Yii;
17
use yii\bootstrap\Modal;
18
use yii\helpers\Html;
19
20
class ServerActionsMenu extends Menu
21
{
22
    /**
23
     * @var Server
24
     */
25
    public $model;
26
27
    public function items(): array
28
    {
29
        $user = Yii::$app->user;
30
31
        $items = [
32
            'view' => [
33
                'label' => Yii::t('hipanel', 'View'),
34
                'icon' => 'fa-info',
35
                'url' => ['@server/view', 'id' => $this->model->id],
36
                'linkOptions' => [
37
                    'data-pjax' => 0,
38
                ],
39
            ],
40
            'update' => [
41
                'label' => Yii::t('hipanel', 'Update'),
42
                'icon' => 'fa-pencil',
43
                'url' => ['@server/update', 'id' => $this->model->id],
44
                'linkOptions' => [
45
                    'data-pjax' => 0,
46
                ],
47
                'visible' => Yii::$app->user->can('server.update'),
0 ignored issues
show
Bug introduced by Andrey Klochok
The method can() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
                'visible' => Yii::$app->user->/** @scrutinizer ignore-call */ can('server.update'),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
            ],
49
            [
50
                'label' => Yii::t('hipanel:server', 'Renew server'),
51
                'icon' => 'fa-forward',
52
                'url' => ['add-to-cart-renewal', 'model_id' => $this->model->id],
53
                'linkOptions' => [
54
                    'data-pjax' => 0,
55
                ],
56
                'visible' => !empty($this->model->expires) && Yii::$app->user->can('server.pay') && Yii::$app->params['module.server.renew.allowed'],
0 ignored issues
show
Bug Best Practice introduced by Andrey Klochok
The property expires does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
57
            ],
58
            'assign-hubs' => [
59
                'label' => Yii::t('hipanel:server', 'Assign hubs'),
60
                'icon' => 'fa-plug',
61
                'url' => ['@server/assign-hubs', 'id' => $this->model->id],
62
                'visible' => Yii::$app->user->can('server.update'),
63
                'linkOptions' => [
64
                    'data-pjax' => 0,
65
                ],
66
            ],
67
            [
68
                'label' => Yii::t('hipanel:server', 'Performance graphs'),
69
                'icon' => 'fa-signal',
70
                'url' => ['@rrd/view', 'id' => $this->model->id],
71
                'visible' => $this->model->canRrd(),
72
                'linkOptions' => [
73
                    'data-pjax' => 0,
74
                ],
75
            ],
76
            [
77
                'label' => Yii::t('hipanel:server', 'Switch graphs'),
78
                'icon' => 'fa-area-chart',
79
                'url' => ['@switch-graph/view', 'id' => $this->model->id],
80
                'linkOptions' => [
81
                    'data-pjax' => 0,
82
                ],
83
            ],
84
            [
85
                'label' => Yii::t('hipanel:server', 'Server IPs'),
86
                'icon' => 'fa-location-arrow',
87
                'url' => ['@ip/index', 'IpSearch' => ['server_in' => $this->model->name]],
88
                'linkOptions' => [
89
                    'data-pjax' => 0,
90
                ],
91
                'visible' => $user->can('ip.read') && Yii::getAlias('@ip', false),
0 ignored issues
show
Bug introduced by Andrey Klochok
The method can() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
                'visible' => $user->/** @scrutinizer ignore-call */ can('ip.read') && Yii::getAlias('@ip', false),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
            ],
93
            [
94
                'label' => Yii::t('hipanel:server', 'Server Accounts'),
95
                'icon' => 'fa-user',
96
                'url' => ['@account/index', 'AccountSearch' => ['server' => $this->model->name]],
97
                'linkOptions' => [
98
                    'data-pjax' => 0,
99
                ],
100
                'visible' => $user->can('account.read') && Yii::getAlias('@account', false),
101
            ],
102
        ];
103
104
        array_splice($items, 9, 0, $this->getSettingsItems());
105
106
        return $items;
107
    }
108
109
    private function getSettingsItems(): array
110
    {
111
        $items = [];
112
113
        foreach ([
114
                     'hardware-settings' => [
115
                         'label' => Yii::t('hipanel:server', 'Hardware properties'),
116
                         'url' => ['hardware-settings', 'id' => $this->model->id],
117
                         'size' => Modal::SIZE_LARGE,
118
                     ],
119
                     'software-settings' => [
120
                         'label' => Yii::t('hipanel:server', 'Software properties'),
121
                         'url' => ['software-settings', 'id' => $this->model->id],
122
                     ],
123
                     'monitoring-settings' => [
124
                         'label' => Yii::t('hipanel:server', 'Monitoring properties'),
125
                         'url' => ['monitoring-settings', 'id' => $this->model->id],
126
                     ],
127
                     'mail-settings' => [
128
                         'label' => Yii::t('hipanel:server', 'Mail settings'),
129
                         'url' => ['mail-settings', 'id' => $this->model->id],
130
                         'size' => Modal::SIZE_SMALL,
131
                     ],
132
                 ] as $key => $item) {
133
            array_push($items, [
134
                'label' => AjaxModalWithTemplatedButton::widget([
135
                    'ajaxModalOptions' => [
136
                        'id' => "{$key}-modal-{$this->model->id}",
137
                        'bulkPage' => true,
138
                        'header' => Html::tag('h4', $item['label'], ['class' => 'modal-title']),
139
                        'scenario' => 'default',
140
                        'actionUrl' => $item['url'],
141
                        'size' => $item['size'],
142
                        'handleSubmit' => $item['url'],
143
                        'toggleButton' => [
144
                            'tag' => 'a',
145
                            'label' => Html::tag('i', null, ['class' => 'fa fa-fw fa-cogs']) . $item['label'],
146
                            'style' => 'cursor: pointer;',
147
                        ],
148
                    ],
149
                    'toggleButtonTemplate' => '{toggleButton}',
150
                ]),
151
                'encode' => false,
152
                'visible' => Yii::$app->user->can('server.manage-settings'),
153
            ]);
154
        }
155
156
        return $items;
157
    }
158
}
159