Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
shopsettings.php
1<?php
2
4
7
8final class ShopSettings
9{
10 public static function getSettingsCoded(): array
11 {
12 return [
13 'ROBOXCHANGE_SHOPLOGIN',
14 'ROBOXCHANGE_SHOPPASSWORD',
15 'ROBOXCHANGE_SHOPPASSWORD2',
16 ];
17 }
18
25 public function add(array $settings): Main\Result
26 {
27 $result = new Main\Result();
28
29 $normalizedSettings = $this->normalizeSettings($settings);
30
31 foreach ($normalizedSettings as $settingCode => $settingValue)
32 {
33 $addResult = Sale\Internals\BusinessValueTable::add([
34 'CODE_KEY' => $settingCode,
35 'CONSUMER_KEY' => Sale\Internals\BusinessValueTable::COMMON_CONSUMER_KEY,
36 'PERSON_TYPE_ID' => Sale\Internals\BusinessValueTable::COMMON_PERSON_TYPE_ID,
37 'PROVIDER_KEY' => 'VALUE',
38 'PROVIDER_VALUE' => $settingValue,
39 ]);
40
41 if (!$addResult->isSuccess())
42 {
43 $result->addErrors($addResult->getErrors());
44 break;
45 }
46 }
47
48 if ($result->isSuccess())
49 {
50 $this->emitAddEvent();
51 }
52
53 return $result;
54 }
55
62 public function update(array $settings): Main\Result
63 {
64 $result = new Main\Result();
65
66 $normalizedSettings = $this->normalizeSettings($settings);
67
68 foreach ($normalizedSettings as $settingCode => $settingValue)
69 {
70 $primary = [
71 'CODE_KEY' => $settingCode,
72 'CONSUMER_KEY' => Sale\Internals\BusinessValueTable::COMMON_CONSUMER_KEY,
73 'PERSON_TYPE_ID' => Sale\Internals\BusinessValueTable::COMMON_PERSON_TYPE_ID,
74 ];
75
76 $updateResult = Sale\Internals\BusinessValueTable::update(
77 $primary,
78 [
79 'PROVIDER_VALUE' => $settingValue,
80 ]
81 );
82
83 if (!$updateResult->isSuccess())
84 {
85 $result->addErrors($updateResult->getErrors());
86 break;
87 }
88 }
89
90 return $result;
91 }
92
97 public function delete(): void
98 {
99 if (!$this->isOnlyCommonSettingsExists())
100 {
101 return;
102 }
103
104 $this->deleteInternal();
105 }
106
107 private function deleteInternal(): void
108 {
109 foreach (self::getSettingsCoded() as $settingsCode)
110 {
111 Sale\Internals\BusinessValueTable::deleteByCodeKey($settingsCode);
112 }
113 }
114
120 public function get(): array
121 {
122 $result = [];
123
124 if ($this->isOnlyCommonSettingsExists())
125 {
126 $businessValues = Sale\Internals\BusinessValueTable::getList([
127 'select' => ['CODE_KEY', 'PROVIDER_VALUE'],
128 'filter' => [
129 '@CODE_KEY' => self::getSettingsCoded(),
130 '=CONSUMER_KEY' => Sale\Internals\BusinessValueTable::COMMON_CONSUMER_KEY,
131 '=PERSON_TYPE_ID' => Sale\Internals\BusinessValueTable::COMMON_PERSON_TYPE_ID,
132 ]
133 ])->fetchAll();
134
135 foreach ($businessValues as $businessValue)
136 {
137 $result[$businessValue['CODE_KEY']] = $businessValue['PROVIDER_VALUE'];
138 }
139 }
140
141 return $result;
142 }
143
149 public function isOnlyCommonSettingsExists(): bool
150 {
151 $businessValues = Sale\Internals\BusinessValueTable::getList([
152 'select' => ['CONSUMER_KEY', 'PERSON_TYPE_ID'],
153 'filter' => [
154 '@CODE_KEY' => self::getSettingsCoded(),
155 ],
156 'group' => ['CONSUMER_KEY', 'PERSON_TYPE_ID'],
157 ])->fetchAll();
158
159 if (!$businessValues)
160 {
161 return false;
162 }
163
164 $otherSettings = array_filter($businessValues, static function ($businessValue) {
165 return
166 $businessValue['CONSUMER_KEY'] !== null
167 || $businessValue['PERSON_TYPE_ID'] !== null
168 ;
169 });
170
171 if ($otherSettings)
172 {
173 return false;
174 }
175
176 return true;
177 }
178
179 public function isAnySettingsExists(): bool
180 {
181 return (bool)Sale\Internals\BusinessValueTable::getList([
182 'select' => ['CODE_KEY'],
183 'filter' => [
184 '@CODE_KEY' => self::getSettingsCoded(),
185 ],
186 'limit' => 1
187 ])->fetch();
188 }
189
190 private function normalizeSettings(array $settings): array
191 {
192 $settingsCodeList = self::getSettingsCoded();
193
194 return array_filter(
195 $settings,
196 static function ($code) use ($settingsCodeList) {
197 return in_array($code, $settingsCodeList, true);
198 },
199 ARRAY_FILTER_USE_KEY
200 );
201 }
202
203 private function emitAddEvent(): void
204 {
205 if ($this->isPushAndPullAvailable())
206 {
207 $handlerClassName = \Sale\Handlers\PaySystem\RoboxchangeHandler::class;
208 $message = [
209 'module_id' => 'sale',
210 'command' => 'on_add_paysystem_settings_robokassa',
211 'params' => [
212 'handlerClassName' => $handlerClassName,
213 'handler' => Sale\PaySystem\Manager::getFolderFromClassName($handlerClassName),
214 ],
215 ];
216
217 \CPullWatch::AddToStack('SALE_PAYSYSTEM_ROBOKASSA_REGISTRATION', $message);
218 }
219 }
220
221 private function isPushAndPullAvailable(): bool
222 {
223 return Main\Loader::includeModule('pull');
224 }
225}