Completed
Push — master ( 692da5...8a82b5 )
by Dmitry
05:31
created

ServiceController::getSofts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Hosting Plugin for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-hosting
7
 * @package   hipanel-module-hosting
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
/**
13
 * @link    http://hiqdev.com/hipanel-module-hosting
14
 * @license http://hiqdev.com/hipanel-module-hosting/license
15
 * @copyright Copyright (c) 2015 HiQDev
16
 */
17
18
namespace hipanel\modules\hosting\controllers;
19
20
use hipanel\actions\Action;
21
use hipanel\actions\IndexAction;
22
use hipanel\actions\OrientationAction;
23
use hipanel\actions\SmartCreateAction;
24
use hipanel\actions\SmartDeleteAction;
25
use hipanel\actions\SmartUpdateAction;
26
use hipanel\actions\ValidateFormAction;
27
use hipanel\actions\ViewAction;
28
use hipanel\modules\hosting\models\Soft;
29
use Yii;
30
use yii\base\Event;
31
use yii\filters\AccessControl;
32
use yii\helpers\ArrayHelper;
33
34
class ServiceController extends \hipanel\base\CrudController
35
{
36 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...
37
    {
38
        return ArrayHelper::merge(parent::behaviors(), [
39
            'manage-access' => [
40
                'class' => AccessControl::class,
41
                'only'  => ['create', 'update', 'delete'],
42
                'rules' => [
43
                    [
44
                        'allow'   => true,
45
                        'roles'   => ['admin'],
46
                    ],
47
                ],
48
            ],
49
        ]);
50
    }
51
52
    public function actions()
53
    {
54
        return [
55
            'set-orientation' => [
56
                'class' => OrientationAction::class,
57
                'allowedRoutes' => [
58
                    '/hosting/service/index'
59
                ]
60
            ],
61
            'index' => [
62
                'class' => IndexAction::class,
63 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...
64
                    /** @var \hipanel\actions\SearchAction $action */
65
                    $action = $event->sender;
66
                    $dataProvider = $action->getDataProvider();
67
                    $dataProvider->query->joinWith('ips')->joinWith('objects_count');
68
                },
69
                'filterStorageMap' => [
70
                    'ip' => 'hosting.ip.ip',
71
                    'state' => 'hosting.service.state',
72
                    'server' => 'server.server.name',
73
                    'account' => 'hosting.account.login',
74
                    'client_id' => 'client.client.id',
75
                ],
76
                '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...
77
                    return [
78
                        'stateData' => $this->getStateData(),
79
                        'softData' => $this->getSofts(),
80
                    ];
81
                }
82
            ],
83
            'view' => [
84
                'class' => ViewAction::class,
85 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...
86
                    /** @var \hipanel\actions\SearchAction $action */
87
                    $action = $event->sender;
88
                    $dataProvider = $action->getDataProvider();
89
                    $dataProvider->query->joinWith('ips')->joinWith('object_count');
90
                }
91
            ],
92
            'create' => [
93
                'class' => SmartCreateAction::class,
94
                'data' => function ($action) {
95
                    /** @var Action $action */
96
                    return [
97
                        'states' => $action->controller->getStateData(),
98
                        'softs' => $action->controller->getSofts(),
99
                    ];
100
                },
101
                'success' => Yii::t('hipanel/hosting', 'Service was created successfully'),
102
                'error' => Yii::t('hipanel/hosting', 'An error occurred when trying to create a service')
103
            ],
104
            'update' => [
105
                'class' => SmartUpdateAction::class,
106
                'data' => function ($action) {
107
                    /** @var Action $action */
108
                    return [
109
                        'states' => $action->controller->getStateData(),
110
                        'softs' => $action->controller->getSofts(),
111
                    ];
112
                },
113
                'on beforeFetchLoad' => function (Event $event) {
114
                    /** @var \hipanel\actions\SearchAction $action */
115
                    $action = $event->sender;
116
                    $dataProvider = $action->getDataProvider();
117
                    $dataProvider->query->joinWith('ips');
118
                },
119
                'success' => Yii::t('hipanel/hosting', 'Service was updated successfully'),
120
                'error' => Yii::t('hipanel/hosting', 'An error occurred when trying to update a service')
121
            ],
122
            'delete' => [
123
                'class' => SmartDeleteAction::class,
124
                'success' => Yii::t('hipanel/hosting', 'Service was deleted successfully'),
125
            ],
126
            'validate-form' => [
127
                'class' => ValidateFormAction::class,
128
            ]
129
        ];
130
    }
131
132
    public function getStateData()
133
    {
134
        return $this->getRefs('state,service', 'hipanel/hosting');
135
    }
136
137
    public function getSofts()
138
    {
139
        $softs = Soft::find()->all();
140
        return ArrayHelper::map($softs, 'name', 'name');
141
    }
142
}
143