Completed
Push — master ( 4d2fcc...d1e17e )
by Klochok
03:39
created

UiOptionsBehavior::getModel()   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 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hipanel\behaviors;
4
5
use hipanel\models\IndexPageUiOptions;
6
use Yii;
7
use yii\base\Behavior;
8
use yii\base\InvalidConfigException;
9
use yii\web\Controller;
10
11
class UiOptionsBehavior extends Behavior
12
{
13
    /**
14
     * @var mixed
15
     */
16
    public $modelClass;
17
18
    /**
19
     * @var array
20
     */
21
    public $allowedRoutes = [];
22
23
    /**
24
     * @var IndexPageUiOptions
25
     */
26
    private $_model;
27
28
    public function init()
29
    {
30
        parent::init();
31
32
        foreach ($this->allowedRoutes as &$allowedRoute) {
33
            $allowedRoute = ltrim(Yii::getAlias($allowedRoute), '/');
34
        }
35
    }
36
37
    public function events()
38
    {
39
        return [Controller::EVENT_BEFORE_ACTION => 'ensureOptions'];
40
    }
41
42
    public function ensureOptions($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...
43
    {
44
        if ($this->isRouteAllowed($this->getRoute())) {
45
            $options = [];
46
            $params = Yii::$app->request->get();
47
            if ($params) {
48
                $model = $this->getModel();
49
                foreach ($params as $key => $value) {
50
                    if (in_array($key, array_keys($model->toArray()))) {
51
                        $options[$key] = $value;
52
                    }
53
                }
54
                $model->attributes = $options;
55
                if ($model->validate()) {
56
                    $this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray());
57
                } else {
58
                    $errors = $model->getErrors();
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
                }
60
            }
61
        }
62
    }
63
64
    protected function getModel()
65
    {
66
        if ($this->_model === null) {
67
            $this->_model = $this->findModel();
68
        }
69
70
        return $this->_model;
71
    }
72
73
    protected function findModel()
74
    {
75
        if (isset($this->modelClass['class']) && class_exists($this->modelClass['class'])) {
76
            return Yii::createObject($this->modelClass);
0 ignored issues
show
Documentation introduced by
$this->modelClass is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        } else {
78
            throw new InvalidConfigException('UiOptionsBehavior::$modelClass must contain `class` item');
79
        }
80
    }
81
82
    protected function isRouteAllowed($route)
83
    {
84
        return in_array($route, $this->allowedRoutes, true);
85
    }
86
87
    protected function getUiOptionsStorage()
88
    {
89
        return Yii::$app->get('uiOptionsStorage');
90
    }
91
92
    protected function getRoute()
93
    {
94
        return Yii::$app->request->pathInfo;
95
    }
96
}
97