Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
requestservice.php
1<?php
2
4
9
10if (!Main\Loader::includeModule('rest'))
11{
12 return;
13}
14
16{
17 private const ERROR_CODE_ADDRESSEE_IS_NOT_SPECIFIED = 'ADDRESSEE_IS_NOT_SPECIFIED';
18 private const ERROR_CODE_ADDRESSEE_UNEXPECTED_VALUE = 'ADDRESSEE_UNEXPECTED_VALUE';
19 private const ERROR_CODE_MESSAGE_NOT_SPECIFIED = 'MESSAGE_NOT_SPECIFIED';
20 private const ERROR_CODE_MESSAGE_STATUS_NOT_SPECIFIED = 'MESSAGE_STATUS_NOT_SPECIFIED';
21 private const ERROR_CODE_MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED = 'MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED';
22 private const ERROR_CODE_UNEXPECTED_MESSAGE_STATUS_SEMANTIC = 'UNEXPECTED_MESSAGE_STATUS_SEMANTIC';
23 private const ERROR_CODE_DELIVERY_ID_NOT_SPECIFIED = 'DELIVERY_ID_NOT_SPECIFIED';
24 private const ERROR_CODE_DELIVERY_NOT_FOUND = 'DELIVERY_NOT_FOUND';
25 private const ERROR_CODE_REQUEST_ID_NOT_SPECIFIED = 'REQUEST_ID_NOT_SPECIFIED';
26 private const ERROR_CODE_REQUEST_NOT_FOUND = 'REQUEST_NOT_FOUND';
27 private const ERROR_CODE_REQUEST_SHIPMENT_NOT_FOUND = 'REQUEST_SHIPMENT_NOT_FOUND';
28 private const ERROR_CODE_DELETE_REQUEST_INTERNAL_ERROR = 'DELETE_REQUEST_INTERNAL_ERROR';
29 private const ERROR_CODE_PROPERTIES_UNEXPECTED_FORMAT = 'PROPERTIES_UNEXPECTED_FORMAT';
30 private const ERROR_CODE_PROPERTY_VALUE_UNEXPECTED_FORMAT = 'PROPERTY_VALUE_UNEXPECTED_FORMAT';
31 private const ERROR_CODE_PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT = 'PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT';
32 private const ERROR_CODE_PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT = 'PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT';
33 private const ERROR_CODE_UNEXPECTED_REQUEST_FINALIZE_INDICATOR_VALUE = 'UNEXPECTED_REQUEST_FINALIZE_INDICATOR_VALUE';
34 private const ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE = 'UNEXPECTED_OVERWRITE_PROPERTIES_VALUE';
35 private const ERROR_CODE_EMPTY_UPDATE_PAYLOAD = 'EMPTY_UPDATE_PAYLOAD';
36 private const ERROR_CODE_UPDATE_REQUEST_INTERNAL_ERROR = 'UPDATE_REQUEST_INTERNAL_ERROR';
37 private const ERROR_CODE_STATUS_UNEXPECTED_FORMAT = 'STATUS_UNEXPECTED_FORMAT';
38 private const ERROR_CODE_STATUS_TEXT_NOT_SPECIFIED = 'STATUS_TEXT_NOT_SPECIFIED';
39 private const ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED = 'STATUS_SEMANTIC_NOT_SPECIFIED';
40 private const ERROR_CODE_DATE_VALUE_UNEXPECTED_FORMAT = 'DATE_VALUE_UNEXPECTED_FORMAT';
41
49 public static function updateRequest($query, $n, \CRestServer $server): bool
50 {
52 $params = self::prepareIncomingParams($query);
53
54 $requestId = self::getRequestId(
55 self::getDeliveryId($params, 'DELIVERY_ID'),
56 $params,
57 'REQUEST_ID'
58 );
59
60 $fields = [];
61
62 if (isset($params['FINALIZE']))
63 {
64 if (!in_array($params['FINALIZE'], ['Y', 'N'], true))
65 {
66 throw new RestException(
67 'Unexpected parameter FINALIZE value: Y, N expected',
68 self::ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE
69 );
70 }
71
72 if ($params['FINALIZE'] === 'Y')
73 {
74 $fields['STATUS'] = Requests\Manager::STATUS_PROCESSED;
75 }
76 }
77
78 $requestStatus = self::getRequestStatus($params, 'STATUS');
79 if (!is_null($requestStatus))
80 {
81 $fields['EXTERNAL_STATUS'] = $requestStatus['TEXT'];
82 $fields['EXTERNAL_STATUS_SEMANTIC'] = $requestStatus['SEMANTIC'];
83 }
84
85 $properties = self::getRequestProperties($params, 'PROPERTIES');
86 if (!is_null($properties))
87 {
88 $fields['EXTERNAL_PROPERTIES'] = $properties;
89 }
90
91 $overwriteProperties = false;
92 if (isset($params['OVERWRITE_PROPERTIES']))
93 {
94 if (!in_array($params['OVERWRITE_PROPERTIES'], ['Y', 'N'], true))
95 {
96 throw new RestException(
97 'Unexpected parameter OVERWRITE_PROPERTIES value: Y, N expected',
98 self::ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE
99 );
100 }
101 $overwriteProperties = $params['OVERWRITE_PROPERTIES'] === 'Y';
102 }
103
104 if (empty($fields))
105 {
106 throw new RestException(
107 'Empty update payload',
108 self::ERROR_CODE_EMPTY_UPDATE_PAYLOAD
109 );
110 }
111
112 $updateResult = Requests\Manager::updateDeliveryRequest(
113 $requestId,
114 $fields,
115 $overwriteProperties
116 );
117 if (!$updateResult->isSuccess())
118 {
119 throw new RestException('Internal error', self::ERROR_CODE_UPDATE_REQUEST_INTERNAL_ERROR);
120 }
121
122 return true;
123 }
124
131 private static function getRequestStatus(array $params, string $key): ?array
132 {
133 if (!isset($params[$key]))
134 {
135 return null;
136 }
137
138 if (!is_array($params[$key]))
139 {
140 throw new RestException(
141 sprintf('Unexpected status (%s) format: array expected', $key),
142 self::ERROR_CODE_STATUS_UNEXPECTED_FORMAT
143 );
144 }
145
146 if (empty($params[$key]['TEXT']))
147 {
148 throw new RestException(
149 'Status text has not been specified',
150 self::ERROR_CODE_STATUS_TEXT_NOT_SPECIFIED
151 );
152 }
153
154 if (empty($params[$key]['SEMANTIC']))
155 {
156 throw new RestException(
157 'Status semantic has not been specified',
158 self::ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED
159 );
160 }
161
162 if (!in_array($params[$key]['SEMANTIC'], Requests\Manager::getRequestStatusSemantics(), true))
163 {
164 throw new RestException(
165 sprintf('Unexpected request status semantic: %s', $params[$key]['SEMANTIC']),
166 self::ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED
167 );
168 }
169
170 return [
171 'TEXT' => $params[$key]['TEXT'],
172 'SEMANTIC' => $params[$key]['SEMANTIC'],
173 ];
174 }
175
182 private static function getRequestProperties(array $params, string $key): ?array
183 {
184 if (!isset($params[$key]))
185 {
186 return null;
187 }
188
189 if (!is_array($params[$key]))
190 {
191 throw new RestException(
192 sprintf('Unexpected properties (%s) format: array expected', $key),
193 self::ERROR_CODE_PROPERTIES_UNEXPECTED_FORMAT
194 );
195 }
196
197 $result = [];
198 foreach ($params[$key] as $propertyKey => $propertyValue)
199 {
200 $isExpectedFormat = (
201 is_array($propertyValue)
202 && isset($propertyValue['NAME'])
203 && is_string($propertyValue['NAME'])
204 && !empty($propertyValue['NAME'])
205 && isset($propertyValue['VALUE'])
206 && is_string($propertyValue['VALUE'])
207 );
208 if (!$isExpectedFormat)
209 {
210 throw new RestException(
211 sprintf('Unexpected property value (%s.%s) format', $key, $propertyKey),
212 self::ERROR_CODE_PROPERTY_VALUE_UNEXPECTED_FORMAT
213 );
214 }
215 $resultItem = [
216 'NAME' => $propertyValue['NAME'],
217 'VALUE' => $propertyValue['VALUE'],
218 ];
219
220 if (isset($propertyValue['TAGS']))
221 {
222 if (!is_array($propertyValue['TAGS']))
223 {
224 throw new RestException(
225 sprintf(
226 'Unexpected property value\'s tags format (%s.%s) format: array expected',
227 $key,
228 $propertyKey
229 ),
230 self::ERROR_CODE_PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT
231 );
232 }
233
234 foreach ($propertyValue['TAGS'] as $tag)
235 {
236 if (!is_string($tag))
237 {
238 throw new RestException(
239 sprintf(
240 'Property value (%s.%s) tag must be of string type',
241 $key,
242 $propertyKey
243 ),
244 self::ERROR_CODE_PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT
245 );
246 }
247 }
248
249 $resultItem['TAGS'] = $propertyValue['TAGS'];
250 }
251
252 $result[] = $resultItem;
253 }
254
255 return $result;
256 }
257
265 public static function deleteRequest($query, $n, \CRestServer $server): bool
266 {
268 $params = self::prepareIncomingParams($query);
269
270 $deliveryId = self::getDeliveryId($params, 'DELIVERY_ID');
271 $requestId = self::getRequestId($deliveryId, $params, 'REQUEST_ID');
272
273 $deleteResult = Requests\Manager::deleteDeliveryRequest($requestId);
274 if (!$deleteResult->isSuccess())
275 {
276 throw new RestException('Internal error', self::ERROR_CODE_DELETE_REQUEST_INTERNAL_ERROR);
277 }
278
279 return true;
280 }
281
289 public static function sendMessage($query, $n, \CRestServer $server): bool
290 {
292 $params = self::prepareIncomingParams($query);
293
294 $delivery = self::getDelivery($params, 'DELIVERY_ID');
295
296 $requestId = self::getRequestId($delivery->getId(), $params, 'REQUEST_ID');
297
298 Requests\Manager::sendMessage(
299 self::getAddressee($params, 'ADDRESSEE'),
300 self::getMessage($params, 'MESSAGE')->setCurrency($delivery->getCurrency()),
301 $requestId,
302 self::getShipmentId($requestId, $params, 'SHIPMENT_REQUEST_ID')
303 );
304
305 return true;
306 }
307
313 private static function getDeliveryId(array $params, string $key): int
314 {
315 $delivery = self::getDelivery($params, $key);
316
317 return $delivery->getId();
318 }
319
326 private static function getDelivery(array $params, string $key): Delivery\Services\Base
327 {
328 if (!isset($params[$key]))
329 {
330 throw new RestException(
331 sprintf('Parameter %s is not specified', $key),
332 self::ERROR_CODE_DELIVERY_ID_NOT_SPECIFIED
333 );
334 }
335
337 $delivery = Delivery\Services\Manager::getObjectById((int)$params[$key]);
338 if (!$delivery)
339 {
340 throw new RestException(
341 'Delivery service has not been found',
342 self::ERROR_CODE_DELIVERY_NOT_FOUND
343 );
344 }
345
346 return $delivery;
347 }
348
355 private static function getAddressee(array $params, string $key): string
356 {
357 if (empty($params[$key]))
358 {
359 throw new RestException(
360 sprintf('Parameter %s is not specified', $key),
361 self::ERROR_CODE_ADDRESSEE_IS_NOT_SPECIFIED
362 );
363 }
364
365 if (!in_array($params[$key], Requests\Manager::getMessageAddressees(), true))
366 {
367 throw new RestException(
368 sprintf('Unexpected %s parameter value', $key),
369 self::ERROR_CODE_ADDRESSEE_UNEXPECTED_VALUE
370 );
371 }
372
373 return $params[$key];
374 }
375
383 private static function getRequestId(int $deliveryId, array $params, string $key): int
384 {
385 if (empty($params[$key]))
386 {
387 throw new RestException(
388 sprintf('Parameter %s is not specified', $key),
389 self::ERROR_CODE_REQUEST_ID_NOT_SPECIFIED
390 );
391 }
392
393 $requestList = Requests\RequestTable::getList([
394 'filter' => [
395 '=DELIVERY_ID' => $deliveryId,
396 '=EXTERNAL_ID' => $params[$key],
397 ]
398 ]);
399 $request = $requestList->fetch();
400
401 if (!$request)
402 {
403 throw new RestException(
404 'Request has not been found',
405 self::ERROR_CODE_REQUEST_NOT_FOUND
406 );
407 }
408
409 return (int)$request['ID'];
410 }
411
419 private static function getShipmentId(int $requestId, array $params, string $key): int
420 {
421 $requestShipmentFilter = ['=REQUEST_ID' => $requestId];
422 if (isset($params[$key]))
423 {
424 $requestShipmentFilter['=EXTERNAL_ID'] = (int)$params[$key];
425 }
426 $requestShipment = Requests\ShipmentTable::getList(['filter' => $requestShipmentFilter])->fetch();
427 if (!$requestShipment)
428 {
429 throw new RestException(
430 'Shipment has not been found',
431 self::ERROR_CODE_REQUEST_SHIPMENT_NOT_FOUND
432 );
433 }
434
435 return (int)$requestShipment['SHIPMENT_ID'];
436 }
437
444 private static function getMessage(array $params, string $key): Requests\Message\Message
445 {
446 if (
447 !isset($params[$key])
448 || !is_array($params[$key])
449 || (
450 (!is_string($params[$key]['SUBJECT']) || empty($params[$key]['SUBJECT']))
451 && (!is_string($params[$key]['BODY']) || empty($params[$key]['BODY']))
452 )
453 )
454 {
455 throw new RestException(
456 sprintf('Parameter %s is not specified', $key),
457 self::ERROR_CODE_MESSAGE_NOT_SPECIFIED
458 );
459 }
460
461 $message = new Requests\Message\Message();
462
463 if (!empty($params[$key]['SUBJECT']))
464 {
465 $message->setSubject($params[$key]['SUBJECT']);
466 }
467 if (!empty($params[$key]['BODY']))
468 {
469 $message->setBody($params[$key]['BODY']);
470 }
471
472 if (isset($params[$key]['MONEY_VALUES']) && is_array($params[$key]['MONEY_VALUES']))
473 {
474 foreach ($params[$key]['MONEY_VALUES'] as $key => $moneyValue)
475 {
476 $message->addMoneyValue(
477 (string)$key,
478 (float)$moneyValue
479 );
480 }
481 }
482
483 if (isset($params[$key]['DATE_VALUES']) && is_array($params[$key]['DATE_VALUES']))
484 {
485 foreach ($params[$key]['DATE_VALUES'] as $key => $dateValue)
486 {
487 if (!isset($dateValue['VALUE']) || !isset($dateValue['FORMAT']))
488 {
489 throw new RestException(
490 'Unexpected date value format',
491 self::ERROR_CODE_DATE_VALUE_UNEXPECTED_FORMAT
492 );
493 }
494
495 $message->addDateValue(
496 (string)$key,
497 (int)$dateValue['VALUE'],
498 (string)$dateValue['FORMAT']
499 );
500 }
501 }
502
503 if (isset($params[$key]['STATUS']))
504 {
505 if (!is_string($params[$key]['STATUS']['MESSAGE']) || empty($params[$key]['STATUS']['MESSAGE']))
506 {
507 throw new RestException(
508 'Status message is not specified',
509 self::ERROR_CODE_MESSAGE_STATUS_NOT_SPECIFIED
510 );
511 }
512
513 if (!is_string($params[$key]['STATUS']['SEMANTIC']) || empty($params['MESSAGE']['STATUS']['SEMANTIC']))
514 {
515 throw new RestException(
516 'Message status semantic is not specified',
517 self::ERROR_CODE_MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED
518 );
519 }
520
521 if (!in_array($params[$key]['STATUS']['SEMANTIC'], Requests\Message\Status::getAvailableSemantics(), true))
522 {
523 throw new RestException(
524 sprintf('Unexpected message status semantic: %s', $params['MESSAGE']['STATUS']['SEMANTIC']),
525 self::ERROR_CODE_UNEXPECTED_MESSAGE_STATUS_SEMANTIC
526 );
527 }
528
529 $message->setStatus(
530 new Requests\Message\Status(
531 $params[$key]['STATUS']['MESSAGE'],
532 $params[$key]['STATUS']['SEMANTIC']
533 )
534 );
535 }
536
537 return $message;
538 }
539}
static includeModule($moduleName)
Definition loader.php:69
static prepareIncomingParams(array $data)
static updateRequest($query, $n, \CRestServer $server)
static deleteRequest($query, $n, \CRestServer $server)
static sendMessage($query, $n, \CRestServer $server)