Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseservice.php
1<?php
2
4
8
9if (!Main\Loader::includeModule('rest'))
10{
11 return;
12}
13
14class BaseService extends \IRestService
15{
16 protected const ALLOW_HANDLERS = [
17 '\\' . \Sale\Handlers\Delivery\RestHandler::class,
18 '\\' . \Sale\Handlers\Delivery\RestProfile::class,
19 ];
20
21 public static function onRestAppDelete(array $fields): void
22 {
23 if (!Main\Loader::includeModule('rest'))
24 {
25 return;
26 }
27
28 if (empty($fields['APP_ID']) || empty($fields['CLEAN']) || $fields['CLEAN'] !== true)
29 {
30 return;
31 }
32
33 $app = Rest\AppTable::getByClientId($fields['APP_ID']);
34 if (!$app)
35 {
36 return;
37 }
38
39 $restHandlerResult = Internals\DeliveryRestHandlerTable::getList([
40 'select' => ['ID', 'CODE'],
41 'filter' => [
42 '=APP_ID' => $app['CLIENT_ID'],
43 ],
44 ]);
45 while ($restHandler = $restHandlerResult->fetch())
46 {
47 $deliveryResult = Sale\Delivery\Services\Manager::getList([
48 'select' => ['ID', 'CONFIG'],
49 'filter' => [
50 '@CLASS_NAME' => self::ALLOW_HANDLERS,
51 ],
52 ]);
53 while ($delivery = $deliveryResult->fetch())
54 {
55 $handlerCode = self::getRestCodeFromConfig($delivery['CONFIG']);
56 if ($handlerCode === $restHandler['CODE'])
57 {
58 Sale\Delivery\Services\Manager::delete($delivery['ID'], false);
59 }
60 }
61
62 Sale\Delivery\Rest\Internals\DeliveryRestHandlerTable::delete($restHandler['ID']);
63 }
64 }
65
69 protected static function getIncomingFieldsMap(): array
70 {
71 return [];
72 }
73
77 protected static function getOutcomingFieldsMap(): array
78 {
79 return [];
80 }
81
82 protected static function checkDeliveryPermission(): void
83 {
84 Sale\Helpers\Rest\AccessChecker::checkAccessPermission();
85 }
86
91 protected static function prepareIncomingParams(array $data): array
92 {
93 return self::replaceIncomingKeys($data);
94 }
95
100 protected static function prepareOutcomingFields(array $data): array
101 {
102 return self::replaceOutcomingKeys($data);
103 }
104
109 private static function replaceIncomingKeys(array $data): array
110 {
111 return self::replaceKeys($data, static::getIncomingFieldsMap());
112 }
113
118 private static function replaceOutcomingKeys(array $data): array
119 {
120 return self::replaceKeys($data, static::getOutcomingFieldsMap());
121 }
122
128 private static function replaceKeys(array $data, array $map): array
129 {
130 foreach ($map as $key => $newKey)
131 {
132 if (array_key_exists($key, $data))
133 {
134 $data[$newKey] = $data[$key];
135 unset($data[$key]);
136 }
137
138 if (isset($data['FIELDS']) && array_key_exists($key, $data['FIELDS']))
139 {
140 $data['FIELDS'][$newKey] = $data['FIELDS'][$key];
141 unset($data['FIELDS'][$key]);
142 }
143 }
144
145 return $data;
146 }
147
148 protected static function hasAccessToDelivery(array $deliveryData, string $appId = null): bool
149 {
150 $className = $deliveryData['CLASS_NAME'];
151 if (self::isRestHandler($className))
152 {
153 $handlerCode = self::getRestCodeFromConfig($deliveryData['CONFIG']);
154 $handlerData = self::getHandlerData($handlerCode);
155 if ($appId && !empty($handlerData['APP_ID']) && $handlerData['APP_ID'] !== $appId)
156 {
157 return false;
158 }
159 }
160 else
161 {
162 return false;
163 }
164
165 return true;
166 }
167
168 protected static function isRestHandler(string $className): bool
169 {
170 return in_array($className, self::ALLOW_HANDLERS, true);
171 }
172
173 protected static function getRestCodeFromConfig(array $config): string
174 {
175 $handlerCode = '';
176
177 foreach ($config as $configItem)
178 {
179 if (!empty($configItem['REST_CODE']))
180 {
181 $handlerCode = (string)$configItem['REST_CODE'];
182 break;
183 }
184 }
185
186 return $handlerCode;
187 }
188
189 protected static function getHandlerData(string $code): ?array
190 {
191 static $result = [];
192
193 if (!empty($result[$code]))
194 {
195 return $result[$code];
196 }
197
198 $handlerData = Internals\DeliveryRestHandlerTable::getList([
199 'filter' => ['CODE' => $code],
200 'limit' => 1,
201 ])->fetch();
202 if ($handlerData)
203 {
204 $result[$code] = $handlerData;
205 }
206
207 return $result[$code] ?? null;
208 }
209}
static includeModule($moduleName)
Definition loader.php:69
static onRestAppDelete(array $fields)
static prepareIncomingParams(array $data)
static hasAccessToDelivery(array $deliveryData, string $appId=null)
static getRestCodeFromConfig(array $config)
static isRestHandler(string $className)
static prepareOutcomingFields(array $data)