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.

AbstractModel::getPropertiesModels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace app\properties;
4
5
use app\models\BaseObject;
6
use app\models\ObjectStaticValues;
7
use app\models\Property;
8
use Yii;
9
use yii\base\Model;
10
use yii\db\ActiveRecord;
11
use yii\helpers\ArrayHelper;
12
13
class AbstractModel extends Model
14
{
15
    /**
16
     * @var PropertyValue[] $values_by_property_key
17
     * @var string $form_name
18
     * @var array $rules
19
     * @var Property[] $properties_models
20
     */
21
    private $values_by_property_key = [];
22
    private $form_name;
23
    private $properties_models = [];
24
    private $rules = [];
25
    private $ownerModel = null;
26
27
    /**
28
     * Changes to behavior of getter and PropertyValue->toVal() to array mode.
29
     * Useful in backend when you want to edit multiple-valued properties.
30
     * @var bool
31
     */
32
    private $arrayMode = false;
33
34
    public function __construct($config = [], $ownerModel = null)
35
    {
36
        parent::__construct($config);
37
        $this->ownerModel = $ownerModel;
38
    }
39
40
    /**
41
     * @param null $value
42
     * @return bool
43
     */
44
    public function setArrayMode($value = null) {
45
        $lastValue = $this->arrayMode;
46
47
        if (is_bool($value)) {
48
            $this->arrayMode = $value;
49
        }
50
51
        return $lastValue;
52
    }
53
54
    /**
55
     * @param $name
56
     */
57
    public function setFormName($name)
58
    {
59
        $this->form_name = $name;
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65
    public function formName()
66
    {
67
        return $this->form_name;
68
    }
69
70
    /**
71
     * @param $properties_models \app\models\Property[]
72
     */
73
    public function setPropertiesModels($properties_models)
74
    {
75
        $this->properties_models = $properties_models;
76
        $this->values_by_property_key = array_fill_keys(array_keys($properties_models), []);
77
    }
78
79
    /**
80
     * @return Property[]
81
     */
82
    public function getPropertiesModels()
83
    {
84
        return $this->properties_models;
85
    }
86
87
    public function rules()
88
    {
89
        return ArrayHelper::merge(
90
            [
91
                [array_keys($this->properties_models), 'safe'],
92
            ],
93
            $this->rules
94
        );
95
    }
96
97
    public function getRules()
98
    {
99
        return $this->rules;
100
    }
101
102
    public function addRules($rules)
103
    {
104
        $this->rules = ArrayHelper::merge($this->rules, $rules);
105
    }
106
107
    public function clearRules()
108
    {
109
        $this->rules = [];
110
    }
111
112
    public function scenarios()
113
    {
114
        $scenarios = parent::scenarios();
115
        $scenarios['readonly'] = [];
116
        return $scenarios;
117
    }
118
119
120
    public function attributeLabels()
121
    {
122
        $labels = [];
123
        foreach ($this->properties_models as $property) {
124
            $labels[$property->key] = $property->name;
125
        }
126
        return $labels;
127
    }
128
129
    public function __get($name)
130
    {
131
        if (isset($this->values_by_property_key[$name])) {
132
            return $this->values_by_property_key[$name]->toValue($this->arrayMode);
133
        }
134
        return parent::__get($name);
135
    }
136
137
    public function attributes()
138
    {
139
        return array_keys($this->values_by_property_key);
140
    }
141
142
    public function setAttributes($values, $safeOnly = true)
143
    {
144
        $this->values_by_property_key = $values;
145
    }
146
147
    public function setAttributesValues($values)
148
    {
149
        foreach ($this->values_by_property_key as $key => $value) {
150
            if (isset($values[$this->form_name][$key])) {
151
                if (is_array($values[$this->form_name][$key])) {
152
                    $this->values_by_property_key[$key]->values = [];
153
                    foreach ($this->values_by_property_key[$key]->values as $val) {
154
                        $this->values_by_property_key[$key]->values[] = ['value' => $val];
155
                    }
156
                } else {
157
                    $this->values_by_property_key[$key]->values = [['value' => $values[$this->form_name][$key]]];
158
                }
159
            }
160
        }
161
    }
162
163
    public function updateValues($new_values, $object_id, $object_model_id)
164
    {
165
        $column_type_updates = ['object_model_id' => $object_model_id];
166
        $osv_psv_ids = [];
167
168
        $new_eav_values = [];
169
        $eav_ids_to_delete = [];
170
171
        foreach ($new_values as $key => $values) {
172
            $property = Property::findById($values->property_id);
173
            if ($property->captcha == 1) {
174
                continue;
175
            }
176
177
            if (!isset($this->values_by_property_key[$key])) {
178
                // нужно добавить
179
                if ($property->is_column_type_stored) {
180
                    $column_type_updates[$key] = (string) $values;
181
                } elseif ($property->has_static_values) {
182
                    foreach ($values->values as $val) {
183
                        $osv_psv_ids[] = $val['value'];
184
                    }
185
                } elseif ($property->is_eav) {
186
                    $new_eav_values[$key] = $values;
187
                }
188
            } else {
189
                if ($property->is_column_type_stored) {
190
                    $column_type_updates[$key] = (string) $values;
191
                } elseif ($property->has_static_values) {
192
                    foreach ($values->values as $val) {
193
                        $osv_psv_ids[] = $val['value'];
194
                    }
195
                } elseif ($property->is_eav) {
196
                    // добавим новые
197
                    foreach ($values->values as $index => $val) {
198
                        $new_eav_values[] = [
199
                            $object_model_id,
200
                            $values->property_group_id,
201
                            $key,
202
                            $val['value'],
203
                            $val['sort_order'],
204
                        ];
205
                    }
206
207
                    // теперь добавим на удаление
208
                    foreach ($this->values_by_property_key[$key]->values as $old_val) {
209
                        if (isset($old_val['eav_id'])) {
210
                            $eav_ids_to_delete[] =  $old_val['eav_id'];
211
                        }
212
                    }
213
                }
214
            }
215
        }
216
        $osv_psv_ids_to_delete = [];
217
        foreach ($this->values_by_property_key as $key => $values) {
218
            $property = Property::findById($values->property_id);
219
            if (in_array($key, array_keys($new_values)) === false) {
220
                // if in incoming array there was no specification for this property - skip it
221
                continue;
222
            }
223
            if ($property->has_static_values) {
224
                foreach ($values->values as $val) {
225
                    if (in_array($val['psv_id'], $osv_psv_ids) === false) {
226
                        // в новых значениях нет
227
                        $osv_psv_ids_to_delete[] = $val['psv_id'];
228
                    } else {
229
                        // удалим, чтобы заново не добавлять
230
                        unset(
231
                            $osv_psv_ids[
232
                                array_search(
233
                                    $val['psv_id'],
234
                                    $osv_psv_ids
235
                                )
236
                            ]
237
                        );
238
                    }
239
                }
240
            }
241
        }
242
        if (count($osv_psv_ids_to_delete) > 0) {
243
            ObjectStaticValues::deleteAll(
244
                [
245
                    'and',
246
                    '`object_id` = :objectId',
247
                    [
248
                        'and',
249
                        '`object_model_id` = :objectModelId',
250
                        [
251
                            'in',
252
                            '`property_static_value_id`',
253
                            $osv_psv_ids_to_delete
254
                        ]
255
                    ]
256
                ],
257
                [
258
                    ':objectId' => $object_id,
259
                    ':objectModelId' => $object_model_id,
260
                ]
261
            );
262
        }
263
        if (count($osv_psv_ids) > 0) {
264
            $rows = [];
265
            foreach ($osv_psv_ids as $psv_id) {
266
                // 0 - Not Selected Field. Такие значения в базу не сохраняем
267
                if ($psv_id == 0) {
268
                    continue;
269
                }
270
                $rows[] = [
271
                    $object_id, $object_model_id, $psv_id,
272
                ];
273
            }
274 View Code Duplication
            if (!empty($rows)) {
275
                Yii::$app->db->createCommand()
276
                    ->batchInsert(
277
                        ObjectStaticValues::tableName(),
278
                        ['object_id', 'object_model_id', 'property_static_value_id'],
279
                        $rows
280
                    )->execute();
281
            }
282
        }
283
        Yii::$app->cache->delete("PSV:".$object_id.":".$object_model_id);
284
        if (count($column_type_updates) > 1) {
285
            $table_name = BaseObject::findById($object_id)->column_properties_table_name;
286
            $exists = Yii::$app->db->createCommand('select object_model_id from '.$table_name . ' where object_model_id=:object_model_id')
287
                ->bindValue(':object_model_id', $object_model_id)
288
                ->queryScalar();
289
            if ($exists) {
290
                Yii::$app->db->createCommand()
291
                    ->update(
292
                        $table_name,
293
                        $column_type_updates,
294
                        'object_model_id = :object_model_id',
295
                        [
296
                            ':object_model_id' => $object_model_id
297
                        ]
298
                    )->execute();
299
            } else {
300
                Yii::$app->db->createCommand()
301
                    ->insert(
302
                        $table_name,
303
                        $column_type_updates
304
                    )->execute();
305
            }
306
        }
307
        if (count($eav_ids_to_delete) > 0) {
308
            $table_name = BaseObject::findById($object_id)->eav_table_name;
309
            Yii::$app->db->createCommand()
310
                ->delete($table_name, ['in', 'id', $eav_ids_to_delete])
311
                ->execute();
312
        }
313
        if (count($new_eav_values) > 0) {
314
            $table_name = BaseObject::findById($object_id)->eav_table_name;
315
            Yii::$app->db->createCommand()
316
                ->batchInsert($table_name, ['object_model_id', 'property_group_id', 'key', 'value', 'sort_order'], $new_eav_values)
317
                ->execute();
318
        }
319
320
        Yii::$app->cache->delete("TIR:".$object_id . ':' .$object_model_id);
321
        $this->values_by_property_key = $new_values;
322
    }
323
324
    /**
325
     * @param $attribute
326
     * @return PropertyValue|null
327
     */
328
    public function getPropertyValueByAttribute($attribute)
329
    {
330
        if (isset($this->values_by_property_key[$attribute])) {
331
            return $this->values_by_property_key[$attribute];
332
        }
333
334
        return null;
335
    }
336
337
    /**
338
     * @return ActiveRecord|null
339
     */
340
    public function getOwnerModel()
341
    {
342
        return $this->ownerModel;
343
    }
344
}
345
?>