Completed
Pull Request — master (#2)
by
unknown
12:33
created

AccountController::actions()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 189
Code Lines 133

Duplication

Lines 11
Ratio 5.82 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 11
loc 189
ccs 0
cts 154
cp 0
rs 5.2676
cc 8
eloc 133
nc 1
nop 0
crap 72

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
/**
12
 * @see    http://hiqdev.com/hipanel-module-hosting
13
 * @license http://hiqdev.com/hipanel-module-hosting/license
14
 * @copyright Copyright (c) 2015 HiQDev
15
 */
16
17
namespace hipanel\modules\hosting\controllers;
18
19
use hipanel\actions\IndexAction;
20
use hipanel\actions\OrientationAction;
21
use hipanel\actions\RenderJsonAction;
22
use hipanel\actions\PrepareBulkAction;
23
use hipanel\actions\RedirectAction;
24
use hipanel\actions\SearchAction;
25
use hipanel\actions\SmartCreateAction;
26
use hipanel\actions\SmartDeleteAction;
27
use hipanel\actions\SmartUpdateAction;
28
use hipanel\actions\ValidateFormAction;
29
use hipanel\actions\ViewAction;
30
use Yii;
31
use yii\base\Event;
32
33
class AccountController extends \hipanel\base\CrudController
34
{
35
    public function actions()
36
    {
37
        return [
38
            'index' => [
39
                'class' => IndexAction::class,
40
                'data' => function ($action) {
41
                    return [
42
                        'stateData' => $action->controller->getStateData(),
43
                        'typeData' => $action->controller->getTypeData(),
44
                    ];
45
                },
46
                'filterStorageMap' => [
47
                    'login_like' => 'hosting.account.login',
48
                    'server' => 'server.server.name',
49
                    'state' => 'hosting.account.state',
50
                    'type' => 'hosting.account.type',
51
                    'client_id' => 'client.client.id',
52
                    'seller_id' => 'client.client.seller_id',
53
                ],
54
            ],
55
            'view' => [
56
                'class' => ViewAction::class,
57
                'findOptions' => [
58
                    'with_mail_settings' => true,
59
                ],
60
                'data' => function ($action) {
61
                    return [
62
                        'blockReasons' => $action->controller->getBlockReasons(),
63
                    ];
64
                },
65
            ],
66
            'create' => [
67
                'class' => SmartCreateAction::class,
68
                'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'),
69
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'),
70
            ],
71
            'create-ftponly' => [
72
                'class' => SmartCreateAction::class,
73
                'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'),
74
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'),
75
            ],
76
            'change-password' => [
77
                'class' => SmartUpdateAction::class,
78
                'view' => '_changePasswordModal',
79
                'POST' => [
80
                    'save' => true,
81
                    'success' => [
82
                        'class' => RenderJsonAction::class,
83
                        'return' => function ($action) {
84
                            return ['success' => !$action->collection->hasErrors()];
85
                        },
86
                    ],
87
                ],
88
            ],
89
            'set-allowed-ips' => [
90
                'class' => SmartUpdateAction::class,
91
                'view' => '_ipRestrictionsModal',
92
                'success' => Yii::t('hipanel:hosting', 'Allowed IPs changing task has been successfully added to queue'),
93
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change allowed IPs'),
94
            ],
95
            'set-mail-settings' => [
96
                'class' => SmartUpdateAction::class,
97
                'success' => Yii::t('hipanel:hosting', 'Mail settings where changed'),
98
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change mail settings'),
99
            ],
100
            'enable-block' => [
101
                'class' => SmartUpdateAction::class,
102
                'success' => Yii::t('hipanel:hosting', 'Account was blocked successfully'),
103
                'error' => Yii::t('hipanel:hosting', 'Error during the account blocking'),
104
            ],
105
            'disable-block' => [
106
                'class' => SmartUpdateAction::class,
107
                'success' => Yii::t('hipanel:hosting', 'Account was unblocked successfully'),
108
                'error' => Yii::t('hipanel:hosting', 'Error during the account unblocking'),
109
            ],
110
            'validate-form' => [
111
                'class' => ValidateFormAction::class,
112
            ],
113
            'single-validate-form' => [
114
                'class' => ValidateFormAction::class,
115
                'validatedInputId' => false,
116
            ],
117
            'delete' => [
118
                'class' => SmartDeleteAction::class,
119
                'success' => Yii::t('hipanel:hosting', 'Account deleting task has been added to queue'),
120
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to delete account'),
121
            ],
122
            'bulk-delete' => [
123
                'class' => SmartDeleteAction::class,
124
                'success' => Yii::t('hipanel:hosting', 'Account deleting task has been added to queue'),
125
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to delete account'),
126
            ],
127
            'bulk-delete-modal' => [
128
                'class' => PrepareBulkAction::class,
129
                'view' => '_bulkDelete',
130
            ],
131
            'get-directories-list' => [
132
                'class' => SearchAction::class,
133
                'findOptions' => ['with_directories' => true],
134
                'ajaxResponseFormatter' => function ($action) {
135
                    $results = [];
136
137
                    $model = $action->collection->first;
138
                    $pathLike = Yii::$app->request->post('path_like');
139
140
                    foreach ($model['path'] as $path) {
141
                        if ($pathLike) {
142
                            if (preg_match('|' . $pathLike . '|', $path)) {
143
                                array_unshift($results, ['id' => $path, 'text' => $path]);
144
                                continue;
145
                            }
146
                        }
147
148
                        $results[] = ['id' => $path, 'text' => $path];
149
                    }
150
151
                    return $results;
152
                },
153
            ],
154
            'bulk-enable-block' => [
155
                'class' => SmartUpdateAction::class,
156
                'scenario' => 'enable-block',
157
                'success' => Yii::t('hipanel:hosting', 'Hosting accounts were blocked successfully'),
158
                'error' => Yii::t('hipanel:hosting', 'Error during the hosting accounts blocking'),
159
                'POST html' => [
160
                    'save'    => true,
161
                    'success' => [
162
                        'class' => RedirectAction::class,
163
                    ],
164
                ],
165
                'on beforeSave' => function (Event $event) {
166
                    /** @var \hipanel\actions\Action $action */
167
                    $action = $event->sender;
168
                    $type = Yii::$app->request->post('type');
169
                    $comment = Yii::$app->request->post('comment');
170
                    if (!empty($type)) {
171
                        foreach ($action->collection->models as $model) {
172
                            $model->setAttributes([
173
                                'type' => $type,
174
                                'comment' => $comment,
175
                            ]);
176
                        }
177
                    }
178
                },
179
            ],
180
            'bulk-enable-block-modal' => [
181
                'class' => PrepareBulkAction::class,
182
                'view' => '_bulkEnableBlock',
183
                'data' => function ($action, $data) {
184
                    return array_merge($data, [
185
                        'blockReasons' => $this->getBlockReasons(),
186
                    ]);
187
                },
188
            ],
189
            'bulk-disable-block' => [
190
                'class' => SmartUpdateAction::class,
191
                'scenario' => 'disable-block',
192
                'success' => Yii::t('hipanel:hosting', 'Hosting accounts were unblocked successfully'),
193
                'error' => Yii::t('hipanel:hosting', 'Error during the hosting accounts unblocking'),
194
                'POST html' => [
195
                    'save'    => true,
196
                    'success' => [
197
                        'class' => RedirectAction::class,
198
                    ],
199
                ],
200 View Code Duplication
                'on beforeSave' => function (Event $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
                    /** @var \hipanel\actions\Action $action */
202
                    $action = $event->sender;
203
                    $type = Yii::$app->request->post('type');
204
                    $comment = Yii::$app->request->post('comment');
205
                    if (!empty($type)) {
206
                        foreach ($action->collection->models as $model) {
207
                            $model->setAttribute('comment', $comment);
208
                        }
209
                    }
210
                },
211
            ],
212
            'bulk-disable-block-modal' => [
213
                'class' => PrepareBulkAction::class,
214
                'scenario' => 'disable-block',
215
                'view' => '_bulkDisableBlock',
216
                'data' => function ($action, $data) {
217
                    return array_merge($data, [
218
                        'blockReasons' => $this->getBlockReasons(),
219
                    ]);
220
                },
221
            ],
222
        ];
223
    }
224
225
    public function getStateData()
226
    {
227
        return $this->getRefs('state,account', 'hipanel:hosting');
228
    }
229
230
    public function getTypeData()
231
    {
232
        return $this->getRefs('type,account', 'hipanel:hosting');
233
    }
234
}
235