Completed
Push — master ( 78c2e1...01635f )
by Andrii
19:30 queued 15:45
created

src/controllers/HubController.php (1 issue)

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