Completed
Push — master ( 3ba3ea...845ba8 )
by Dmitry
08:38
created

UiOptionsBehavior::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * HiPanel core package.
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\behaviors;
12
13
use hipanel\components\UiOptionsStorage;
14
use hipanel\grid\RepresentationCollectionFinder;
15
use hipanel\models\IndexPageUiOptions;
16
use hiqdev\higrid\representations\RepresentationCollectionInterface;
17
use Yii;
18
use yii\base\Behavior;
19
use yii\base\InvalidConfigException;
20
use yii\web\Controller;
21
22
class UiOptionsBehavior extends Behavior
23
{
24
    /**
25
     * @var mixed
26
     */
27
    public $modelClass;
28
29
    /**
30
     * @var IndexPageUiOptions
31
     */
32
    private $_model;
33
34
    /**
35
     * @var RepresentationCollectionInterface
36
     */
37
    public $representationsCollection;
38
39
    public function init()
40
    {
41
        parent::init();
42
43
        if ($this->representationsCollection === null) {
44
            $this->representationsCollection = RepresentationCollectionFinder::forCurrentRoute()->findOrFallback();
0 ignored issues
show
Documentation Bug introduced by
It seems like \hipanel\grid\Representa...ute()->findOrFallback() can also be of type object<hiqdev\higrid\rep...presentationCollection>. However, the property $representationsCollection is declared as type object<hiqdev\higrid\rep...ionCollectionInterface>. 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...
45
        }
46
    }
47
48
    public function events()
49
    {
50
        return [Controller::EVENT_BEFORE_ACTION => 'ensureUiOptions'];
51
    }
52
53
    public function ensureUiOptions($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        if (!$this->isRouteAllowed()) {
56
            return;
57
        }
58
59
        $options = [];
60
        $params = Yii::$app->request->get();
61
        $model = $this->getModel();
62
        $model->attributes = $this->getUiOptionsStorage()->get($this->getRoute());
63
        $model->availableRepresentations = $this->findRepresentations();
64
        if ($params) {
65
            foreach ($params as $key => $value) {
66
                if (in_array($key, array_keys($model->toArray()), true)) {
67
                    $options[$key] = $value;
68
                }
69
            }
70
            $model->attributes = $options;
71
72
            if ($model->validate()) {
73
                $this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray());
74
            } else {
75
                $errors = json_encode($model->getErrors());
76
                Yii::warning('UiOptionsBehavior - IndexPageUiModel validation errors: ' . $errors);
77
            }
78
        }
79
    }
80
81
    protected function getModel()
82
    {
83
        if ($this->_model === null) {
84
            $this->_model = $this->findModel();
85
        }
86
87
        return $this->_model;
88
    }
89
90
    protected function findModel()
91
    {
92
        return $this->owner->indexPageUiOptionsModel;
93
    }
94
95
    protected function isRouteAllowed()
96
    {
97
        return $this->owner->action->id === 'index';
98
    }
99
100
    /**
101
     * @return UiOptionsStorage
102
     */
103
    protected function getUiOptionsStorage()
104
    {
105
        return Yii::$app->get('uiOptionsStorage');
106
    }
107
108
    protected function getRoute()
109
    {
110
        return Yii::$app->request->pathInfo;
111
    }
112
113
    protected function findRepresentations()
114
    {
115
        return $this->representationsCollection->getAll();
116
    }
117
}
118