ServiceController::getStateData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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-2019, 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\Action;
20
use hipanel\actions\IndexAction;
21
use hipanel\actions\SmartCreateAction;
22
use hipanel\actions\SmartUpdateAction;
23
use hipanel\actions\ValidateFormAction;
24
use hipanel\actions\ViewAction;
25
use hipanel\filters\EasyAccessControl;
26
use hipanel\modules\hosting\models\Soft;
27
use Yii;
28
use yii\base\Event;
29
use yii\helpers\ArrayHelper;
30
31
class ServiceController extends \hipanel\base\CrudController
32
{
33 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...
34
    {
35
        return ArrayHelper::merge(parent::behaviors(), [
36
            [
37
                'class' => EasyAccessControl::class,
38
                'actions' => [
39
                    'create' => 'admin',
40
                    'update' => 'admin',
41
                    'delete' => 'admin',
42
                    '*' => 'server.read',
43
                ],
44
            ],
45
        ]);
46
    }
47
48
    public function actions()
49
    {
50
        return array_merge(parent::actions(), [
51
            'index' => [
52
                'class' => IndexAction::class,
53 View Code Duplication
                'on beforePerform' => 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...
54
                    /** @var \hipanel\actions\SearchAction $action */
55
                    $action = $event->sender;
56
                    $dataProvider = $action->getDataProvider();
57
                    $dataProvider->query->joinWith(['ips'])->addSelect('objects_count');
58
                },
59
                'filterStorageMap' => [
60
                    'ip' => 'hosting.ip.ip',
61
                    'state' => 'hosting.service.state',
62
                    'server' => 'server.server.name',
63
                    'account' => 'hosting.account.login',
64
                    'client_id' => 'client.client.id',
65
                ],
66
                'data' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
                    return [
68
                        'stateData' => $this->getStateData(),
69
                        'softData' => $this->getSofts(),
70
                    ];
71
                },
72
            ],
73
            'view' => [
74
                'class' => ViewAction::class,
75 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...
76
                    /** @var \hipanel\actions\SearchAction $action */
77
                    $action = $event->sender;
78
                    $dataProvider = $action->getDataProvider();
79
                    $dataProvider->query->joinWith('ips')->addSelect('objects_count');
80
                },
81
            ],
82
            'create' => [
83
                'class' => SmartCreateAction::class,
84
                'data' => function ($action) {
85
                    /** @var Action $action */
86
                    return [
87
                        'states' => $action->controller->getStateData(),
88
                        'softs' => $action->controller->getSofts(),
89
                    ];
90
                },
91
                'success' => Yii::t('hipanel:hosting', 'Service was created successfully'),
92
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create a service'),
93
            ],
94
            'update' => [
95
                'class' => SmartUpdateAction::class,
96
                'data' => function ($action) {
97
                    /** @var Action $action */
98
                    return [
99
                        'states' => $action->controller->getStateData(),
100
                        'softs' => $action->controller->getSofts(),
101
                    ];
102
                },
103
                'on beforeFetchLoad' => function (Event $event) {
104
                    /** @var \hipanel\actions\SearchAction $action */
105
                    $action = $event->sender;
106
                    $dataProvider = $action->getDataProvider();
107
                    $dataProvider->query->joinWith('ips');
108
                },
109
                'success' => Yii::t('hipanel:hosting', 'Service was updated successfully'),
110
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to update a service'),
111
            ],
112
            'validate-form' => [
113
                'class' => ValidateFormAction::class,
114
            ],
115
        ]);
116
    }
117
118
    public function getStateData()
119
    {
120
        return $this->getRefs('state,service', 'hipanel:hosting');
121
    }
122
123
    public function getSofts()
124
    {
125
        $softs = Soft::find()->all();
126
127
        return ArrayHelper::map($softs, 'name', 'name');
128
    }
129
}
130