GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WarehousesRemains   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 51
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 40 6
1
<?php
2
3
namespace app\backend\widgets;
4
5
use devgroup\TagDependencyHelper\ActiveRecordHelper;
6
use Yii;
7
use app;
8
use yii\base\InvalidConfigException;
9
use yii\base\Widget;
10
use yii\caching\TagDependency;
11
use app\modules\shop\models\WarehouseProduct;
12
use app\modules\shop\models\Warehouse;
13
14
15
/**
16
 * Widget WarehousesRemains renders input block for specifying product remains on each of active warehouse
17
 * @package app\backend\widgets
18
 */
19
class WarehousesRemains extends Widget
20
{
21
    /**
22
     * @var app\modules\shop\models\Product Product model
23
     */
24
    public $model = null;
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function run()
30
    {
31
        if ($this->model === null) {
32
            throw new InvalidConfigException("Model should be set for WarehousesRemains widget");
33
        }
34
35
        $state = $this->model->getWarehousesState();
36
37
        $activeWarehousesIds = Warehouse::activeWarehousesIds();
38
        $remains = [];
39
        foreach ($state as $remain) {
40
            $remains[$remain->warehouse_id] = $remain;
41
            if(($key = array_search($remain->warehouse_id, $activeWarehousesIds)) !== false) {
42
                unset($activeWarehousesIds[$key]);
43
            }
44
        }
45
46
        // if we have new warehouses that not represented in warehouses state
47
        if (count($activeWarehousesIds) > 0) {
48
            foreach ($activeWarehousesIds as $id) {
49
                // create new record with default values
50
                $remain = new WarehouseProduct();
51
                $remain->warehouse_id = $id;
52
                $remain->product_id = $this->model->id;
53
                $remain->save();
54
55
                // add to remains
56
                $remains[$remain->warehouse_id] = $remain;
57
            }
58
            TagDependency::invalidate(Yii::$app->cache, ActiveRecordHelper::getObjectTag($this->model->className(), $this->model->id));
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
        }
60
61
        return $this->render(
62
            'warehouses-remains',
63
            [
64
                'model' => $this->model,
65
                'remains' => $remains,
66
            ]
67
        );
68
    }
69
}