Completed
Push — master ( 678f4c...d13b92 )
by Andrii
10:30
created

HubController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\ProxyAction;
16
use hipanel\actions\SmartCreateAction;
17
use hipanel\actions\SmartUpdateAction;
18
use hipanel\actions\ValidateFormAction;
19
use hipanel\actions\ViewAction;
20
use hipanel\base\CrudController;
21
use hipanel\filters\EasyAccessControl;
22
use hipanel\helpers\ArrayHelper;
23
use hipanel\models\Ref;
24
use hipanel\modules\server\forms\AssignSwitchesForm;
25
use hipanel\modules\server\forms\HubSellForm;
26
use hipanel\modules\server\models\HardwareSettings;
27
use hiqdev\hiart\Collection;
28
use Yii;
29
use yii\base\Event;
30
use yii\web\NotFoundHttpException;
31
32
class HubController extends CrudController
33
{
34
    public function behaviors()
35
    {
36
        return array_merge(parent::behaviors(), [
37
            [
38
                'class' => EasyAccessControl::class,
39
                'actions' => [
40
                    'create' => 'hub.create',
41
                    'update,options' => 'hub.update',
42
                    '*' => 'hub.read',
43
                ],
44
            ],
45
        ]);
46
    }
47
48
    public function actions()
49
    {
50
        return array_merge(parent::actions(), [
51
            'index' => [
52
                'class' => IndexAction::class,
53
                'data' => function () {
54
                    return [
55
                        'types' => $this->getTypes(),
56
                    ];
57
                },
58
            ],
59
            'view' => [
60
                'on beforePerform' => function (Event $event) {
61
                    /** @var \hipanel\actions\SearchAction $action */
62
                    $action = $event->sender;
63
                    $dataProvider = $action->getDataProvider();
64
                    $dataProvider->query->joinWith([
65
                        'bindings',
66
                        'hardwareSettings'
67
                    ]);
68
                    $dataProvider->query
69
                        ->andWhere([
70
                            'with_bindings' => 1,
71
                            'with_servers' => 1,
72
                            'with_hardwareSettings' => 1,
73
                        ]);
74
                },
75
                'class' => ViewAction::class,
76
            ],
77
            'validate-form' => [
78
                'class' => ValidateFormAction::class,
79
            ],
80
            'create' => [
81
                'class' => SmartCreateAction::class,
82
                'success' => Yii::t('hipanel:server:hub', 'Switch was created'),
83
                'data' => function () {
84
                    return [
85
                        'types' => $this->getTypes(),
86
                    ];
87
                },
88
            ],
89
            'update' => [
90
                'class' => SmartUpdateAction::class,
91
                'success' => Yii::t('hipanel:server:hub', 'Switch was updated'),
92
                'data' => function () {
93
                    return [
94
                        'types' => $this->getTypes(),
95
                    ];
96
                },
97
            ],
98
            'options' => [
99
                'class' => SmartUpdateAction::class,
100
                'success' => Yii::t('hipanel:server:hub', 'Options was updated'),
101
                'data' => function () {
102
                    return [
103
                        'snmpOptions' => $this->getSnmpOptions(),
104
                        'digitalCapacityOptions' => $this->getDigitalCapacityOptions(),
105
                        'nicMediaOptions' => $this->getNicMediaOptions(),
106
                    ];
107
                },
108
            ],
109
            'sell' => [
110
                'class' => SmartUpdateAction::class,
111
                'success' => Yii::t('hipanel:server:hub', 'Switches were sold'),
112
                'view' => 'modal/_bulkSale',
113
                'collection' => [
114
                    'class' => Collection::class,
115
                    'model' => new HubSellForm(),
116
                    'scenario' => 'sell',
117
                ],
118
                'data' => function (Action $action, array $data) {
119
                    $result = [];
120
                    foreach ($data['models'] as $model) {
121
                        $result['models'][] = HubSellForm::fromHub($model);
122
                    }
123
                    $result['model'] = reset($result['models']);
124
125
                    return $result;
126
                },
127
                'on beforeSave' => function (Event $event) {
128
                    /** @var \hipanel\actions\Action $action */
129
                    $action = $event->sender;
130
                    $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...
131
132
                    if ($request->isPost) {
133
                        $values = [];
134
                        foreach (['client_id', 'tariff_id', 'sale_time'] as $attribute) {
135
                            $value = $request->post($attribute);
136
                            if (!empty($value)) {
137
                                $values[$attribute] = $value;
138
                            }
139
                        }
140
                        foreach ($action->collection->models as $model) {
141
                            foreach ($values as $attr => $value) {
142
                                $model->setAttribute($attr, $value);
143
                            }
144
                        }
145
                    }
146
                },
147
            ],
148
            'assign-switches' => [
149
                'class' => SmartUpdateAction::class,
150
                'success' => Yii::t('hipanel:server:hub', 'Switches have been edited'),
151
                'view' => 'assign-switches',
152
                'on beforeFetch' => function (Event $event) {
153
                    /** @var \hipanel\actions\SearchAction $action */
154
                    $action = $event->sender;
155
                    $dataProvider = $action->getDataProvider();
156
                    $dataProvider->query->withBindings()->select(['*']);
157
                },
158
                'collection' => [
159
                    'class' => Collection::class,
160
                    'model' => new AssignSwitchesForm(),
161
                    'scenario' => 'default',
162
                ],
163
                'data' => function (Action $action, array $data) {
164
                    $result = [];
165
                    foreach ($data['models'] as $model) {
166
                        $result['models'][] = AssignSwitchesForm::fromOriginalModel($model);
167
                    }
168
                    if (!$result['models']) {
169
                        throw new NotFoundHttpException('There are no entries available for the selected operation.');
170
                    }
171
                    $result['model'] = reset($result['models']);
172
173
                    return $result;
174
                },
175
            ],
176
            'validate-switches-form' => [
177
                'class' => ValidateFormAction::class,
178
                'collection' => [
179
                    'class' => Collection::class,
180
                    'model' => new AssignSwitchesForm(),
181
                ],
182
            ],
183
            'validate-sell-form' => [
184
                'class' => ValidateFormAction::class,
185
                'collection' => [
186
                    'class' => Collection::class,
187
                    'model' => new HubSellForm(),
188
                ],
189
            ],
190
        ]);
191
    }
192
193
    protected function getTypes()
194
    {
195
        return $this->getFullFromRef('type,device,switch');
196
    }
197
198
    protected function getSnmpOptions()
199
    {
200
        return $this->getFullFromRef('type,snmp_version');
201
    }
202
203
    protected function getDigitalCapacityOptions()
204
    {
205
        return $this->getFullFromRef('type,digit_capacity');
206
    }
207
208
    protected function getNicMediaOptions()
209
    {
210
        return $this->getFullFromRef('type,nic_media');
211
    }
212
213
    protected function getFullFromRef($gtype)
214
    {
215
        $callingMethod = debug_backtrace()[1]['function'];
216
        $result = Yii::$app->get('cache')->getOrSet([$callingMethod], function () use ($gtype) {
217
            $result = ArrayHelper::map(Ref::find()->where([
218
                'gtype' => $gtype,
219
                'select' => 'full',
220
            ])->all(), 'id', function ($model) {
221
                return Yii::t('hipanel:server:hub', $model->label);
222
            });
223
224
            return $result;
225
        }, 86400 * 24); // 24 days
226
227
        return $result;
228
    }
229
}
230