ServerController::actions()   F
last analyzed

Complexity

Conditions 33
Paths 1

Size

Total Lines 680
Code Lines 475

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 155
CRAP Score 205.8154

Importance

Changes 0
Metric Value
eloc 475
dl 0
loc 680
ccs 155
cts 338
cp 0.4586
rs 3.3333
c 0
b 0
f 0
cc 33
nc 1
nop 0
crap 205.8154

How to fix   Long Method    Complexity   

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
 * 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\ComboSearchAction;
15
use hipanel\actions\IndexAction;
16
use hipanel\actions\PrepareBulkAction;
17
use hipanel\actions\ProxyAction;
18
use hipanel\actions\RedirectAction;
19
use hipanel\actions\RenderAction;
20
use hipanel\actions\RenderJsonAction;
21
use hipanel\actions\RequestStateAction;
22
use hipanel\actions\SmartCreateAction;
23
use hipanel\actions\SmartDeleteAction;
24
use hipanel\actions\SmartPerformAction;
25
use hipanel\actions\SmartUpdateAction;
26
use hipanel\actions\ValidateFormAction;
27
use hipanel\actions\ViewAction;
28
use hipanel\base\CrudController;
29
use hipanel\filters\EasyAccessControl;
30
use hipanel\models\Ref;
31
use hipanel\modules\finance\models\Tariff;
32
use hipanel\modules\server\cart\ServerRenewProduct;
33
use hipanel\modules\server\forms\AssignHubsForm;
34
use hipanel\modules\server\forms\ServerForm;
35
use hipanel\modules\server\helpers\ServerHelper;
36
use hipanel\modules\server\models\HardwareSettings;
37
use hipanel\modules\server\models\MonitoringSettings;
38
use hipanel\modules\server\models\Osimage;
39
use hipanel\modules\server\models\query\ServerQuery;
40
use hipanel\modules\server\models\Server;
41
use hipanel\modules\server\models\ServerUseSearch;
42
use hipanel\modules\server\models\SoftwareSettings;
43
use hipanel\modules\server\widgets\ResourceConsumption;
44
use hiqdev\hiart\Collection;
45
use hiqdev\hiart\ResponseErrorException;
46
use hiqdev\yii2\cart\actions\AddToCartAction;
47
use Yii;
48
use yii\base\Event;
49
use yii\filters\VerbFilter;
50
use yii\helpers\ArrayHelper;
51
use yii\web\NotFoundHttpException;
52
use yii\web\Response;
53
54
class ServerController extends CrudController
55
{
56
    public function behaviors()
57
    {
58
        return array_merge(parent::behaviors(), [
59
            'server-actions-verb' => [
60
                'class' => VerbFilter::class,
61
                'actions' => [
62
                    'reboot' => ['post'],
63
                    'reset' => ['post'],
64
                    'shutdown' => ['post'],
65
                    'power-off' => ['post'],
66
                    'power-on' => ['post'],
67
                    'reset-password' => ['post'],
68
                    'enable-block' => ['post'],
69
                    'disable-block' => ['post'],
70
                    'refuse' => ['post'],
71
                    'flush-switch-graphs' => ['post'],
72
                ],
73
            ],
74
            [
75
                'class' => EasyAccessControl::class,
76
                'actions' => [
77
                    'monitoring-settings' => 'support',
78
                    'software-settings' => 'support',
79
                    'hardware-settings' => 'support',
80
                    'create' => 'server.create',
81
                    'update' => 'server.update',
82
                    'delete' => 'server.delete',
83
                    'renew' => 'server.pay',
84
                    'refuse' => 'server.pay',
85
                    'assign-hubs' => 'server.update',
86
                    'set-units' => 'server.update',
87
                    'set-rack-no' => 'server.update',
88
                    'reboot' => 'server.control-power',
89
                    'shutdown' => 'server.control-power',
90
                    'power-on' => 'server.control-power',
91
                    'power-off' => 'server.control-power',
92
                    'reset' => 'server.control-power',
93
                    'reinstall' => 'server.control-system',
94
                    'boot-live' => 'server.control-system',
95
                    'enable-wizzard' => 'server.update',
96
                    'disable-wizzard' => 'server.wizzard',
97
                    'enable-vnc' => 'server.control-system',
98
                    'enable-block' => 'server.enable-block',
99
                    'disable-block' => 'server.disable-block',
100
                    'set-label' => 'server.set-label',
101
                    'set-note' => 'server.set-note',
102
                    'clear-resources' => 'consumption.delete',
103
                    'flush-switch-graphs' => 'consumption.delete',
104
                    '*' => 'server.read',
105
                ],
106
            ],
107
        ]);
108
    }
109
110 1
    public function actions()
111
    {
112 1
        return array_merge(parent::actions(), [
113 1
            'index' => [
114
                'class' => IndexAction::class,
115
                'findOptions' => ['with_requests' => true, 'with_discounts' => true],
116 1
                'on beforePerform' => function (Event $event) {
117
                    /** @var \hipanel\actions\SearchAction $action */
118
                    $action = $event->sender;
119
                    $dataProvider = $action->getDataProvider();
120
121
                    $dataProvider->query->withBindings();
122
123
                    if (Yii::getAlias('@ip', false)) {
124
                        $dataProvider->query
125
                            ->joinWith(['ips'])
126
                            ->andWhere(['with_ips' => 1]);
127
                    }
128
129
                    if ($this->indexPageUiOptionsModel->representation === 'billing' && Yii::$app->user->can('consumption.read')) {
0 ignored issues
show
Bug introduced by Andrey Klochok
The method can() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
                    if ($this->indexPageUiOptionsModel->representation === 'billing' && Yii::$app->user->/** @scrutinizer ignore-call */ can('consumption.read')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
                        $dataProvider->query->withConsumptions()->withHardwareSales();
131
                    }
132
133
                    $dataProvider->query
134
                        ->andWhere(['with_requests' => 1])
135
                        ->andWhere(['with_discounts' => 1])
136
                        ->select(['*']);
137 1
                },
138
                'filterStorageMap' => [
139
                    'name_dc'    => 'server.server.name_dc',
140
                    'name_like'  => 'server.server.name',
141
                    'label_like' => 'server.server.label',
142
                    'note_like'  => 'server.server.note',
143
                    'order_no'   => 'server.server.order_no',
144
                    'dc_like'    => 'server.server.dc',
145
                    'ip_like'    => 'server.server.ip',
146
147
                    'ips'       => 'hosting.ip.ip_in',
148
                    'client_id' => 'client.client.id',
149
                    'seller_id' => 'client.client.seller_id',
150
151
                    'hwsummary_like' => 'server.server.hwsummary',
152
                    'type'           => 'server.server.type',
153 1
                    'state'          => 'server.server.state',
154 1
                    'net_like'       => 'server.server.net',
155
                    'kvm_like'       => 'server.server.kvm',
156 1
                    'pdu_like'       => 'server.server.pdu',
157
                    'rack_like'      => 'server.server.rack',
158
                    'mac_like'       => 'server.server.mac',
159
160
                    'tariff_like'  => 'server.server.tariff',
161
                    'wizzarded_eq' => 'server.server.wizzarded',
162 1
                ],
163 1
            ],
164
            'search' => [
165 1
                'class' => ComboSearchAction::class,
166
            ],
167
            'create' => [
168
                'class' => SmartCreateAction::class,
169
                'collection' => [
170
                    'class' => Collection::class,
171
                    'model' => new ServerForm(['scenario' => 'create']),
172 1
                    'scenario' => 'create',
173 1
                ],
174
                'success' => Yii::t('hipanel:server', 'Server has been created'),
175
            ],
176
            'update' => [
177
                'class' => SmartUpdateAction::class,
178
                'collection' => [
179
                    'class' => Collection::class,
180
                    'model' => new ServerForm(),
181 1
                    'scenario' => 'update',
182 1
                ],
183
                'on beforeFetch' => function (Event $event) {
184
                    /** @var \hipanel\actions\SearchAction $action */
185
                    $action = $event->sender;
186 1
                    $dataProvider = $action->getDataProvider();
187 1
                    if (Yii::getAlias('@ip', false)) {
188 1
                        $dataProvider->query->joinWith('ips');
189
                    }
190
                },
191
                'data' => function (Action $action, array $data) {
192
                    $result = [];
193 1
                    foreach ($data['models'] as $model) {
194
                        $result['models'][] = ServerForm::fromServer($model);
195
                    }
196 1
                    $result['model'] = reset($result['models']);
197 1
198
                    return $result;
199 1
                },
200
                'success' => Yii::t('hipanel:server', 'Server has been updated'),
201
            ],
202
            'assign-hubs' => [
203
                'class' => SmartUpdateAction::class,
204
                'success' => Yii::t('hipanel:server', 'Hubs were assigned'),
205
                'view' => 'assignHubs',
206
                'on beforeFetch' => function (Event $event) {
207
                    /** @var \hipanel\actions\SearchAction $action */
208
                    $action = $event->sender;
209
                    $dataProvider = $action->getDataProvider();
210
                    $dataProvider->query->withBindings()->select(['*']);
211
                },
212 1
                'collection' => [
213
                    'class' => Collection::class,
214
                    'model' => new AssignHubsForm(),
215
                    'scenario' => 'default',
216 1
                ],
217 1
                'data' => function (Action $action, array $data) {
218 1
                    $result = [];
219
                    foreach ($data['models'] as $model) {
220
                        if ($model->canAssignHubs()) {
221
                            $result['models'][] = AssignHubsForm::fromServer($model);
222
                        }
223
                    }
224
                    if (!$result['models']) {
225
                        throw new NotFoundHttpException('There are no entries available for the selected operation. The type of selected records may not be suitable for the selected operation.');
226
                    }
227 1
                    $result['model'] = reset($result['models']);
228 1
229
                    return $result;
230
                },
231
            ],
232
            'set-units' => [
233
                'class' => SmartUpdateAction::class,
234 1
                'success' => Yii::t('hipanel:server', 'Units property was changed'),
235 1
                'view' => 'setUnits',
236
                'on beforeSave' => function (Event $event) {
237
                    /** @var \hipanel\actions\Action $action */
238
                    $action = $event->sender;
239
                    $servers = Yii::$app->request->post('HardwareSettings');
240 1
                    $units = ArrayHelper::remove($servers, 'units');
241
                    foreach ($servers as $id => $server) {
242
                        $servers[$id]['units'] = $units;
243
                    }
244 1
                    $action->collection->load($servers);
245 1
                },
246
                'on beforeFetch' => function (Event $event) {
247
                    /** @var \hipanel\actions\SearchAction $action */
248 1
                    $action = $event->sender;
249 1
                    /** @var ServerQuery $query */
250
                    $query = $action->getDataProvider()->query;
251 1
                    $query->withHardwareSettings();
252
                },
253
                'on beforeLoad' => function (Event $event) {
254
                    /** @var Action $action */
255
                    $action = $event->sender;
256
257
                    $action->collection->setModel((new HardwareSettings(['scenario' => 'set-units'])));
258
                },
259
            ],
260
            'set-rack-no' => [
261
                'class' => SmartUpdateAction::class,
262 1
                'success' => Yii::t('hipanel:server', 'Rack No. was assigned'),
263 1
                'view' => 'setRackNo',
264
                'collection' => [
265
                    'class' => Collection::class,
266
                    'model' => new AssignHubsForm(),
267
                    'scenario' => 'default',
268 1
                ],
269 1
                'on beforeSave' => function (Event $event) {
270
                    /** @var \hipanel\actions\Action $action */
271
                    $action = $event->sender;
272
                    $servers = Yii::$app->request->post('AssignHubsForm');
273
                    $rackId = ArrayHelper::remove($servers, 'rack_id');
274
                    $rackPort = ArrayHelper::remove($servers, 'rack_port');
275
                    foreach ($servers as $id => $server) {
276
                        $servers[$id]['rack_id'] = $rackId;
277
                        $servers[$id]['rack_port'] = $rackPort;
278
                    }
279
                    $action->collection->load($servers);
280
                },
281
                'on beforeFetch' => function (Event $event) {
282 1
                    /** @var \hipanel\actions\SearchAction $action */
283
                    $action = $event->sender;
284
                    $dataProvider = $action->getDataProvider();
285
                    $dataProvider->query->withBindings()->select(['*']);
286 1
                },
287 1
                'data' => function (Action $action, array $data) {
288 1
                    $result = [];
289
                    foreach ($data['models'] as $model) {
290
                        if ($model->canAssignHubs()) {
291
                            $result['models'][] = AssignHubsForm::fromServer($model);
292
                        }
293
                    }
294 1
                    if (!$result['models']) {
295 1
                        throw new NotFoundHttpException('There are no entries available for the selected operation. The type of selected records may not be suitable for the selected operation.');
296
                    }
297
                    $result['model'] = reset($result['models']);
298
299
                    return $result;
300 1
                },
301
            ],
302
            'hardware-settings' => [
303
                'class' => SmartUpdateAction::class,
304
                'success' => Yii::t('hipanel:server', 'Hardware properties was changed'),
305 1
                'view' => 'hardwareSettings',
306
                'on beforeFetch' => function (Event $event) {
307
                    /** @var \hipanel\actions\SearchAction $action */
308
                    $action = $event->sender;
309 1
                    /** @var ServerQuery $query */
310
                    $query = $action->getDataProvider()->query;
311
                    $query->withHardwareSettings();
312
                },
313
                'on beforeLoad' => function (Event $event) {
314
                    /** @var Action $action */
315 1
                    $action = $event->sender;
316 1
317 1
                    $action->collection->setModel(HardwareSettings::class);
0 ignored issues
show
Bug introduced by tofid
hipanel\modules\server\m...HardwareSettings::class of type string is incompatible with the type array|hiqdev\hiart\ActiveRecord expected by parameter $model of hiqdev\hiart\Collection::setModel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

317
                    $action->collection->setModel(/** @scrutinizer ignore-type */ HardwareSettings::class);
Loading history...
318 1
                },
319
                'POST html' => [
320
                    'save' => true,
321
                    'success' => [
322
                        'class' => RedirectAction::class,
323
                        'url' => function () {
324 1
                            $server = Yii::$app->request->post('HardwareSettings');
325 1
326
                            return ['@server/view', 'id' => $server['id']];
327
                        },
328
                    ],
329
                ],
330 1
            ],
331
            'software-settings' => [
332
                'class' => SmartUpdateAction::class,
333
                'success' => Yii::t('hipanel:server', 'Software properties was changed'),
334
                'view' => 'softwareSettings',
335 1
                'scenario' => 'default',
336
                'on beforeFetch' => function (Event $event) {
337
                    /** @var \hipanel\actions\SearchAction $action */
338
                    $action = $event->sender;
339 1
                    /** @var ServerQuery $query */
340
                    $query = $action->getDataProvider()->query;
341
                    $query->withSoftwareSettings();
342
                },
343
                'on beforeLoad' => function (Event $event) {
344
                    /** @var Action $action */
345 1
                    $action = $event->sender;
346 1
347 1
                    $action->collection->setModel(SoftwareSettings::class);
0 ignored issues
show
Bug introduced by tofid
hipanel\modules\server\m...SoftwareSettings::class of type string is incompatible with the type array|hiqdev\hiart\ActiveRecord expected by parameter $model of hiqdev\hiart\Collection::setModel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

347
                    $action->collection->setModel(/** @scrutinizer ignore-type */ SoftwareSettings::class);
Loading history...
348 1
                },
349
                'POST html' => [
350
                    'save' => true,
351
                    'success' => [
352
                        'class' => RedirectAction::class,
353
                        'url' => function () {
354
                            $server = Yii::$app->request->post('SoftwareSettings');
355 1
356 1
                            return ['@server/view', 'id' => $server['id']];
357
                        },
358
                    ],
359
                ],
360
            ],
361 1
            'monitoring-settings' => [
362 1
                'class' => SmartUpdateAction::class,
363
                'success' => Yii::t('hipanel:server', 'Monitoring properties was changed'),
364
                'view' => 'monitoringSettings',
365
                'scenario' => 'default',
366 1
                'on beforeFetch' => function (Event $event) {
367
                    /** @var \hipanel\actions\SearchAction $action */
368
                    $action = $event->sender;
369
                    $query = $action->getDataProvider()->query;
370
                    $query->withMonitoringSettings()->select(['*']);
371 1
                },
372
                'on beforeLoad' => function (Event $event) {
373
                    /** @var Action $action */
374
                    $action = $event->sender;
375 1
376
                    $action->collection->setModel(MonitoringSettings::class);
0 ignored issues
show
Bug introduced by tofid
hipanel\modules\server\m...nitoringSettings::class of type string is incompatible with the type array|hiqdev\hiart\ActiveRecord expected by parameter $model of hiqdev\hiart\Collection::setModel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

376
                    $action->collection->setModel(/** @scrutinizer ignore-type */ MonitoringSettings::class);
Loading history...
377
                },
378
                'data' => function ($action) {
379
                    return [
380
                        'nicMediaOptions' => $action->controller->getFullFromRef('type,nic_media'),
381 1
                    ];
382
                },
383
                'POST html' => [
384
                    'save' => true,
385
                    'success' => [
386
                        'class' => RedirectAction::class,
387
                        'url' => function () {
388
                            $server = Yii::$app->request->post('MonitoringSettings');
389
390
                            return ['@server/view', 'id' => $server['id']];
391
                        },
392
                    ],
393
                ],
394
            ],
395
            'view' => [
396
                'class' => ViewAction::class,
397
                'on beforePerform' => function (Event $event) {
398
                    /** @var \hipanel\actions\SearchAction $action */
399
                    $action = $event->sender;
400
                    /** @var ServerQuery $query */
401
                    $query = $action->getDataProvider()->query;
402
                    $query
403
                        ->withSoftwareSettings()
404
                        ->withHardwareSettings()
405
                        ->withBindings()
406
                        ->withBlocking()
407 1
                        ->withUses()
408 1
                        ->withConsumptions()
409
                        ->joinWith(['switches']);
410
411
                    if (Yii::getAlias('@ip', false)) {
412
                        $query
413
                            ->joinWith(['ips'])
414
                            ->andWhere(['with_ips' => 1]);
415
                    }
416
417
                    // TODO: ipModule is not wise yet. Redo
418
                    $query
419
                        ->andWhere(['with_requests' => 1])
420
                        ->andWhere(['show_deleted' => 1])
421
                        ->andWhere(['with_discounts' => 1])
422
                        ->select(['*']);
423
                },
424
                'data' => function ($action) {
425
                    /**
426
                     * @var Action
427
                     * @var self $controller
428
                     * @var Server $model
429
                     */
430
                    $controller = $action->controller;
431
                    $model = $action->getModel();
432
                    $model->vnc = $controller->getVNCInfo($model);
433
434
                    $panels = $controller->getPanelTypes();
435
436
                    $cacheKeys = [__METHOD__, 'view', 'tariff', $model->tariff_id, Yii::$app->user->getId()];
437
                    $tariff = Yii::$app->cache->getOrSet($cacheKeys, function () use ($model) {
438
                        return Tariff::find()->where([
439
                            'id' => $model->tariff_id,
440
                            'show_final' => true,
441
                            'show_deleted' => true,
442
                            'with_resources' => true,
443
                        ])->joinWith('resources')->one();
444
                    });
445
446
                    $ispSupported = false;
447
                    if ($tariff !== null) {
448
                        foreach ($tariff->getResources() as $resource) {
449
                            if ($resource->type === 'isp' && $resource->quantity > 0) {
450
                                $ispSupported = true;
451
                            }
452
                        }
453
                    }
454
455
                    $osimages = $controller->getOsimages($model);
456 1
                    $groupedOsimages = ServerHelper::groupOsimages($osimages, $ispSupported);
457
458
                    if ($model->isLiveCDSupported()) {
459
                        $osimageslivecd = $controller->getOsimagesLiveCd();
460 1
                    }
461 1
462
                    $blockReasons = $controller->getBlockReasons();
463
464
                    return compact([
465
                        'model',
466 1
                        'osimages',
467 1
                        'osimageslivecd',
468
                        'groupedOsimages',
469
                        'panels',
470
                        'blockReasons',
471
                    ]);
472 1
                },
473
            ],
474
            'resources' => [
475
                'class' => ViewAction::class,
476
                'view' => 'resources',
477
                'on beforePerform' => function (Event $event) {
478
                    /** @var \hipanel\actions\SearchAction $action */
479
                    $action = $event->sender;
480 1
                    $dataProvider = $action->getDataProvider();
481 1
                    $dataProvider->query->withUses()->select(['*']);
482 1
                },
483
                'data' => function ($action) {
484
                    $model = $action->getModel();
485
                    list($chartsLabels, $chartsData) = $model->groupUsesForCharts();
486 1
487 1
                    return compact('model', 'chartsData', 'chartsLabels');
488 1
                }
489
            ],
490
            'requests-state' => [
491
                'class' => RequestStateAction::class,
492 1
                'model' => Server::class,
493 1
            ],
494
            'set-note' => [
495
                'class' => SmartUpdateAction::class,
496
                'view' => 'modal/_bulkSetNote',
497
                'success' => Yii::t('hipanel:server', 'Note changed'),
498
                'error' => Yii::t('hipanel:server', 'Failed to change note'),
499
            ],
500
            'set-label' => [
501
                'class' => SmartUpdateAction::class,
502
                'view' => 'modal/_bulkSetLabel',
503
                'success' => Yii::t('hipanel:server', 'Internal note changed'),
504
                'error' => Yii::t('hipanel:server', 'Failed to change internal note'),
505 1
            ],
506
            'set-lock' => [
507
                'class' => RenderAction::class,
508 1
                'success' => Yii::t('hipanel:server', 'Record was changed'),
509
                'error' => Yii::t('hipanel:server', 'Error occurred'),
510
                'POST pjax' => [
511
                    'save' => true,
512
                    'success' => [
513
                        'class' => ProxyAction::class,
514 1
                        'action' => 'index',
515 1
                    ],
516
                ],
517
                'POST' => [
518
                    'save' => true,
519
                    'success' => [
520
                        'class' => RenderJsonAction::class,
521
                        'return' => function ($action) {
522 1
                            /** @var \hipanel\actions\Action $action */
523
                            return $action->collection->models;
524
                        },
525
                    ],
526 1
                ],
527 1
            ],
528 1
            'enable-vnc' => [
529
                'class' => ViewAction::class,
530
                'view' => '_vnc',
531
                'data' => function ($action) {
532
                    $model = $action->getModel();
533
                    if ($model->canEnableVNC()) {
534
                        $model->vnc = $this->getVNCInfo($model, true);
535
                    }
536 1
537
                    return [];
538
                },
539
            ],
540
            'bulk-sale' => [
541
                'class' => SmartUpdateAction::class,
542
                'scenario' => 'sale',
543
                'view' => 'modal/_bulkSale',
544
                'success' => Yii::t('hipanel:server', 'Servers were sold'),
545
                'POST pjax' => [
546
                    'save' => true,
547
                    'success' => [
548
                        'class' => ProxyAction::class,
549
                        'action' => 'index',
550
                    ],
551
                ],
552
                'on beforeSave' => function (Event $event) {
553
                    /** @var \hipanel\actions\Action $action */
554
                    $action = $event->sender;
555 1
                    $request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by SilverFire - Dmitry Naumenko
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...
556
557
                    if ($request->isPost) {
558
                        $values = [];
559 1
                        foreach (['client_id', 'tariff_id', 'sale_time', 'move_accounts'] as $attribute) {
560 1
                            $value = $request->post($attribute);
561 1
                            if (!empty($value)) {
562 1
                                $values[$attribute] = $value;
563 1
                            }
564
                        }
565
                        foreach ($action->collection->models as $model) {
566
                            foreach ($values as $attr => $value) {
567
                                $model->setAttribute($attr, $value);
568
                            }
569
                        }
570
                    }
571
                },
572
            ],
573 1
            'set-one-type' => [
574
                'class' => SmartUpdateAction::class,
575
                'view' => 'setOneType',
576
                'success' => Yii::t('hipanel:server', 'Type was changed'),
577 1
                'error' => Yii::t('hipanel:server', 'Failed to change type'),
578 1
                'scenario' => 'set-type',
579 1
                'on beforeSave' => function (Event $event) {
580
                    /** @var \hipanel\actions\Action $action */
581
                    $action = $event->sender;
582
                    $servers = Yii::$app->request->post('Server');
583 1
                    $type = ArrayHelper::remove($servers, 'type');
584 1
                    foreach ($servers as $id => $server) {
585
                        $servers[$id]['type'] = $type;
586
                    }
587
                    $action->collection->setModel($this->newModel(['scenario' => 'set-type']));
588 1
                    $action->collection->load($servers);
589 1
                },
590
            ],
591
            'set-type' => [
592
                'class' => SmartUpdateAction::class,
593 1
                'view' => '_bulkSetType',
594 1
                'success' => Yii::t('hipanel:server', 'Type was changed'),
595
                'error' => Yii::t('hipanel:server', 'Failed to change type'),
596
            ],
597
            'reboot' => [
598 1
                'class' => SmartPerformAction::class,
599 1
                'success' => Yii::t('hipanel:server', 'Reboot task has been successfully added to queue'),
600
                'error' => Yii::t('hipanel:server', 'Error during the rebooting'),
601
            ],
602
            'reset' => [
603 1
                'class' => SmartPerformAction::class,
604 1
                'success' => Yii::t('hipanel:server', 'Reset task has been successfully added to queue'),
605
                'error' => Yii::t('hipanel:server', 'Error during the resetting'),
606
            ],
607
            'shutdown' => [
608 1
                'class' => SmartPerformAction::class,
609 1
                'success' => Yii::t('hipanel:server', 'Shutdown task has been successfully added to queue'),
610
                'error' => Yii::t('hipanel:server', 'Error during the shutting down'),
611
            ],
612
            'power-off' => [
613 1
                'class' => SmartPerformAction::class,
614 1
                'success' => Yii::t('hipanel:server', 'Power off task has been successfully added to queue'),
615
                'error' => Yii::t('hipanel:server', 'Error during the turning power off'),
616
            ],
617
            'power-on' => [
618
                'class' => SmartPerformAction::class,
619
                'success' => Yii::t('hipanel:server', 'Power on task has been successfully added to queue'),
620
                'error' => Yii::t('hipanel:server', 'Error during the turning power on'),
621 1
            ],
622
            'reset-password' => [
623
                'class' => SmartPerformAction::class,
624
                'success' => Yii::t('hipanel:server', 'Root password reset task has been successfully added to queue'),
625
                'error' => Yii::t('hipanel:server', 'Error during the resetting root password'),
626
            ],
627
            'enable-block' => [
628
                'class' => SmartPerformAction::class,
629
                'success' => Yii::t('hipanel:server', 'Server was blocked successfully'),
630
                'error' => Yii::t('hipanel:server', 'Error during the server blocking'),
631
                'POST html' => [
632
                    'save' => true,
633
                    'success' => [
634 1
                        'class' => RedirectAction::class,
635
                    ],
636
                ],
637
                'on beforeSave' => function (Event $event) {
638 1
                    /** @var \hipanel\actions\Action $action */
639 1
                    $action = $event->sender;
640
                    $type = Yii::$app->request->post('type');
641
                    $comment = Yii::$app->request->post('comment');
642
                    if (!empty($type)) {
643 1
                        foreach ($action->collection->models as $model) {
644
                            $model->setAttributes([
645
                                'type' => $type,
646
                                'comment' => $comment,
647 1
                            ]);
648 1
                        }
649
                    }
650
                },
651
            ],
652
            'bulk-enable-block-modal' => [
653
                'class' => PrepareBulkAction::class,
654
                'view' => 'modal/_bulkEnableBlock',
655 1
                'data' => function ($action, $data) {
656
                    return array_merge($data, [
657
                        'blockReasons' => $this->getBlockReasons(),
658
                    ]);
659
                },
660
            ],
661
            'disable-block' => [
662
                'class' => SmartPerformAction::class,
663
                'success' => Yii::t('hipanel:server', 'Server was unblocked successfully'),
664
                'error' => Yii::t('hipanel:server', 'Error during the server unblocking'),
665
                'POST html' => [
666
                    'save' => true,
667
                    'success' => [
668 1
                        'class' => RedirectAction::class,
669
                    ],
670
                ],
671
                'on beforeSave' => function (Event $event) {
672 1
                    /** @var \hipanel\actions\Action $action */
673 1
                    $action = $event->sender;
674
                    $type = Yii::$app->request->post('type');
675
                    $comment = Yii::$app->request->post('comment');
676
                    if (!empty($type)) {
677 1
                        foreach ($action->collection->models as $model) {
678
                            $model->setAttributes([
679
                                'comment' => $comment,
680
                                'type' => $type,
681 1
                            ]);
682 1
                        }
683
                    }
684
                },
685
            ],
686 1
            'bulk-disable-block-modal' => [
687 1
                'class' => PrepareBulkAction::class,
688
                'view' => 'modal/_bulkDisableBlock',
689
                'data' => function ($action, $data) {
690
                    return array_merge($data, [
691 1
                        'blockReasons' => $this->getBlockReasons(),
692
                    ]);
693
                },
694
            ],
695
            'refuse' => [
696
                'class' => SmartPerformAction::class,
697
                'success' => Yii::t('hipanel:server', 'You have refused the service'),
698 1
                'error' => Yii::t('hipanel:server', 'Error during the refusing the service'),
699 1
            ],
700 1
            'enable-autorenewal' => [
701
                'class' => SmartUpdateAction::class,
702
                'success' => Yii::t('hipanel:server', 'Server renewal enabled successfully'),
703
                'error' => Yii::t('hipanel:server', 'Error during the renewing the service'),
704 1
            ],
705
            'reinstall' => [
706
                'class' => SmartUpdateAction::class,
707
                'on beforeSave' => function (Event $event) {
708
                    /** @var Action $action */
709
                    $action = $event->sender;
710 1
                    foreach ($action->collection->models as $model) {
711 1
                        $model->osimage = Yii::$app->request->post('osimage');
712 1
                        $model->panel = Yii::$app->request->post('panel');
713
                    }
714
                },
715
                'success' => Yii::t('hipanel:server', 'Server reinstalling task has been successfully added to queue'),
716
                'error' => Yii::t('hipanel:server', 'Error during the server reinstalling'),
717
            ],
718 1
            'boot-live' => [
719
                'class' => SmartPerformAction::class,
720
                'on beforeSave' => function (Event $event) {
721
                    /** @var Action $action */
722
                    $action = $event->sender;
723
                    foreach ($action->collection->models as $model) {
724
                        $model->osimage = Yii::$app->request->post('osimage');
725 1
                    }
726
                },
727
                'success' => Yii::t('hipanel:server', 'Live CD booting task has been successfully added to queue'),
728
                'error' => Yii::t('hipanel:server', 'Error during the booting live CD'),
729
            ],
730
            'validate-hw-form' => [
731
                'class' => ValidateFormAction::class,
732 1
                'collection' => [
733
                    'class' => Collection::class,
734
                    'model' => new HardwareSettings(),
735
                ],
736
            ],
737
            'validate-crud-form' => [
738
                'class' => ValidateFormAction::class,
739
                'collection' => [
740 1
                    'class' => Collection::class,
741
                    'model' => new ServerForm(),
742
                ],
743
            ],
744
            'validate-assign-hubs-form' => [
745
                'class' => ValidateFormAction::class,
746
                'collection' => [
747
                    'class' => Collection::class,
748 1
                    'model' => new AssignHubsForm(),
749 1
                ],
750
            ],
751
            'validate-form' => [
752
                'class' => ValidateFormAction::class,
753
            ],
754
            'buy' => [
755
                'class' => RedirectAction::class,
756
                'url' => Yii::$app->params['organization.url'],
757 1
            ],
758 1
            'add-to-cart-renewal' => [
759 1
                'class' => AddToCartAction::class,
760
                'productClass' => ServerRenewProduct::class,
761
            ],
762
            'delete' => [
763
                'class' => SmartDeleteAction::class,
764
                'success' => Yii::t('hipanel:server', 'Server was deleted successfully'),
765
                'error' => Yii::t('hipanel:server', 'Failed to delete server'),
766
            ],
767 1
            'bulk-delete-modal' => [
768 1
                'class' => PrepareBulkAction::class,
769 1
                'view' => 'modal/_bulkDelete',
770
            ],
771
            'clear-resources' => [
772
                'class' => SmartPerformAction::class,
773
                'view' => '_clearResources',
774
                'success' => Yii::t('hipanel:server', 'Servers resources were cleared successfully'),
775
                'error' => Yii::t('hipanel:server', 'Error occurred during server resources flushing'),
776
            ],
777
            'clear-resources-modal' => [
778
                'class' => PrepareBulkAction::class,
779
                'view' => '_clearResources',
780
            ],
781
            'flush-switch-graphs' => [
782
                'class' => SmartPerformAction::class,
783
                'view' => '_clearResources',
784
                'success' => Yii::t('hipanel:server', 'Switch graphs were flushed successfully'),
785
                'error' => Yii::t('hipanel:server', 'Error occurred during switch graphs flushing'),
786
            ],
787
            'flush-switch-graphs-modal' => [
788
                'class' => PrepareBulkAction::class,
789
                'view' => '_flushSwitchGraphs',
790
            ],
791
        ]);
792
    }
793
794
    /**
795
     * Gets info of VNC on the server.
796
     *
797
     * @param Server $model
798
     * @param bool $enable
799
     * @throws ResponseErrorException
800
     * @return array
801
     */
802
    public function getVNCInfo($model, $enable = false)
803
    {
804
        if ($enable) {
805
            try {
806
                $vnc = Server::perform('enable-VNC', ['id' => $model->id]);
807
                $vnc['endTime'] = time() + 28800;
808
                Yii::$app->cache->set([__METHOD__, $model->id, $model], $vnc, 28800);
809
                $vnc['enabled'] = true;
810
            } catch (ResponseErrorException $e) {
811
                if ($e->getMessage() !== 'vds_has_tasks') {
812
                    throw $e;
813
                }
814
            }
815
        } else {
816
            if ($model->statuses['serverEnableVNC'] !== null && strtotime('+8 hours', strtotime($model->statuses['serverEnableVNC'])) > time()) {
0 ignored issues
show
Bug Best Practice introduced by Yurii Myronchuk
The property statuses does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
817
                $vnc = Yii::$app->cache->getOrSet([__METHOD__, $model->id, $model], function () use ($model) {
818
                    return ArrayHelper::merge([
819
                        'endTime' => strtotime($model->statuses['serverEnableVNC']) + 28800,
0 ignored issues
show
Bug Best Practice introduced by Yurii Myronchuk
The property statuses does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
820
                    ], Server::perform('enable-VNC', ['id' => $model->id]));
821
                }, 28800);
822
            }
823
            $vnc['enabled'] = $model->statuses['serverEnableVNC'] === null ? false : strtotime('+8 hours', strtotime($model->statuses['serverEnableVNC'])) > time();
824
        }
825
826
        return $vnc;
827
    }
828
829
    public function actionDrawChart()
830
    {
831
        $post = Yii::$app->request->post();
832
        $types = array_merge(['server_traf', 'server_traf95'], array_keys(ResourceConsumption::types()));
833
        if (!in_array($post['type'], $types, true)) {
834
            throw new NotFoundHttpException();
835
        }
836
837
        $searchModel = new ServerUseSearch();
838
        $dataProvider = $searchModel->search([]);
839
        $dataProvider->pagination = false;
840
        $dataProvider->query->action('get-uses');
0 ignored issues
show
Bug introduced by SilverFire - Dmitry Naumenko
The method action() does not exist on yii\db\QueryInterface. It seems like you code against a sub-type of said class. However, the method does not exist in yii\db\ActiveQueryInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

840
        $dataProvider->query->/** @scrutinizer ignore-call */ 
841
                              action('get-uses');
Loading history...
841
        $dataProvider->query->andWhere($post);
842
        $models = $dataProvider->getModels();
843
844
        list($labels, $data) = ServerHelper::groupUsesForChart($models);
845
846
        return $this->renderAjax('_consumption', [
847
            'labels' => $labels,
848
            'data' => $data,
849
            'consumptionBase' => $post['type'],
850
        ]);
851
    }
852
853
    /**
854
     * Gets OS images.
855
     *
856
     * @param Server $model
857
     * @throws NotFoundHttpException
858
     * @return array
859
     */
860
    protected function getOsimages(Server $model = null)
861
    {
862
        if ($model !== null) {
863
            $type = $model->type;
0 ignored issues
show
Bug Best Practice introduced by SilverFire - Dmitry Naumenko
The property type does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
864
        } else {
865
            $type = null;
866
        }
867
868
        $models = ServerHelper::getOsimages($type);
869
870
        if ($models === null) {
871
            throw new NotFoundHttpException('The requested page does not exist.');
872
        }
873
874
        return $models;
875
    }
876
877
    protected function getOsimagesLiveCd()
878
    {
879
        $models = Yii::$app->cache->getOrSet([__METHOD__], function () {
880
            return Osimage::findAll(['livecd' => true]);
881
        }, 3600);
882
883
        if ($models !== null) {
884
            return $models;
885
        }
886
887
        throw new NotFoundHttpException('The requested page does not exist.');
888
    }
889
890
    protected function getPanelTypes()
891
    {
892
        return ServerHelper::getPanels();
893
    }
894
895
    public function actionIsOperable($id)
896
    {
897
        Yii::$app->response->format = Response::FORMAT_JSON;
898
899
        $result = ['id' => $id, 'result' => false];
900
901
        if ($server = Server::find()->where(['id' => $id])->one()) {
902
            $result['result'] = $server->isOperable();
903
        }
904
905
        return $result;
906
    }
907
908
    protected function getFullFromRef($gtype)
909
    {
910
        $callingMethod = debug_backtrace()[1]['function'];
911
        $result = Yii::$app->get('cache')->getOrSet([$callingMethod], function () use ($gtype) {
912
            $result = ArrayHelper::map(Ref::find()->where([
913
                'gtype' => $gtype,
914
                'select' => 'full',
915
            ])->all(), 'id', function ($model) {
916
                return Yii::t('hipanel:server:hub', $model->label);
917
            });
918
919
            return $result;
920
        }, 86400 * 24); // 24 days
921
922
        return $result;
923
    }
924
}
925