Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
handlerservice.php
1<?php
2
4
9
10if (!Main\Loader::includeModule('rest'))
11{
12 return;
13}
14
20{
21 private const ERROR_HANDLER_ADD = 'ERROR_HANDLER_ADD';
22 private const ERROR_HANDLER_UPDATE = 'ERROR_HANDLER_UPDATE';
23 private const ERROR_HANDLER_DELETE = 'ERROR_HANDLER_DELETE';
24 private const ERROR_CHECK_FAILURE = 'ERROR_CHECK_FAILURE';
25 private const ERROR_HANDLER_ALREADY_EXIST = 'ERROR_HANDLER_ALREADY_EXIST';
26 private const ERROR_HANDLER_NOT_FOUND = 'ERROR_HANDLER_NOT_FOUND';
27
33 private static function prepareHandlerParams(array $data, \CRestServer $server): array
34 {
35 $data = self::prepareIncomingParams($data);
36 $data['APP_ID'] = $server->getClientId();
37
38 return $data;
39 }
40
48 private static function checkParamsOnAddHandler(array $params): void
49 {
50 if (empty($params['NAME']))
51 {
52 throw new RestException('Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
53 }
54
55 if (empty($params['CODE']))
56 {
57 throw new RestException('Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
58 }
59
60 self::checkSettings($params['SETTINGS']);
61 self::checkProfiles($params['PROFILES']);
62
63 $deliveryRestHandler = Internals\DeliveryRestHandlerTable::getList([
64 'filter' => [
65 '=CODE' => $params['CODE']
66 ]
67 ])->fetch();
68 if ($deliveryRestHandler)
69 {
70 throw new RestException('Handler already exists!', self::ERROR_HANDLER_ALREADY_EXIST);
71 }
72 }
73
79 private static function checkParamsOnUpdateHandler(array $params): void
80 {
81 if (empty($params['ID']))
82 {
83 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
84 }
85
86 if (array_key_exists('NAME', $params) && empty($params['NAME']))
87 {
88 throw new RestException('Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
89 }
90
91 if (array_key_exists('CODE', $params) && empty($params['CODE']))
92 {
93 throw new RestException('Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
94 }
95
96 if (array_key_exists('SETTINGS', $params))
97 {
98 self::checkSettings($params['SETTINGS']);
99 }
100
101 if (array_key_exists('PROFILES', $params))
102 {
103 self::checkProfiles($params['PROFILES']);
104 }
105
106 $deliveryRestHandler = Internals\DeliveryRestHandlerTable::getList(array(
107 'filter' => array(
108 'ID' => $params['ID']
109 )
110 ))->fetch();
111 if (!$deliveryRestHandler)
112 {
113 throw new RestException('Handler not found', self::ERROR_HANDLER_NOT_FOUND);
114 }
115
116 if ($params['APP_ID'] && !empty($deliveryRestHandler['APP_ID']) && $deliveryRestHandler['APP_ID'] !== $params['APP_ID'])
117 {
118 throw new AccessException();
119 }
120 }
121
127 private static function checkParamsOnDeleteHandler($params): void
128 {
129 if (empty($params['ID']))
130 {
131 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
132 }
133
134 $deliveryRestHandler = Internals\DeliveryRestHandlerTable::getList(array(
135 'filter' => array(
136 'ID' => $params['ID']
137 )
138 ))->fetch();
139 if (!$deliveryRestHandler)
140 {
141 throw new RestException('Handler not found', self::ERROR_HANDLER_NOT_FOUND);
142 }
143
144 if ($params['APP_ID'] && !empty($deliveryRestHandler['APP_ID']) && $deliveryRestHandler['APP_ID'] !== $params['APP_ID'])
145 {
146 throw new AccessException();
147 }
148
149 $deliveryListResult = Sale\Delivery\Services\Manager::getList([
150 'select' => ['ID', 'CLASS_NAME', 'CONFIG'],
151 'filter' => [
152 '=CLASS_NAME' => '\\'.\Sale\Handlers\Delivery\RestHandler::class,
153 ],
154 ]);
155 $deliveryIdList = [];
156 while ($delivery = $deliveryListResult->fetch())
157 {
158 if ($delivery['CONFIG']['MAIN']['REST_CODE'] === $deliveryRestHandler['CODE'])
159 {
160 $deliveryIdList[] = $delivery['ID'];
161 }
162 }
163
164 if ($deliveryIdList)
165 {
166 throw new RestException(
167 'There are deliveries with this handler: '.implode(', ', $deliveryIdList),
168 self::ERROR_HANDLER_DELETE
169 );
170 }
171 }
172
180 public static function addHandler($query, $n, \CRestServer $server)
181 {
183 $params = self::prepareHandlerParams($query, $server);
184 self::checkParamsOnAddHandler($params);
185
186 if (isset($params['SETTINGS']['CONFIG']) && is_array($params['SETTINGS']['CONFIG']))
187 {
188 $params['SETTINGS']['CONFIG'] = [
189 'ITEMS' => self::convertArrayForSaving($params['SETTINGS']['CONFIG'], '[SETTINGS][CONFIG][]'),
190 ];
191 }
192
193 $data = [
194 'NAME' => $params['NAME'],
195 'CODE' => $params['CODE'],
196 'SORT' => $params['SORT'] ?: 100,
197 'DESCRIPTION' => $params['DESCRIPTION'],
198 'SETTINGS' => $params['SETTINGS'],
199 'PROFILES' => self::convertArrayForSaving($params['PROFILES'], '[PROFILES][]'),
200 'APP_ID' => $params['APP_ID'],
201 ];
202
203 $result = Internals\DeliveryRestHandlerTable::add($data);
204 if ($result->isSuccess())
205 {
206 return $result->getId();
207 }
208
209 $error = implode("\n", $result->getErrorMessages());
210 throw new RestException($error, self::ERROR_HANDLER_ADD);
211 }
212
220 public static function updateHandler($query, $n, \CRestServer $server): bool
221 {
223 $params = self::prepareHandlerParams($query, $server);
224
225 self::checkParamsOnUpdateHandler($params);
226
227 $fields = [];
228 foreach (['CODE', 'NAME', 'DESCRIPTION', 'SETTINGS', 'PROFILES'] as $field)
229 {
230 if (!array_key_exists($field, $params))
231 {
232 continue;
233 }
234
235 if ($field === 'PROFILES')
236 {
237 $value = self::convertArrayForSaving($params[$field], '[PROFILES][]');
238 }
239 elseif ($field === 'SETTINGS')
240 {
241 if (isset($params[$field]['CONFIG']) && is_array($params[$field]['CONFIG']))
242 {
243 $params[$field]['CONFIG'] = [
244 'ITEMS' => self::convertArrayForSaving($params[$field]['CONFIG'], '[SETTINGS][CONFIG][]'),
245 ];
246 }
247
248 $value = $params[$field];
249 }
250 else
251 {
252 $value = $params[$field];
253 }
254
255 $fields[$field] = $value;
256 }
257
258 $result = Internals\DeliveryRestHandlerTable::update($params['ID'], $fields);
259 if ($result->isSuccess())
260 {
261 return true;
262 }
263
264 $error = implode("\n", $result->getErrorMessages());
265 throw new RestException($error, self::ERROR_HANDLER_UPDATE);
266 }
267
275 public static function deleteHandler($query, $n, \CRestServer $server): bool
276 {
278 $params = self::prepareHandlerParams($query, $server);
279
280 self::checkParamsOnDeleteHandler($params);
281
282 $result = Internals\DeliveryRestHandlerTable::delete($params['ID']);
283 if ($result->isSuccess())
284 {
285 return true;
286 }
287
288 $error = implode("\n", $result->getErrorMessages());
289 throw new RestException($error, self::ERROR_HANDLER_DELETE);
290 }
291
298 public static function getHandlerList($query, $n, \CRestServer $server): array
299 {
301
302 $result = [];
303
304 $handlersList = array_values(Sale\Delivery\Services\Manager::getRestHandlerList());
305 foreach ($handlersList as $handler)
306 {
310 $profiles = [];
311 if (isset($handler['PROFILES']) && is_array($handler['PROFILES']))
312 {
313 $profiles = self::convertArrayForOutput($handler['PROFILES']);
314 }
315 $handler['PROFILES'] = $profiles;
316
320 $settings = $handler['SETTINGS'];
321 $config = [];
322 if (isset($settings['CONFIG']['ITEMS']) && is_array($settings['CONFIG']['ITEMS']))
323 {
324 $config = self::convertArrayForOutput($settings['CONFIG']['ITEMS']);
325 }
326 $settings['CONFIG'] = $config;
327 $handler['SETTINGS'] = $settings;
328
329 $result[] = $handler;
330 }
331
332 return $result;
333 }
334
339 private static function checkProfiles($profiles): void
340 {
341 if (empty($profiles) || !is_array($profiles))
342 {
343 throw new RestException('Parameter PROFILES is not defined', self::ERROR_CHECK_FAILURE);
344 }
345 }
346
351 private static function checkSettings($settings): void
352 {
353 if (empty($settings) || !is_array($settings))
354 {
355 throw new RestException('Parameter SETTINGS is not defined', self::ERROR_CHECK_FAILURE);
356 }
357
358 if (empty($settings['CALCULATE_URL']))
359 {
360 throw new RestException(
361 'Parameter SETTINGS[CALCULATE_URL] is not defined',
362 self::ERROR_CHECK_FAILURE
363 );
364 }
365 elseif (!is_string($settings['CALCULATE_URL']))
366 {
367 throw new RestException(
368 'Parameter SETTINGS[CALCULATE_URL] must be of string type',
369 self::ERROR_CHECK_FAILURE
370 );
371 }
372
373 if (
374 !empty($settings['CREATE_DELIVERY_REQUEST_URL'])
375 && !is_string($settings['CREATE_DELIVERY_REQUEST_URL'])
376 )
377 {
378 throw new RestException(
379 'Parameter SETTINGS[CREATE_DELIVERY_REQUEST_URL] must be of string type',
380 self::ERROR_CHECK_FAILURE
381 );
382 }
383
384 if (
385 !empty($settings['CANCEL_DELIVERY_REQUEST_URL'])
386 && !is_string($settings['CANCEL_DELIVERY_REQUEST_URL'])
387 )
388 {
389 throw new RestException(
390 'Parameter SETTINGS[CANCEL_DELIVERY_REQUEST_URL] must be of string type',
391 self::ERROR_CHECK_FAILURE
392 );
393 }
394
395 if (empty($settings['CONFIG']) || !is_array($settings['CONFIG']))
396 {
397 throw new RestException('Parameter SETTINGS[CONFIG] is not defined', self::ERROR_CHECK_FAILURE);
398 }
399 }
400
407 private static function convertArrayForSaving(array $items, string $path = ''): array
408 {
409 $result = [];
410
411 foreach ($items as $item)
412 {
413 if (!isset($item['CODE']))
414 {
415 throw new RestException(
416 ($path === '' ? '' : $path . ' ') . 'Item CODE is not specified',
417 self::ERROR_CHECK_FAILURE
418 );
419 }
420
421 $profileCode = $item['CODE'];
422 unset($item['CODE']);
423
424 $result[$profileCode] = $item;
425 }
426
427 return $result;
428 }
429
434 private static function convertArrayForOutput(array $items): array
435 {
436 $result = [];
437
438 foreach ($items as $profileCode => $item)
439 {
440 $item['CODE'] = $profileCode;
441
442 $result[] = $item;
443 }
444
445 return $result;
446 }
447}
static includeModule($moduleName)
Definition loader.php:69
static prepareIncomingParams(array $data)
static getHandlerList($query, $n, \CRestServer $server)
static addHandler($query, $n, \CRestServer $server)
static updateHandler($query, $n, \CRestServer $server)
static deleteHandler($query, $n, \CRestServer $server)