Completed
Push — master ( 5f78b9...e7f319 )
by Dmitry
26:47 queued 11:50
created

BackupingController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 104
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 8
dl 13
loc 104
ccs 0
cts 96
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 13 13 1
B actions() 0 59 1
A getDayOptions() 0 6 1
A getHourOptions() 0 6 1
A getMethodOptions() 0 4 1
A getStateOptions() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\SmartDeleteAction;
15
use hipanel\actions\SmartPerformAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\actions\ValidateFormAction;
18
use hipanel\actions\ViewAction;
19
use hipanel\helpers\ArrayHelper;
20
use hipanel\models\Ref;
21
use hipanel\modules\hosting\models\Backuping;
22
use hipanel\modules\hosting\models\BackupSearch;
23
use hipanel\filters\EasyAccessControl;
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 array_merge(parent::behaviors(), [
32
            [
33
                'class' => EasyAccessControl::class,
34
                'actions' => [
35
                    'update' => 'account.update',
36
                    'delete' => 'account.delete',
37
                    '*' => 'account.read',
38
                ],
39
            ],
40
        ]);
41
    }
42
43
    public function actions()
44
    {
45
        return array_merge(parent::actions(), [
46
            'index' => [
47
                'class' => IndexAction::class,
48
                'data' => function ($action) {
49
                    return [
50
                        'stateOptions' => $action->controller->getStateOptions(),
51
                    ];
52
                },
53
                'filterStorageMap' => [
54
                    'state' => 'hosting.backuping.state',
55
                    'server' => 'server.server.name',
56
                    'account' => 'hosting.account.login',
57
                    'client_id' => 'client.client.id',
58
                ],
59
            ],
60
            'update' => [
61
                'class' => SmartUpdateAction::class,
62
                'success' => Yii::t('hipanel:hosting', 'Backup settings have been changed'),
63
                'data' => function ($action) {
64
                    return [
65
                        'methodOptions' => $action->controller->getMethodOptions(),
66
                        'dayOptions' => $action->controller->getDayOptions(),
67
                        'hourOptions' => $action->controller->getHourOptions(),
68
                    ];
69
                },
70
            ],
71
            'disable' => [
72
                'class' => SmartPerformAction::class,
73
            ],
74
            'enable' => [
75
                'class' => SmartPerformAction::class,
76
            ],
77
            'delete' => [
78
                'class' => SmartDeleteAction::class,
79
                'success' => Yii::t('hipanel', 'Deleted'),
80
                'error' => Yii::t('hipanel', 'An error occurred. Try again please.'),
81
            ],
82
            'view' => [
83
                'class' => ViewAction::class,
84
                'findOptions' => ['show_deleted' => true],
85
                'data' => function ($action) {
86
                    $backupSearch = new BackupSearch();
87
                    $backupsDataProvider = $backupSearch->search([$backupSearch->formName() => ['object_id' => $action->getId()]]);
88
                    $backupsDataProvider->setSort(['defaultOrder' => ['time' => SORT_DESC]]);
89
90
                    return [
91
                        'stateOptions' => $action->controller->getStateOptions(),
92
                        'backupsDataProvider' => $backupsDataProvider,
93
                        'hasBackup' => Backuping::find()->where(['id' => $action->getId()])->exists(),
94
                    ];
95
                },
96
            ],
97
            'validate-form' => [
98
                'class' => ValidateFormAction::class,
99
            ],
100
        ]);
101
    }
102
103
    public function getDayOptions()
104
    {
105
        return ArrayHelper::map(Ref::find()->where(['gtype' => 'type,day', 'select' => 'full'])->all(), 'id', function ($model) {
106
            return Yii::t('hipanel:hosting', $model->label);
107
        });
108
    }
109
110
    public function getHourOptions()
111
    {
112
        return ArrayHelper::map(Ref::find()->where(['gtype' => 'type,hour', 'select' => 'full'])->all(), 'id', function ($model) {
113
            return Yii::t('hipanel:hosting', $model->label);
114
        });
115
    }
116
117
    public function getMethodOptions()
118
    {
119
        return $this->getRefs('type,backup_method', 'hipanel:hosting');
120
    }
121
122
    public function getStateOptions()
123
    {
124
        return [
125
            'ok' => Yii::t('hipanel', 'Ok'),
126
            'disabled' => Yii::t('hipanel', 'Disabled'),
127
            'deleted' => Yii::t('hipanel', 'Deleted'),
128
        ];
129
    }
130
}
131