Completed
Push — master ( 5071d1...56b015 )
by Klochok
14:27
created

HubController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 52
c 2
b 0
f 0
rs 9.4929
cc 1
eloc 34
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace hipanel\modules\server\controllers;
4
5
use hipanel\actions\IndexAction;
6
use hipanel\actions\SmartCreateAction;
7
use hipanel\actions\SmartUpdateAction;
8
use hipanel\actions\ViewAction;
9
use hipanel\base\CrudController;
10
use hipanel\helpers\ArrayHelper;
11
use hipanel\models\Ref;
12
use Yii;
13
use yii\base\Event;
14
15
class HubController extends CrudController
16
{
17
    public function actions()
18
    {
19
        return [
20
            'index' => [
21
                'class' => IndexAction::class,
22
                'data' => function () {
23
                    return [
24
                        'types' => $this->getTypes(),
25
                    ];
26
                }
27
            ],
28
            'view' => [
29
                'on beforePerform' => function (Event $event) {
30
                    /** @var \hipanel\actions\SearchAction $action */
31
                    $action = $event->sender;
32
                    $dataProvider = $action->getDataProvider();
33
                    $dataProvider->query->joinWith(['bindings']);
34
                    $dataProvider->query->andWhere(['with_bindings' => 1]);
35
                },
36
                'class' => ViewAction::class,
37
            ],
38
            'create' => [
39
                'class' => SmartCreateAction::class,
40
                'success' => Yii::t('hipanel:server:hub', 'Switch was created'),
41
                'data' => function () {
42
                    return [
43
                        'types' => $this->getTypes(),
44
                    ];
45
                }
46
            ],
47
            'update' => [
48
                'class' => SmartUpdateAction::class,
49
                'success' => Yii::t('hipanel:server:hub', 'Switch was updated'),
50
                'data' => function () {
51
                    return [
52
                        'types' => $this->getTypes(),
53
                    ];
54
                }
55
            ],
56
            'options' => [
57
                'class' => SmartUpdateAction::class,
58
                'success' => Yii::t('hipanel:server:hub', 'Options was updated'),
59
                'data' => function () {
60
                    return [
61
                        'snmpOptions' => $this->getSnmpOptions(),
62
                        'digitalCapacityOptions' => $this->getDigitalCapacityOptions(),
63
                        'nicMediaOptions' => $this->getNicMediaOptions(),
64
                    ];
65
                }
66
            ],
67
        ];
68
    }
69
70
    protected function getTypes()
71
    {
72
        return $this->getFullFromRef('type,device,switch');
73
    }
74
75
    protected function getSnmpOptions()
76
    {
77
        return $this->getFullFromRef('type,snmp_version');
78
    }
79
80
    protected function getDigitalCapacityOptions()
81
    {
82
        return $this->getFullFromRef('type,digit_capacity');
83
    }
84
85
    protected function getNicMediaOptions()
86
    {
87
        return $this->getFullFromRef('type,nic_media');
88
    }
89
90
    protected function getFullFromRef($gtype)
91
    {
92
        $callingMethod = debug_backtrace()[1]['function'];
93
        $result = Yii::$app->get('cache')->getOrSet([$callingMethod], function () use ($gtype) {
94
            $result = ArrayHelper::map(Ref::find()->where(['gtype' => $gtype, 'select' => 'full'])->all(), 'id', function ($model) {
95
                return Yii::t('hipanel:server:hub', $model->label);
96
            });
97
98
            return $result;
99
        }, 86400 * 24); // 24 days
100
101
        return $result;
102
    }
103
}