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

BackupingController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 13.16 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 8
dl 15
loc 114
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 15 15 1
A actions() 0 62 1
A getDayOptions() 0 6 1
A getHourOptions() 0 6 1
A getTypeOptions() 0 4 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-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