Completed
Push — master ( 141dff...6b8d6b )
by Dmitry
05:19
created

HubController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
eloc 56
dl 0
loc 106
ccs 0
cts 92
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDigitalCapacityOptions() 0 3 1
A actions() 0 49 1
A getNicMediaOptions() 0 3 1
A getSnmpOptions() 0 3 1
A getTypes() 0 3 1
A behaviors() 0 9 1
A getFullFromRef() 0 15 1
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\SmartCreateAction;
15
use hipanel\actions\SmartUpdateAction;
16
use hipanel\actions\ViewAction;
17
use hipanel\base\CrudController;
18
use hipanel\filters\EasyAccessControl;
19
use hipanel\helpers\ArrayHelper;
20
use hipanel\models\Ref;
21
use Yii;
22
use yii\base\Event;
23
24
class HubController extends CrudController
25
{
26
    public function behaviors()
27
    {
28
        return array_merge(parent::behaviors(), [
29
            [
30
                'class' => EasyAccessControl::class,
31
                'actions' => [
32
                    'create' => 'hub.create',
33
                    'update,options' => 'hub.update',
34
                    '*' => 'hub.read',
35
                ],
36
            ],
37
        ]);
38
    }
39
40
    public function actions()
41
    {
42
        return array_merge(parent::actions(), [
43
            'index' => [
44
                'class' => IndexAction::class,
45
                'data' => function () {
46
                    return [
47
                        'types' => $this->getTypes(),
48
                    ];
49
                },
50
            ],
51
            'view' => [
52
                'on beforePerform' => function (Event $event) {
53
                    /** @var \hipanel\actions\SearchAction $action */
54
                    $action = $event->sender;
55
                    $dataProvider = $action->getDataProvider();
56
                    $dataProvider->query->joinWith(['bindings']);
57
                    $dataProvider->query
58
                        ->andWhere(['with_bindings' => 1])
59
                        ->andWhere(['with_servers' => 1]);
60
                },
61
                'class' => ViewAction::class,
62
            ],
63
            'create' => [
64
                'class' => SmartCreateAction::class,
65
                'success' => Yii::t('hipanel:server:hub', 'Switch was created'),
66
                'data' => function () {
67
                    return [
68
                        'types' => $this->getTypes(),
69
                    ];
70
                },
71
            ],
72
            'update' => [
73
                'class' => SmartUpdateAction::class,
74
                'success' => Yii::t('hipanel:server:hub', 'Switch was updated'),
75
                'data' => function () {
76
                    return [
77
                        'types' => $this->getTypes(),
78
                    ];
79
                },
80
            ],
81
            'options' => [
82
                'class' => SmartUpdateAction::class,
83
                'success' => Yii::t('hipanel:server:hub', 'Options was updated'),
84
                'data' => function () {
85
                    return [
86
                        'snmpOptions' => $this->getSnmpOptions(),
87
                        'digitalCapacityOptions' => $this->getDigitalCapacityOptions(),
88
                        'nicMediaOptions' => $this->getNicMediaOptions(),
89
                    ];
90
                },
91
            ],
92
        ]);
93
    }
94
95
    protected function getTypes()
96
    {
97
        return $this->getFullFromRef('type,device,switch');
98
    }
99
100
    protected function getSnmpOptions()
101
    {
102
        return $this->getFullFromRef('type,snmp_version');
103
    }
104
105
    protected function getDigitalCapacityOptions()
106
    {
107
        return $this->getFullFromRef('type,digit_capacity');
108
    }
109
110
    protected function getNicMediaOptions()
111
    {
112
        return $this->getFullFromRef('type,nic_media');
113
    }
114
115
    protected function getFullFromRef($gtype)
116
    {
117
        $callingMethod = debug_backtrace()[1]['function'];
118
        $result = Yii::$app->get('cache')->getOrSet([$callingMethod], function () use ($gtype) {
119
            $result = ArrayHelper::map(Ref::find()->where([
120
                'gtype' => $gtype,
121
                'select' => 'full',
122
            ])->all(), 'id', function ($model) {
123
                return Yii::t('hipanel:server:hub', $model->label);
124
            });
125
126
            return $result;
127
        }, 86400 * 24); // 24 days
128
129
        return $result;
130
    }
131
}
132