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.

DynamicSearchModel::search()   C
last analyzed

Complexity

Conditions 14
Paths 12

Size

Total Lines 93

Duplication

Lines 12
Ratio 12.9 %

Importance

Changes 0
Metric Value
dl 12
loc 93
rs 5.166
c 0
b 0
f 0
cc 14
nc 12
nop 1

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
namespace app\properties;
4
5
6
use app\models\BaseObject;
7
use app\models\Property;
8
use app\models\PropertyStaticValues;
9
use Yii;
10
use yii\base\DynamicModel;
11
use yii\data\ActiveDataProvider;
12
use yii\helpers\ArrayHelper;
13
14
class DynamicSearchModel extends DynamicModel
15
{
16
    /** @var \yii\db\ActiveRecord */
17
    private $baseModel = null;
18
    private $propertyGroups = null;
19
    /**
20
     * @param \yii\db\ActiveRecord $baseModel
21
     * @param array $config
22
     */
23
    public function __construct($baseModel, $propertyGroups, $config=[])
24
    {
25
        $this->baseModel = $baseModel;
26
        $this->propertyGroups = $propertyGroups;
27
28
        $attributes = $this->baseModel->activeAttributes();
29
        foreach ($this->propertyGroups as $groupId => $properties) {
30
            $attributes = ArrayHelper::merge($attributes, array_keys($properties));
31
        }
32
        parent::__construct($attributes, $config);
33
34
        $this->addRule($attributes, 'safe');
35
36
    }
37
38
    public function search($params)
39
    {
40
        /* @var $query \yii\db\ActiveQuery */
41
        $query = $this->baseModel->find();
42
43
        $table_inheritance_joined = false;
44
45
        $dataProvider = new ActiveDataProvider(
46
            [
47
                'query' => &$query,
48
                'pagination' => [
49
                    'pageSize' => 10,
50
                ],
51
                'sort' => [
52
                    'defaultOrder' => [
53
                        'id' => SORT_DESC
54
                    ]
55
                ],
56
            ]
57
        );
58
59
        if (!($this->load($params))) {
60
            return $dataProvider;
61
        }
62
63
        $object = BaseObject::getForClass($this->baseModel->className());
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...
64
        $baseModelTableName = $this->baseModel->tableName();
65
66
        $eavJoinsCount = 0;
67
        $osvJoinsCount = 0;
68
69
        foreach ($this->propertyGroups as $groupId => $properties) {
70
            foreach ($properties as $key => $propertyValue) {
71
                /** @var \app\properties\PropertyValue $propertyValue */
72
                $prop = Property::findById($propertyValue->property_id);
73
                if (empty($this->{$prop->key}) === true && $this->{$prop->key} !== '0') {
74
                    continue;
75
                }
76
77
                // determine property storage type and join needed table if needed
78
                if ($prop->is_column_type_stored) {
79
                    if ($table_inheritance_joined === false) {
80
                        $table_inheritance_joined = true;
81
                        $query->join('INNER JOIN', $object->column_properties_table_name . ' ti',
82
                            'ti.object_model_id = ' . $baseModelTableName . '.id');
83
                    }
84
85 View Code Duplication
                    if ($prop->value_type === 'STRING' && $prop->property_handler_id !== 3) {
86
                        $query->andFilterWhere(['like', 'ti.'.$prop->key, $this->{$prop->key}]);
87
                    } else {
88
                        $query->andFilterWhere(['ti.'.$prop->key => $this->{$prop->key}]);
89
                    }
90
91
                } elseif ($prop->is_eav) {
92
                    $eavJoinsCount++;
93
                    $eavTableName = 'eav'.$eavJoinsCount;
94
95
                    $key = 'key'.$eavJoinsCount;
96
97
                    $query->join(
98
                        'INNER JOIN',
99
                        "$object->eav_table_name $eavTableName",
100
                        $eavTableName.'.object_model_id = '.$baseModelTableName.".id AND $eavTableName.key=:$key",
101
                        [$key=>$prop->key]
102
                    );
103 View Code Duplication
                    if ($prop->value_type === 'STRING' && $prop->property_handler_id !== 3) {
104
                        $query->andFilterWhere(['like', $eavTableName.'.value', $this->{$prop->key}]);
105
                    } else {
106
                        // numeric - direct match
107
                        $query->andFilterWhere([$eavTableName.'.value' => $this->{$prop->key}]);
108
109
                    }
110
111
                } elseif ($prop->has_static_values) {
112
                    $osvJoinsCount++;
113
                    $osvTableName = 'osv'.$osvJoinsCount;
114
115
                    $query->join(
116
                        'INNER JOIN',
117
                        "object_static_values $osvTableName",
118
                        "$osvTableName.object_id={$object->id} AND $osvTableName.object_model_id=$baseModelTableName.id"
119
                    );
120
121
122
                    // numeric - direct match
123
                    $query->andFilterWhere(["$osvTableName.property_static_value_id", $this->{$prop->key}]);
124
125
                }
126
            }
127
        }
128
129
        return $dataProvider;
130
    }
131
132
133
    public function columns($baseModelColumns)
134
    {
135
        $columns = $baseModelColumns;
136
        foreach ($this->propertyGroups as $groupId => $properties) {
137
            foreach ($properties as $key => $propertyValue){
138
                /** @var PropertyValue $propertyValue */
139
                $prop = Property::findById($propertyValue->property_id);
140
141
                if ($prop->has_static_values) {
142
                    $column = [
143
                        'value' => function($model) use($prop){
144
                            return $model->property($prop->key);
145
                        },
146
                        'filter' => PropertyStaticValues::getSelectForPropertyId($propertyValue->property_id),
147
                    ];
148
                } elseif ($prop->property_handler_id === 3){
149
                    $column = [
150
                        'value' => function($model) use($prop){
151
                            return $model->property($prop->key);
152
                        },
153
                        'filter' => [
154
                            0 => Yii::t('app', 'No'),
155
                            1 => Yii::t('app', 'Yes'),
156
                        ],
157
                    ];
158
                } else {
159
                    $column = [
160
                        'value' => function($model) use($prop){
161
                            return $model->property($prop->key);
162
                        },
163
                    ];
164
                }
165
                $column = ArrayHelper::merge($column, [
166
                    'header' => $prop->name,
167
                    'attribute' => $prop->key,
168
                    'enableSorting' => true,
169
                ]);
170
                $columns[] = $column;
171
            }
172
        }
173
        return $columns;
174
    }
175
}