collectPropertyFieldsFromDB()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Arrilot\BitrixMigrations\Autocreate\Handlers;
4
5
use Arrilot\BitrixMigrations\Exceptions\SkipHandlerException;
6
use CIBlockProperty;
7
use CIBlockPropertyEnum;
8
9
class OnBeforeIBlockPropertyUpdate extends BaseHandler implements HandlerInterface
10
{
11
    /**
12
     * Old property fields from DB.
13
     *
14
     * @var array
15
     */
16
    protected $dbFields;
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param array $params
22
     *
23
     * @throws SkipHandlerException
24
     */
25
    public function __construct($params)
26
    {
27
        $this->fields = $params[0];
28
29
        $this->dbFields = $this->collectPropertyFieldsFromDB();
30
31
        if (!$this->propertyHasChanged() || !$this->fields['IBLOCK_ID']) {
32
            throw new SkipHandlerException();
33
        }
34
    }
35
36
    /**
37
     * Get migration name.
38
     *
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return "auto_update_iblock_element_property_{$this->fields['CODE']}_in_ib_{$this->fields['IBLOCK_ID']}";
44
    }
45
46
    /**
47
     * Get template name.
48
     *
49
     * @return string
50
     */
51
    public function getTemplate()
52
    {
53
        return 'auto_update_iblock_element_property';
54
    }
55
56
    /**
57
     * Get array of placeholders to replace.
58
     *
59
     * @return array
60
     */
61 View Code Duplication
    public function getReplace()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        return [
64
            'fields'   => var_export($this->fields, true),
65
            'iblockId' => $this->fields['IBLOCK_ID'],
66
            'code'     => "'".$this->fields['CODE']."'",
67
        ];
68
    }
69
70
    /**
71
     * Collect property fields from DB and convert them to format that can be compared from user input.
72
     *
73
     * @return array
74
     */
75
    protected function collectPropertyFieldsFromDB()
76
    {
77
        $fields = CIBlockProperty::getByID($this->fields['ID'])->fetch();
78
        $fields['VALUES'] = [];
79
80
        $filter = [
81
            'IBLOCK_ID'   => $this->fields['IBLOCK_ID'],
82
            'PROPERTY_ID' => $this->fields['ID'],
83
        ];
84
        $sort = [
85
            'SORT'  => 'ASC',
86
            'VALUE' => 'ASC',
87
        ];
88
89
        $propertyEnums = CIBlockPropertyEnum::GetList($sort, $filter);
90
        while ($v = $propertyEnums->GetNext()) {
91
            $fields['VALUES'][$v['ID']] = [
92
                'ID'     => $v['ID'],
93
                'VALUE'  => $v['VALUE'],
94
                'SORT'   => $v['SORT'],
95
                'XML_ID' => $v['XML_ID'],
96
                'DEF'    => $v['DEF'],
97
            ];
98
        }
99
100
        return $fields;
101
    }
102
103
    /**
104
     * Determine if property has been changed.
105
     *
106
     * @return bool
107
     */
108
    protected function propertyHasChanged()
109
    {
110
        foreach ($this->dbFields as $field => $value) {
111
            if (isset($this->fields[$field]) && ($this->fields[$field] != $value)) {
112
                return true;
113
            }
114
        }
115
116
        return false;
117
    }
118
}
119