Completed
Push — master ( 20a6b1...b57415 )
by Klochok
15:20
created

BackupingController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 0
cts 40
cp 0
rs 9.4743
c 0
b 0
f 0
cc 1
eloc 44
nc 1
nop 0
crap 2

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\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\OrientationAction;
15
use hipanel\actions\SmartDeleteAction;
16
use hipanel\actions\SmartPerformAction;
17
use hipanel\actions\SmartUpdateAction;
18
use hipanel\actions\ValidateFormAction;
19
use hipanel\actions\ViewAction;
20
use hipanel\helpers\ArrayHelper;
21
use hipanel\models\Ref;
22
use hipanel\modules\hosting\models\Backuping;
23
use hipanel\modules\hosting\models\BackupSearch;
24
use Yii;
25
use yii\filters\AccessControl;
26
27
class BackupingController extends \hipanel\base\CrudController
28
{
29 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
30
    {
31
        return ArrayHelper::merge(parent::behaviors(), [
32
            'manage-access' => [
33
                'class' => AccessControl::class,
34
                'only' => ['update'],
35
                'rules' => [
36
                    [
37
                        'allow' => true,
38
                        'roles' => ['support'],
39
                    ],
40
                ],
41
            ],
42
        ]);
43
    }
44
45
    public function actions()
46
    {
47
        return [
48
            'index' => [
49
                'class' => IndexAction::class,
50
                'data' => function ($action) {
51
                    return [
52
                        'stateOptions' => $action->controller->getStateOptions(),
53
                        'typeOptions' => $action->controller->getTypeOptions(),
54
                    ];
55
                },
56
                'filterStorageMap' => [
57
                    'state' => 'hosting.backuping.state',
58
                    'server' => 'server.server.name',
59
                    'account' => 'hosting.account.login',
60
                    'client_id' => 'client.client.id',
61
                ],
62
            ],
63
            'update' => [
64
                'class' => SmartUpdateAction::class,
65
                'success' => Yii::t('hipanel:hosting', 'Backup settings have been changed'),
66
                'data' => function ($action) {
67
                    return [
68
                        'typeOptions' => $action->controller->getTypeOptions(),
69
                        'methodOptions' => $action->controller->getMethodOptions(),
70
                        'dayOptions' => $action->controller->getDayOptions(),
71
                        'hourOptions' => $action->controller->getHourOptions(),
72
                    ];
73
                }
74
            ],
75
            'disable' => [
76
                'class' => SmartPerformAction::class,
77
            ],
78
            'enable' => [
79
                'class' => SmartPerformAction::class,
80
            ],
81
            'delete' => [
82
                'class' => SmartDeleteAction::class,
83
                'success' => Yii::t('hipanel', 'Deleted'),
84
                'error' => Yii::t('hipanel', 'An error occurred. Try again please.'),
85
            ],
86
            'view' => [
87
                'class' => ViewAction::class,
88
                'findOptions' => ['show_deleted' => true],
89
                'data' => function ($action) {
90
                    $backupSearch = new BackupSearch();
91
                    $backupsDataProvider = $backupSearch->search([$backupSearch->formName() => ['object_id' => $action->getId()]]);
92
                    $backupsDataProvider->setSort(['defaultOrder' => ['time' => SORT_DESC]]);
93
94
                    return [
95
                        'stateOptions' => $action->controller->getStateOptions(),
96
                        'typeOptions' => $action->controller->getTypeOptions(),
97
                        'backupsDataProvider' => $backupsDataProvider,
98
                        'hasBackup' => Backuping::find()->where(['id' => $action->getId()])->exists(),
99
                    ];
100
                },
101
            ],
102
            'validate-form' => [
103
                'class' => ValidateFormAction::class,
104
            ],
105
        ];
106
    }
107
108
    public function getDayOptions()
109
    {
110
        return ArrayHelper::map(Ref::find()->where(['gtype' => 'type,day', 'select' => 'full'])->all(), 'id', function ($model) {
111
            return Yii::t('hipanel:hosting', $model->label);
112
        });
113
    }
114
115
    public function getHourOptions()
116
    {
117
        return ArrayHelper::map(Ref::find()->where(['gtype' => 'type,hour', 'select' => 'full'])->all(), 'id', function ($model) {
118
            return Yii::t('hipanel:hosting', $model->label);
119
        });
120
    }
121
122
    public function getTypeOptions()
123
    {
124
        return $this->getRefs('type,backuping', 'hipanel:hosting:backuping:periodicity');
125
    }
126
127
    public function getMethodOptions()
128
    {
129
        return $this->getRefs('type,backup_method', 'hipanel:hosting');
130
    }
131
132
    public function getStateOptions()
133
    {
134
        return [
135
            'ok' => Yii::t('hipanel', 'Ok'),
136
            'disabled' => Yii::t('hipanel', 'Disabled'),
137
            'deleted' => Yii::t('hipanel', 'Deleted'),
138
        ];
139
    }
140
}
141