Completed
Push — master ( 823023...c43f27 )
by Dmitry
06:01 queued 02:13
created

HubController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
eloc 87
dl 0
loc 152
ccs 0
cts 135
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDigitalCapacityOptions() 0 3 1
B actions() 0 96 7
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\Action;
14
use hipanel\actions\IndexAction;
15
use hipanel\actions\SmartCreateAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\actions\ValidateFormAction;
18
use hipanel\actions\ViewAction;
19
use hipanel\base\CrudController;
20
use hipanel\filters\EasyAccessControl;
21
use hipanel\helpers\ArrayHelper;
22
use hipanel\models\Ref;
23
use hipanel\modules\server\forms\HubSellForm;
24
use hiqdev\hiart\Collection;
25
use Yii;
26
use yii\base\Event;
27
28
class HubController extends CrudController
29
{
30
    public function behaviors()
31
    {
32
        return array_merge(parent::behaviors(), [
33
            [
34
                'class' => EasyAccessControl::class,
35
                'actions' => [
36
                    'create' => 'hub.create',
37
                    'update,options' => 'hub.update',
38
                    '*' => 'hub.read',
39
                ],
40
            ],
41
        ]);
42
    }
43
44
    public function actions()
45
    {
46
        return array_merge(parent::actions(), [
47
            'index' => [
48
                'class' => IndexAction::class,
49
                'data' => function () {
50
                    return [
51
                        'types' => $this->getTypes(),
52
                    ];
53
                },
54
            ],
55
            'view' => [
56
                'on beforePerform' => function (Event $event) {
57
                    /** @var \hipanel\actions\SearchAction $action */
58
                    $action = $event->sender;
59
                    $dataProvider = $action->getDataProvider();
60
                    $dataProvider->query->joinWith(['bindings']);
61
                    $dataProvider->query
62
                        ->andWhere(['with_bindings' => 1])
63
                        ->andWhere(['with_servers' => 1]);
64
                },
65
                'class' => ViewAction::class,
66
            ],
67
            'create' => [
68
                'class' => SmartCreateAction::class,
69
                'success' => Yii::t('hipanel:server:hub', 'Switch was created'),
70
                'data' => function () {
71
                    return [
72
                        'types' => $this->getTypes(),
73
                    ];
74
                },
75
            ],
76
            'update' => [
77
                'class' => SmartUpdateAction::class,
78
                'success' => Yii::t('hipanel:server:hub', 'Switch was updated'),
79
                'data' => function () {
80
                    return [
81
                        'types' => $this->getTypes(),
82
                    ];
83
                },
84
            ],
85
            'options' => [
86
                'class' => SmartUpdateAction::class,
87
                'success' => Yii::t('hipanel:server:hub', 'Options was updated'),
88
                'data' => function () {
89
                    return [
90
                        'snmpOptions' => $this->getSnmpOptions(),
91
                        'digitalCapacityOptions' => $this->getDigitalCapacityOptions(),
92
                        'nicMediaOptions' => $this->getNicMediaOptions(),
93
                    ];
94
                },
95
            ],
96
            'sell' => [
97
                'class' => SmartUpdateAction::class,
98
                'success' => Yii::t('hipanel:server:hub', 'Switches were sold'),
99
                'collection' => [
100
                    'class' => Collection::class,
101
                    'model' => new HubSellForm(),
102
                    'scenario' => 'sell',
103
                ],
104
                'data' => function (Action $action, array $data) {
105
                    $result = [];
106
                    foreach ($data['models'] as $model) {
107
                        $result['models'][] = HubSellForm::fromHub($model);
108
                    }
109
                    $result['model'] = reset($result['models']);
110
111
                    return $result;
112
                },
113
                'on beforeSave' => function (Event $event) {
114
                    /** @var \hipanel\actions\Action $action */
115
                    $action = $event->sender;
116
                    $request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
117
118
                    if ($request->isPost) {
119
                        $values = [];
120
                        foreach (['client_id', 'tariff_id', 'sale_time'] as $attribute) {
121
                            $value = $request->post($attribute);
122
                            if (!empty($value)) {
123
                                $values[$attribute] = $value;
124
                            }
125
                        }
126
                        foreach ($action->collection->models as $model) {
127
                            foreach ($values as $attr => $value) {
128
                                $model->setAttribute($attr, $value);
129
                            }
130
                        }
131
                    }
132
                },
133
                'view' => 'modals/sell',
134
            ],
135
            'validate-sell-form' => [
136
                'class' => ValidateFormAction::class,
137
                'collection' => [
138
                    'class' => Collection::class,
139
                    'model' => new HubSellForm(),
140
                ],
141
            ],
142
        ]);
143
    }
144
145
    protected function getTypes()
146
    {
147
        return $this->getFullFromRef('type,device,switch');
148
    }
149
150
    protected function getSnmpOptions()
151
    {
152
        return $this->getFullFromRef('type,snmp_version');
153
    }
154
155
    protected function getDigitalCapacityOptions()
156
    {
157
        return $this->getFullFromRef('type,digit_capacity');
158
    }
159
160
    protected function getNicMediaOptions()
161
    {
162
        return $this->getFullFromRef('type,nic_media');
163
    }
164
165
    protected function getFullFromRef($gtype)
166
    {
167
        $callingMethod = debug_backtrace()[1]['function'];
168
        $result = Yii::$app->get('cache')->getOrSet([$callingMethod], function () use ($gtype) {
169
            $result = ArrayHelper::map(Ref::find()->where([
170
                'gtype' => $gtype,
171
                'select' => 'full',
172
            ])->all(), 'id', function ($model) {
173
                return Yii::t('hipanel:server:hub', $model->label);
174
            });
175
176
            return $result;
177
        }, 86400 * 24); // 24 days
178
179
        return $result;
180
    }
181
}
182