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.

PropertiesHelper::createSubQuery()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 3
nc 3
nop 4
1
<?php
2
3
namespace app\properties;
4
5
6
use app\models\ObjectStaticValues;
7
use app\models\Property;
8
use app\modules\shop\models\ConfigConfigurationModel;
9
use Yii;
10
use yii\db\ActiveQuery;
11
use yii\db\Expression;
12
use yii\db\Query;
13
14
class PropertiesHelper
15
{
16
17
    /**
18
     * Добавляет к запросу фильтры по свойствам
19
     * @param $object \app\models\BaseObject
20
     * @param $query ActiveQuery
21
     * @param $values_by_property_id array
22
     * @param array $dynamic_values_by_property_id array
23
     * @param string $multiFilterMode
24
     * @throws \Exception
25
     */
26
    public static function appendPropertiesFilters(
27
        $object,
28
        &$query,
29
        $values_by_property_id,
30
        $dynamic_values_by_property_id = [],
31
        $multiFilterMode = ConfigConfigurationModel::MULTI_FILTER_MODE_INTERSECTION
32
    )
33
    {
34
35
        // сначала сгруппируем свойства по типу хранения
36
        $by_storage = [
37
            'eav' => [],
38
            'table_inheritance' => [],
39
            'static_values' => [],
40
        ];
41
        $dynamic_by_storage = [
42
            'eav' => [],
43
            'table_inheritance' => [],
44
        ];
45
        foreach ($values_by_property_id as $property_id => $values) {
46
            // values может быть просто строкой(одно значение), а может уже прийти массивом
47
            $values = (array)$values;
48
            $property = Property::findById($property_id);
49
            if ($property === null) {
50
                continue;
51
            }
52
            if ($property->is_eav) {
53
                $by_storage['eav'][] = [
54
                    'property' => $property,
55
                    'values' => $values,
56
                ];
57
            } elseif ($property->is_column_type_stored) {
58
                $by_storage['table_inheritance'][] = [
59
                    'property' => $property,
60
                    'values' => $values,
61
                ];
62
            } elseif ($property->has_static_values) {
63
                switch ($multiFilterMode) {
64
                    case ConfigConfigurationModel::MULTI_FILTER_MODE_UNION:
65
                        $by_storage['static_values'][] = $values;
66
                        break;
67
                    default:
68
                        $by_storage['static_values'] = array_merge($by_storage['static_values'], $values);
69
                }
70
            } else {
71
                throw new \Exception("Wrong property type for " . $property->id);
72
            }
73
        }
74
        foreach ($dynamic_values_by_property_id as $property_id => $values) {
75
            $property = Property::findById($property_id);
76
            if ($property) {
77
                if ($property->is_eav) {
78
                    $dynamic_by_storage['eav'][] = [
79
                        'property' => $property,
80
                        'values' => $values,
81
                    ];
82
                } elseif ($property->is_column_type_stored) {
83
                    $dynamic_by_storage['table_inheritance'][] = [
84
                        'property' => $property,
85
                        'values' => $values,
86
                    ];
87
                } else {
88
                    throw new \Exception("Wrong property type for " . $property->id);
89
                }
90
            }
91
        }
92
93
        $ti_clauses = [];
94
        $join_table_name = "PropertiesJoinTable1";
95
96
        foreach ($by_storage['table_inheritance'] as $item) {
97
            $property = $item['property'];
98
            $or_clauses = [];
99
            foreach ($item['values'] as $val) {
100
                $or_clauses[] = "$join_table_name." .
101
                    Yii::$app->db->quoteColumnName($property->key) . " = " .
102
                    Yii::$app->db->quoteValue($val);
103
            }
104
105
            $ti_clauses[] = implode(" OR ", $or_clauses);
106
107
        }
108
        foreach ($dynamic_by_storage['table_inheritance'] as $item) {
109
            $property = $item['property'];
110
            $clauses = [];
111 View Code Duplication
            if (isset($item['values']['min']) && strlen($item['values']['min'])) {
112
                $clauses[] = "$join_table_name." .
113
                    Yii::$app->db->quoteColumnName($property->key) . " >= " .
114
                    Yii::$app->db->quoteValue((double)($item['values']['min']));
115
            }
116 View Code Duplication
            if (isset($item['values']['max']) && strlen($item['values']['max'])) {
117
                $clauses[] = "$join_table_name." .
118
                    Yii::$app->db->quoteColumnName($property->key) . " <= " .
119
                    Yii::$app->db->quoteValue((double)($item['values']['max']));
120
            }
121
            if (!empty($clauses)) {
122
                $ti_clauses[] = '(' . implode(" AND ", $clauses) . ')';
123
            }
124
        }
125
        if (count($ti_clauses) > 0) {
126
            $ti_clauses = implode(" AND ", $ti_clauses);
127
128
            $query = $query->innerJoin(
129
                $object->column_properties_table_name . " $join_table_name",
130
                "$join_table_name.object_model_id = " .
131
                Yii::$app->db->quoteTableName($object->object_table_name) . ".id " .
132
                " AND " . $ti_clauses
133
            );
134
        }
135
136
137
        if (count($by_storage['static_values'])) {
138
139
            switch ($multiFilterMode) {
140
                case ConfigConfigurationModel::MULTI_FILTER_MODE_UNION:
141
                    $subQuery = new Query();
142
                    $lastQuery = $subQuery;
143
                    $counter = 0;
144
                    foreach ($by_storage['static_values'] as $staticValues) {
145
                        $tmp = self::createSubQuery($object, $staticValues, ++$counter, $multiFilterMode);
146
                        $lastQuery->innerJoin(
147
                            ['osvm' . $counter => $tmp]
148
                            , 'osvm' . $counter . '.object_model_id = ' . 'osv' . ($counter - 1) . '.object_model_id'
149
                        );
150
                        $lastQuery = $tmp;
151
                    }
152
153
                    $query->innerJoin(
154
                        $subQuery->join[0][1]
155
                        , 'osvm1.object_model_id = ' . Yii::$app->db->quoteTableName($object->object_table_name) . '.id'
156
                    );
157
158
                    break;
159
                default:
160
                    $query->innerJoin(
161
                        ['osvm' => self::createSubQuery($object, $by_storage['static_values'])]
162
                        , 'osvm.object_model_id = ' . Yii::$app->db->quoteTableName($object->object_table_name) . '.id'
163
                    );
164
            }
165
        }
166
167
        if (count($by_storage['eav'])) {
168
            foreach ($by_storage['eav'] as $item) {
169
                $joinTableName = 'EAVJoinTable' . $item['property']->id;
170
                if (count($item['values']) > 0) {
171
                    $query = $query->innerJoin(
172
                        $object->eav_table_name . " " . $joinTableName,
173
                        "$joinTableName.object_model_id = " .
174
                        Yii::$app->db->quoteTableName($object->object_table_name) . ".id "
175
                    )->andWhere(
176
                        new Expression(
177
                            '`' . $joinTableName . '`.`value` in (' .
178
                            implode(', ', array_map('intval', $item['values'])) .
179
                            ') AND `' . $joinTableName . '`.`key` = "' . $item['property']->key . '"'
180
                        )
181
                    );
182
                }
183
            }
184
        }
185
186
187
    }
188
189
    /**
190
     * @param \app\models\BaseObject $object
191
     * @param array $psvs
192
     * @param int $counter
193
     * @param string $multiFilterMode
194
     * @return Query
195
     */
196
    public static function createSubQuery(
197
        \app\models\BaseObject $object,
198
        $psvs = [],
199
        $counter = 1,
200
        $multiFilterMode = ConfigConfigurationModel::MULTI_FILTER_MODE_INTERSECTION
201
    )
202
    {
203
        $tableInner = 'osv' . $counter;
204
        $psvsCount = count($psvs);
205
        switch ($multiFilterMode) {
206
            case ConfigConfigurationModel::MULTI_FILTER_MODE_UNION:
207
                if ($psvsCount > 1) {
208
                    $having = "count($tableInner.object_model_id) BETWEEN 1 AND $psvsCount";
209
                    break;
210
                }
211
                // no break in case of $psvsCount is 1 or 0
212
            default:
213
                $having = "count($tableInner.object_model_id) = $psvsCount";
214
        }
215
        $subQuery = (new Query)
216
            ->select("$tableInner.object_model_id")
217
            ->distinct()
218
            ->from(ObjectStaticValues::tableName() . " $tableInner")
219
            ->where([
220
                "$tableInner.object_id" => $object->id,
221
                "$tableInner.property_static_value_id" => $psvs
222
            ])
223
            ->groupBy("$tableInner.object_model_id")
224
            ->having($having);
225
226
        return $subQuery;
227
    }
228
}
229