Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
handlerservice.php
1<?php
2
4
12
13if (!Main\Loader::includeModule('rest'))
14{
15 return;
16}
17
23{
24 private const ERROR_HANDLER_ALREADY_EXISTS = 'ERROR_HANDLER_ALREADY_EXIST';
25 private const ERROR_HANDLER_NOT_FOUND = 'ERROR_HANDLER_NOT_FOUND';
26 private const ERROR_HANDLER_UPDATE = 'ERROR_HANDLER_UPDATE';
27 private const ERROR_HANDLER_DELETE = 'ERROR_HANDLER_DELETE';
28
33 private static function checkParamsBeforeAddHandler($params)
34 {
35 if (empty($params['CODE']))
36 {
37 throw new RestException('Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
38 }
39
40 if (empty($params['NAME']))
41 {
42 throw new RestException('Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
43 }
44
45 if (empty($params['SETTINGS']) || !is_array($params['SETTINGS']))
46 {
47 throw new RestException('Parameter SETTINGS is not defined or empty', self::ERROR_CHECK_FAILURE);
48 }
49
50 self::checkHandlerSettingsBeforeAdd($params['SETTINGS']);
51
53 'filter' => [
54 '=CODE' => $params['CODE']
55 ]
56 ])->fetch();
57 if ($handler)
58 {
59 throw new RestException('Handler already exists!', self::ERROR_HANDLER_ALREADY_EXISTS);
60 }
61 }
62
68 private static function checkParamsBeforeUpdateHandler($params)
69 {
70 if (empty($params['ID']))
71 {
72 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
73 }
74
76 'filter' => [
77 'ID' => $params['ID']
78 ]
79 ])->fetch();
80 if (!$handler)
81 {
82 throw new RestException('Handler not found', self::ERROR_HANDLER_NOT_FOUND);
83 }
84
85 if ($params['APP_ID'] && !empty($handler['APP_ID']) && $handler['APP_ID'] !== $params['APP_ID'])
86 {
87 throw new AccessException();
88 }
89
90 if (empty($params['FIELDS']) || !is_array($params['FIELDS']))
91 {
92 throw new RestException('Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
93 }
94
95 if (isset($params['FIELDS']['SETTINGS']))
96 {
97 self::checkHandlerSettingsBeforeUpdate($params['FIELDS']['SETTINGS']);
98 }
99 }
100
106 private static function checkParamsBeforeDeleteHandler($params)
107 {
108 if (empty($params['ID']))
109 {
110 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
111 }
112
114 'filter' => [
115 'ID' => $params['ID']
116 ]
117 ])->fetch();
118 if (!$handler)
119 {
120 throw new RestException('Handler not found', self::ERROR_HANDLER_NOT_FOUND);
121 }
122
123 if ($params['APP_ID'] && !empty($handler['APP_ID']) && $handler['APP_ID'] !== $params['APP_ID'])
124 {
125 throw new AccessException();
126 }
127
128 $cashboxListResult = Manager::getList([
129 'select' => ['ID', 'HANDLER', 'SETTINGS'],
130 'filter' => [
131 '=HANDLER' => '\\'.CashboxRest::class,
132 ],
133 ]);
134
135 $cashboxIdList = [];
136 while ($cashbox = $cashboxListResult->fetch())
137 {
138 if ($cashbox['SETTINGS']['REST']['REST_CODE'] === $handler['CODE'])
139 {
140 $cashboxIdList[] = $cashbox['ID'];
141 }
142 }
143
144 if ($cashboxIdList)
145 {
146 throw new RestException(
147 'There are cashboxes with this handler: '.implode(', ', $cashboxIdList),
148 self::ERROR_CHECK_FAILURE
149 );
150 }
151 }
152
156 private static function checkHandlerSettingsBeforeAdd($settings): void
157 {
158 self::checkRequiredSettingsFields($settings, ['PRINT_URL', 'CHECK_URL', 'CONFIG']);
159 self::checkSettingsFieldValues($settings, ['HTTP_VERSION', 'CONFIG']);
160 }
161
165 private static function checkHandlerSettingsBeforeUpdate($settings): void
166 {
167 self::checkSettingsFieldValues($settings, ['PRINT_URL', 'CHECK_URL', 'CONFIG', 'HTTP_VERSION']);
168 }
169
175 private static function checkRequiredSettingsFields(array $settings, array $requiredFields): void
176 {
177 foreach ($requiredFields as $fieldName)
178 {
179 if (empty($settings[$fieldName]))
180 {
181 throw new RestException('Parameter SETTINGS[' . $fieldName . '] is not defined', self::ERROR_CHECK_FAILURE);
182 }
183 }
184 }
185
191 private static function checkSettingsFieldValues(array $settings, array $fields): void
192 {
193 foreach ($fields as $fieldName)
194 {
195 if ($fieldName === 'HTTP_VERSION' && array_key_exists('HTTP_VERSION', $settings))
196 {
197 $version = $settings['HTTP_VERSION'];
198 if (
199 $version !== Main\Web\HttpClient::HTTP_1_0
200 && $version !== Main\Web\HttpClient::HTTP_1_1
201 )
202 {
203 throw new RestException('The value of SETTINGS[HTTP_VERSION] is not valid', self::ERROR_CHECK_FAILURE);
204 }
205 }
206 elseif ($fieldName === 'CONFIG' && array_key_exists('CONFIG', $settings))
207 {
208 self::checkSettingsConfig($settings['CONFIG']);
209 }
210 elseif (array_key_exists($fieldName, $settings) && empty($settings[$fieldName]))
211 {
212 throw new RestException('The value of SETTINGS[' . $fieldName . '] is not valid', self::ERROR_CHECK_FAILURE);
213 }
214 }
215 }
216
221 private static function checkSettingsConfig(array $config): void
222 {
223 foreach ($config as $group => $block)
224 {
225 foreach ($block['ITEMS'] as $code => $item)
226 {
227 try
228 {
229 \Bitrix\Sale\Internals\Input\Manager::getEditHtml('SETTINGS['.$group.']['.$code.']', $item);
230 }
231 catch (\Exception $exception)
232 {
233 throw new RestException('The config provided in SETTINGS[CONFIG] is not valid', self::ERROR_CHECK_FAILURE);
234 }
235 }
236 }
237 }
238
244 private static function mergeHandlerSettings($cashboxId, array $newSettings): array
245 {
246 $dbResult = $existingSettings = CashboxRestHandlerTable::getList([
247 'select' => ['SETTINGS'],
248 'filter' => ['=ID' => $cashboxId],
249 'limit' => 1,
250 ])->fetch();
251
252 if (!$dbResult)
253 {
254 return $newSettings;
255 }
256
257 $existingSettings = $dbResult['SETTINGS'];
258 if (!$existingSettings)
259 {
260 return $newSettings;
261 }
262
263 return array_replace_recursive($existingSettings, $newSettings);
264 }
265
271 public static function addHandler($params, $page, \CRestServer $server)
272 {
273 Helpers\Rest\AccessChecker::checkAccessPermission();
274 $params = self::prepareHandlerParams($params, $server);
275 self::checkParamsBeforeAddHandler($params);
276
277 if (!isset($params['SETTINGS']['SUPPORTS_FFD105']))
278 {
279 $params['SETTINGS']['SUPPORTS_FFD105'] = 'N';
280 }
281
283 'NAME' => $params['NAME'],
284 'CODE' => $params['CODE'],
285 'SORT' => $params['SORT'] ?: 100,
286 'SETTINGS' => $params['SETTINGS'],
287 'APP_ID' => $params['APP_ID'],
288 ]);
289
290 if ($result->isSuccess())
291 {
292 return $result->getId();
293 }
294
295 return false;
296 }
297
305 public static function updateHandler($params, $page, \CRestServer $server)
306 {
307 Helpers\Rest\AccessChecker::checkAccessPermission();
308 $params = self::prepareHandlerParams($params, $server);
309 self::checkParamsBeforeUpdateHandler($params);
310
311 $handlerFields = $params['FIELDS'];
312 if ($handlerFields['SETTINGS'])
313 {
314 $handlerFields['SETTINGS'] = self::mergeHandlerSettings($params['ID'], $handlerFields['SETTINGS']);
315 }
316
317 $result = CashboxRestHandlerTable::update($params['ID'], $handlerFields);
318 if ($result->isSuccess())
319 {
320 return true;
321 }
322
323 $errors = implode("\n", $result->getErrorMessages());
324 throw new RestException($errors, self::ERROR_HANDLER_UPDATE);
325 }
326
332 public static function deleteHandler($params, $page, \CRestServer $server)
333 {
334 Helpers\Rest\AccessChecker::checkAccessPermission();
335 $params = self::prepareHandlerParams($params, $server);
336 self::checkParamsBeforeDeleteHandler($params);
337
338 $result = CashboxRestHandlerTable::delete($params['ID']);
339 if ($result->isSuccess())
340 {
341 return true;
342 }
343
344 $errors = implode("\n", $result->getErrorMessages());
345 throw new RestException($errors, self::ERROR_HANDLER_DELETE);
346 }
347
351 public static function getHandlerList($params, $page, \CRestServer $server)
352 {
353 Helpers\Rest\AccessChecker::checkAccessPermission();
354
355 $result = [];
356
358 'select' => ['ID', 'NAME', 'CODE', 'SORT', 'SETTINGS'],
359 ]);
360 while ($item = $dbRes->fetch())
361 {
362 $result[] = $item;
363 }
364
365 return $result;
366 }
367}
static includeModule($moduleName)
Definition loader.php:69
static getList(array $parameters=array())
static update($primary, array $data)
static getList(array $parameters=[])
Definition manager.php:135
static updateHandler($params, $page, \CRestServer $server)
static addHandler($params, $page, \CRestServer $server)
static getHandlerList($params, $page, \CRestServer $server)
static deleteHandler($params, $page, \CRestServer $server)
static prepareHandlerParams($data, \CRestServer $server)