| Conditions | 29 |
| Paths | 7344 |
| Total Lines | 160 |
| Code Lines | 106 |
| Lines | 8 |
| Ratio | 5 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 = Object::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 = Object::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 = Object::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 | |||
| 345 | ?> |