IBlockPropertyEnum::setValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace Arrilot\BitrixMigrations\Constructors;
5
6
7
use Arrilot\BitrixMigrations\Logger;
8
use Bitrix\Main\Application;
9
10
class IBlockPropertyEnum
11
{
12
    use FieldConstructor;
13
14
    /**
15
     * Добавить значение списка
16
     * @throws \Exception
17
     */
18 View Code Duplication
    public function add()
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...
19
    {
20
        $obj = new \CIBlockPropertyEnum();
21
22
        $property_enum_id = $obj->Add($this->getFieldsWithDefault());
23
24
        if (!$property_enum_id) {
25
            throw new \Exception("Ошибка добавления значения enum");
26
        }
27
28
        Logger::log("Добавлено значение списка enum {$this->fields['VALUE']}", Logger::COLOR_GREEN);
29
30
        return $property_enum_id;
31
    }
32
33
    /**
34
     * Обновить свойство инфоблока
35
     * @param $id
36
     * @throws \Exception
37
     */
38 View Code Duplication
    public function update($id)
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...
39
    {
40
        $obj = new \CIBlockPropertyEnum();
41
        if (!$obj->Update($id, $this->fields)) {
42
            throw new \Exception("Ошибка обновления значения enum");
43
        }
44
45
        Logger::log("Обновлено значение списка enum {$id}", Logger::COLOR_GREEN);
46
    }
47
48
    /**
49
     * Удалить свойство инфоблока
50
     * @param $id
51
     * @throws \Exception
52
     */
53
    public static function delete($id)
54
    {
55
        if (!\CIBlockPropertyEnum::Delete($id)) {
56
            throw new \Exception('Ошибка при удалении значения enum');
57
        }
58
59
        Logger::log("Удалено значение списка enum {$id}", Logger::COLOR_GREEN);
60
    }
61
62
    /**
63
     * Установить настройки для добавления значения enum инфоблока по умолчанию
64
     * @param string $xml_id
65
     * @param string $value
66
     * @param int $propertyId
67
     * @return $this
68
     */
69
    public function constructDefault($xml_id, $value, $propertyId = null)
70
    {
71
         $this->setXmlId($xml_id)->setValue($value);
72
73
         if ($propertyId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $propertyId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
74
             $this->setPropertyId($propertyId);
75
         }
76
77
         return $this;
78
    }
79
80
    /**
81
     * Код свойства.
82
     * @param string $propertyId
83
     * @return $this
84
     */
85
    public function setPropertyId($propertyId)
86
    {
87
        $this->fields['PROPERTY_ID'] = $propertyId;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Внешний код.
94
     * @param string $xml_id
95
     * @return $this
96
     */
97
    public function setXmlId($xml_id)
98
    {
99
        $this->fields['XML_ID'] = $xml_id;
100
101
        return $this;
102
    }
103
104
    /**
105
     * Индекс сортировки.
106
     * @param int $sort
107
     * @return $this
108
     */
109
    public function setSort($sort = 500)
110
    {
111
        $this->fields['SORT'] = $sort;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Значение варианта свойства.
118
     * @param string $value
119
     * @return $this
120
     */
121
    public function setValue($value)
122
    {
123
        $this->fields['VALUE'] = $value;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Значение варианта свойства.
130
     * @param bool $def
131
     * @return $this
132
     */
133
    public function setDef($def)
134
    {
135
        $this->fields['DEF'] = $def ? 'Y' : 'N';
136
137
        return $this;
138
    }
139
}