Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vendorsynchronization.php
1<?php
2
4
5use Bitrix\Calendar\Core;
28use Bitrix\Main\Entity\ReferenceField;
34use Exception;
35
37{
41 private FactoryInterface $factory;
45 private Core\Mappers\Factory $mapperFactory;
46
52 public function __construct(FactoryInterface $factory)
53 {
54 $this->factory = $factory;
55 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
56 }
57
68 public function createEvent(Event $event, EventContext $context): Result
69 {
70 $mainResult = new Result();
71 $data = [];
72 $factory = $this->factory;
73 $manager = $factory->getEventManager();
74
75 $sectionLink = $context->getSectionConnection();
76
77 if (!$sectionLink)
78 {
79 return $mainResult->setData([$factory->getConnection()->getVendor()->getCode() => [
80 'status' => Dictionary::SYNC_STATUS['failed'],
81 'message' => 'Section link not found', // TODO: Localize?
82 ]]);
83 }
84
85 $eventLink = $this->getEventConnection($event)
86 ?? (new EventConnection())
87 ->setEvent($event)
88 ->setConnection($sectionLink->getConnection());
89 if ($sectionLink->isActive())
90 {
91 try
92 {
93 $result = $manager->create($event, $context);
94 if ($result->isSuccess())
95 {
96 $resultData = $result->getData();
97 $status = Dictionary::SYNC_STATUS['success'];
98 $eventLink
99 ->setVendorEventId($resultData['event']['id'])
100 ->setEntityTag($resultData['event']['etag'])
101 ->setVendorVersionId($resultData['event']['version'] ?? null)
102 ->setVersion($event->getVersion())
103 ->setLastSyncStatus($status);
104 if (!empty($result->getData()['data']))
105 {
106 $eventLink->setData($result->getData()['data']);
107 }
108 }
109 else
110 {
111 $status = Dictionary::SYNC_STATUS['create'];
112 $eventLink->setLastSyncStatus($status);
113 }
114 $syncResult = [
115 'status' => $status,
116 'result' => $result,
117 ];
118 }
119 catch (NotFoundException $e)
120 {
121 // if section was not found on service
122 $sectionLink->setActive(false)->setSyncToken(null)->setPageToken(null);
123 $this->mapperFactory->getSectionConnection()->update($sectionLink);
124 }
125 }
126 // dont change to "else", because active status could change in the if section
127 if (!$sectionLink->isActive())
128 {
129 $status = Dictionary::SYNC_STATUS['create'];
130 $eventLink->setLastSyncStatus($status)->setVendorEventId('');
131
132 $syncResult = [
133 'status' => $status,
134 ];
135 }
137 $eventLink = $eventLink->getId()
138 ? $this->mapperFactory->getEventConnection()->update($eventLink)
139 : $this->mapperFactory->getEventConnection()->create($eventLink);
140
141 $syncResult['eventConnectionId'] = $eventLink->getId();
142 $data['result'] = $syncResult;
143
144 return $mainResult->setData($data);
145 }
146
158 public function updateEvent(Event $event, EventContext $context): Result
159 {
160 $mainResult = new Result();
161 $resultData = [];
162 $factory = $this->factory;
163 $sectionLink = $context->getSectionConnection();
164
165 if (!$sectionLink)
166 {
167 // TODO: this condition must be checked before call this method
168 $mainResult->addError(new Error('Section connection not found'));
169 return $mainResult;
170 }
171 $manager = $factory->getEventManager();
172 $eventLink = $context->getEventConnection();
173
174 if ($eventLink && $eventLink->getVendorEventId() && $sectionLink->isActive())
175 {
176 if ((int)$eventLink->getEventVersion() !== $event->getVersion())
177 {
178 try
179 {
180 $result = $manager->update($event, $context);
181 if ($result->isSuccess())
182 {
183 $status = Dictionary::SYNC_STATUS['success'];
184 $eventLink
185 ->setEntityTag($result->getData()['event']['etag'])
186 ->setVendorVersionId($result->getData()['event']['version'] ?? null)
187 ->setVersion($event->getVersion());
188 }
189 else
190 {
191 $status = Dictionary::SYNC_STATUS['update'];
192 }
193 $eventLink->setLastSyncStatus($status);
194
195 $this->mapperFactory->getEventConnection()->update($eventLink);
196 }
197 catch (NotFoundException $e)
198 {
199 $this->mapperFactory->getEventConnection()->delete($eventLink);
200
201 return $this->createEvent($event, $context);
202 }
203 }
204 else
205 {
206 $status = Dictionary::SYNC_STATUS['success'];
207 $eventLink->setLastSyncStatus($status);
208 $this->mapperFactory->getEventConnection()->update($eventLink);
209 }
210
211 }
212 elseif ($eventLink && !$sectionLink->isActive())
213 {
214 $status = Dictionary::SYNC_STATUS['update'];
215 $eventLink
216 ->setVersion($event->getVersion())
217 ->setLastSyncStatus($status)
218 ;
219 $this->mapperFactory->getEventConnection()->update($eventLink);
220 }
221 elseif (!$eventLink)
222 {
223 $status = Dictionary::SYNC_STATUS['create'];
224 $eventLink = (new EventConnection())
225 ->setEvent($event)
226 ->setConnection($sectionLink->getConnection())
227 ->setLastSyncStatus($status)
228 ->setVersion($event->getVersion())
229 ;
230 $this->mapperFactory->getEventConnection()->create($eventLink);
231 }
232 else
233 {
234 $status = Dictionary::SYNC_STATUS['create'];
235 }
236 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
237 'status' => $status,
238 ];
239
240 return $mainResult->setData($resultData);
241 }
242
255 public function deleteEvent(Event $event, EventContext $context): Result
256 {
257 global $DB;
258 $mainResult = new Result();
259 $resultData = [];
260 $factory = $this->factory;
261 $sectionLink = $context->getSectionConnection();
262
263 if (!$sectionLink)
264 {
265 // TODO: can't be, maybe need log
266 $mainResult->addError(new Error('Section connection not found'));
267 return $mainResult;
268 }
269
270 $manager = $factory->getEventManager();
271 $eventLink = $context->getEventConnection();
272 if ($eventLink && $eventLink->getVendorEventId() && $sectionLink->isActive())
273 {
274 try
275 {
276 $result = $manager->delete($event, $context);
277 if ($result->isSuccess())
278 {
279 if ($event->getRecurringRule())
280 {
281 $childToDelete = [];
282 $childIds = EventConnectionTable::query()
283 ->setSelect(['ID', 'EVENT'])
284 ->where('EVENT.RECURRENCE_ID', $event->getParentId())
285 ->where('EVENT.OWNER_ID', $event->getOwner()->getId())
286 ->exec()
287 ;
288 while ($child = $childIds->fetch())
289 {
290 $childToDelete[] = $child['ID'];
291 }
292 if ($childToDelete)
293 {
294 $DB->Query("DELETE FROM b_calendar_event_connection
295 WHERE ID IN (" . implode(',', $childToDelete) . ")
296 AND CONNECTION_ID = '{$factory->getConnection()->getId()}';
297 ");
298 }
299
300 }
301
302 $status = Dictionary::SYNC_STATUS['success'];
303 $this->mapperFactory->getEventConnection()->delete($eventLink);
304 }
305 else
306 {
307 $status = Dictionary::SYNC_STATUS['delete'];
308 $eventLink->setLastSyncStatus($status);
309 $this->mapperFactory->getEventConnection()->update($eventLink);
310 }
311 }
312 catch (NotFoundException $e)
313 {
314 $status = Dictionary::SYNC_STATUS['delete'];
315 $this->mapperFactory->getEventConnection()->delete($eventLink);
316 }
317 }
318 elseif ($eventLink && !$sectionLink->isActive())
319 {
320 $status = Dictionary::SYNC_STATUS['delete'];
321 $eventLink->setLastSyncStatus($status);
322 $this->mapperFactory->getEventConnection()->update($eventLink);
323 }
324 else
325 {
326 $status = Dictionary::SYNC_STATUS['success'];
327 }
328 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
329 'status' => $status,
330 ];
331
332 return $mainResult->setData($resultData);
333 }
334
346 public function createInstance(Event $event, EventContext $context): Result
347 {
348 $resultData = [];
349 $result = new Result();
350 $factory = $this->factory;
351
352 $sectionLink = $context->getSectionConnection();
353
354 if (!$sectionLink)
355 {
356 $result->addError(new Error('Section connection not found'));
357
358 return $result;
359 }
360
362 if ($masterEvent = $this->getMasterEvent($event))
363 {
364 $manager = $factory->getEventManager();
365 $masterLink = $this->getEventConnection($masterEvent);
366 if ($masterLink === null)
367 {
368 // error of Transition period. This situation is impossible in regular mode.
369 $result->addError(new Error('Series master event does not have connection with vendor'));
370
371 return $result;
372 }
373 $context->setEventConnection($masterLink);
374
375 $result = $manager->createInstance($event, $context);
376
377 if ($result->isSuccess())
378 {
379 $resultData = $result->getData();
380 $status = Dictionary::SYNC_STATUS['success'];
381 if ($factory->getCode() === Icloud\Helper::ACCOUNT_TYPE)
382 {
383 $masterLink
384 ->setVendorEventId($result->getData()['event']['id'])
385 ->setEntityTag($resultData['event']['etag'])
386 ->setLastSyncStatus($status)
387 ->setVersion($masterEvent->getVersion())
388 ;
389 $this->mapperFactory->getEventConnection()->update($masterLink);
390 }
391
392 $eventLink = (new EventConnection())
393 ->setEvent($event)
394 ->setConnection($sectionLink->getConnection())
395 ->setVendorEventId($result->getData()['event']['id'])
396 ->setVendorVersionId($result->getData()['event']['version'] ?? null)
397 ->setRecurrenceId($result->getData()['event']['recurrence'] ?? null)
398 ->setData($result->getData()['event']['data'] ?? null)
399 ->setLastSyncStatus($status)
400 ->setVersion($event->getVersion())
401 ;
402 if ($factory->getCode() !== Icloud\Helper::ACCOUNT_TYPE)
403 {
404 $eventLink->setEntityTag($result->getData()['event']['etag'] ?? null);
405 }
406
407 $this->mapperFactory->getEventConnection()->create($eventLink);
408 }
409 else
410 {
411 $status = Dictionary::SYNC_STATUS['failed'];
412 $masterLink->setLastSyncStatus(Dictionary::SYNC_STATUS['update']);
413 $this->mapperFactory->getEventConnection()->update($masterLink);
414 }
415 }
416 else
417 {
418 $status = Dictionary::SYNC_STATUS['failed'];
419 $result->addError(new Error('Master event not found'));
420 }
421
422 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
423 'result' => $result,
424 'status' => $status,
425 ];
426
427 return (new Result())->setData($resultData);
428 }
429
441 public function updateInstance(Event $event, EventContext $context): Result
442 {
443 $result = new Result();
444 $resultData = [];
445 $factory = $this->factory;
446 $sectionLink = $context->getSectionConnection();
447 $eventLink = $context->getEventConnection();
448 if (!$sectionLink)
449 {
450 $result->addError(new Error('Section connection not found'));
451
452 return $result;
453 }
454 if (!$eventLink)
455 {
456 $result->addError(new Error('Instance connection not found'));
457
458 return $result;
459 }
460
461 if ($masterEvent = $this->getMasterEvent($event))
462 {
463 $manager = $factory->getEventManager();
464 $masterLink = $this->getEventConnection($masterEvent);
465
466 if ($masterLink === null)
467 {
468 // error of Transition period. This situation is impossible in regular mode.
469 $result->addError(new Error('Series master event does not have connection with vendor'));
470
471 return $result;
472 }
473
474 $context
475 ->setEventConnection($masterLink)
476 ->add('sync', 'instanceLink', $eventLink)
477 ;
478
479 $result = $manager->updateInstance($event, $context);
480 if ($result->isSuccess())
481 {
482 $status = Dictionary::SYNC_STATUS['success'];
483 $eventLink
484 ->setLastSyncStatus($status)
485 ->setVersion($event->getVersion())
486 ->setVendorVersionId($result->getData()['event']['version'] ?? null)
487 ;
488 if (!empty($result->getData()['event']['id']))
489 {
490 $eventLink->setVendorEventId($result->getData()['event']['id']);
491 }
492 if ($factory->getCode() !== Icloud\Helper::ACCOUNT_TYPE)
493 {
494 $eventLink->setEntityTag($result->getData()['event']['etag'] ?? null);
495 }
496 $this->mapperFactory->getEventConnection()->update($eventLink);
497
498 if ($factory->getCode() === Icloud\Helper::ACCOUNT_TYPE)
499 {
500 $masterLink
501 ->setEntityTag($result->getData()['event']['etag'])
502 ->setLastSyncStatus($status)
503 ->setVersion($masterEvent->getVersion())
504 ;
505 $this->mapperFactory->getEventConnection()->update($masterLink);
506 }
507 }
508 else
509 {
510 $status = Dictionary::SYNC_STATUS['failed'];
511 $masterLink->setLastSyncStatus(Dictionary::SYNC_STATUS['update']);
512 $this->mapperFactory->getEventConnection()->update($masterLink);
513 }
514 }
515 else
516 {
517 $status = Dictionary::SYNC_STATUS['failed'];
518 $result->addError(new Error('Master event not found'));
519 }
520
521 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
522 'result' => $result,
523 'status' => $status,
524 ];
525
526 return (new Result())->setData($resultData);
527 }
528
536 public function deleteInstance(Event $event, EventContext $context): Result
537 {
538 $mainResult = new Result();
539 $result = new Result();
540 $resultData = [];
541 $factory = $this->factory;
542 $excludeDate = $context->sync['excludeDate'] ?? null;
543
544 if (!$excludeDate)
545 {
546 $mainResult->addError(new Error('Not found info about exclude date'));
547 return $mainResult;
548 }
549 $sectionLink = $context->getSectionConnection();
550 $masterLink = $context->getEventConnection();
551
552 if ($masterLink && $sectionLink)
553 {
554 $manager = $factory->getEventManager();
555 $result = $manager->deleteInstance($event, $context);
556 if ($result->isSuccess())
557 {
558 $status = Dictionary::SYNC_STATUS['success'];
559 $masterLink
560 ->setEntityTag($result->getData()['event']['etag'] ?? null)
561 ->setLastSyncStatus($status)
562 ->setVersion($masterLink->getEvent()->getVersion())
563 ;
564 }
565 else
566 {
567 $status = Dictionary::SYNC_STATUS['failed'];
568 $masterLink->setLastSyncStatus(Dictionary::SYNC_STATUS['update']);
569 }
570
571 $this->mapperFactory->getEventConnection()->update($masterLink);
572 }
573 else
574 {
575 $status = Dictionary::SYNC_STATUS['failed'];
576 $result->addError(new Error('Link not found'));
577 }
578
579 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
580 'result' => $result,
581 'status' => $status,
582 ];
583
584 return $result->setData($resultData);
585 }
586
594 public function createRecurrence(SyncEvent $recurrenceEvent, EventContext $context): Result
595 {
596 $mainResult = new Result();
597 $resultData = [];
598 $factory = $this->factory;
599 $manager = $factory->getEventManager();
601 $sectionLink = $context->getSectionConnection();
602 if (!$sectionLink)
603 {
604 $mainResult->addError(new Error('Section connection not found'));
605
606 return $mainResult;
607 }
608 $context->add('sync', 'vendorSectionId', $sectionLink->getVendorSectionId());
609
610 $recurrenceEvent->getEvent()->setUid(null);
611
612 $result = $manager->createRecurrence($recurrenceEvent, $sectionLink, $context);
613
614 if ($result->isSuccess())
615 {
616 $masterResult = $this->createEventLink($recurrenceEvent, $this->factory->getConnection()->getId());
617
619 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
620 {
621 $instanceResult[] = $this->createEventLink($instance, $this->factory->getConnection()->getId());
622 }
623
624 $status = Dictionary::SYNC_STATUS['success'];
625 }
626
627 else
628 {
629 $status = Dictionary::SYNC_STATUS['failed'];
630 }
631
632 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
633 'result' => $result,
634 'status' => $status,
635 'linkMasterResult' => $masterResult ?? null,
636 'linkInstancesResult' => $instanceResult ?? null,
637 ];
638
639 return $mainResult->setData($resultData);
640 }
641
649 public function updateRecurrence(SyncEvent $recurrenceEvent, EventContext $context): Result
650 {
651 $mainResult = new Result();
652 $resultData = [];
653 $factory = $this->factory;
654 $manager = $factory->getEventManager();
656 $sectionLink = $context->getSectionConnection();
657 if (!$sectionLink)
658 {
659 $mainResult->addError(new Error('Section connection not found'));
660
661 return $mainResult;
662 }
663 $context->add('sync', 'vendorSectionId', $sectionLink->getVendorSectionId());
664
665 $recurrenceEvent->getEvent()->setUid(
666 $recurrenceEvent->getEventConnection()->getVendorEventId()
667 );
668
669 if (
670 (int)$recurrenceEvent->getEventConnection()->getEventVersion()
671 === $recurrenceEvent->getEvent()->getVersion()
672 )
673 {
674 $result = new Result();
675 $result->setData([
676 'event' => [
677 'id' => $recurrenceEvent->getEventConnection()->getVendorEventId(),
678 'etag' => $recurrenceEvent->getEventConnection()->getEntityTag(),
679 ],
680 ]);
681 $status = Dictionary::SYNC_STATUS['success'];
682 }
683 else
684 {
685 try
686 {
687 $result = $manager->updateRecurrence($recurrenceEvent, $sectionLink, $context);
688 if ($result->isSuccess())
689 {
690 $masterResult = $this->updateEventLink($recurrenceEvent);
691 $instanceResult = [];
693 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
694 {
695 if ($instance->getEventConnection() && $instance->getEventConnection()->getId())
696 {
697 $instanceResult[] = $this->updateEventLink($instance);
698 }
699 else
700 {
701 $instanceResult[] = $this->createEventLink(
702 $instance,
703 $this->factory->getConnection()->getId()
704 );
705 }
706 }
707
708 $status = Dictionary::SYNC_STATUS['success'];
709 }
710 else
711 {
712 $status = Dictionary::SYNC_STATUS['failed'];
713 }
714 }
715 catch (NotFoundException $e)
716 {
717 $recurrenceEvent->getEvent()->setUid(null);
718 return $this->createRecurrence($recurrenceEvent, $context);
719 }
720 }
721
722 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
723 'result' => $result,
724 'status' => $status,
725 'linkMasterResult' => $masterResult ?? null,
726 'linkInstancesResult' => $instanceResult ?? null,
727 ];
728
729 return $mainResult->setData($resultData);
730 }
731
744 public function createSection(Section $section, SectionContext $context): Result
745 {
746 $mainResult = new Result();
747 $resultData = [];
748 $factory = $this->factory;
749 $manager = $factory->getSectionManager();
750 $context->add('sync', 'connection', $factory->getConnection());
751
752 if ($sectionLink = $context->getSectionConnection())
753 {
754 $resultData['sectionConnection'] = $sectionLink;
755 $status = Dictionary::SYNC_STATUS['update'];
756 // TODO: what to do?
757 }
758 else
759 {
760 $safeCreate = static function (Section $section, SectionContext $context) use ($manager)
761 {
762 $result = new Result();
763 $counter = 0;
764 $originalName = $section->getName();
765 do
766 {
767 try
768 {
769 $result = $manager->create($section, $context);
770 $success = true;
771 }
772 catch (ConflictException $e)
773 {
774 $counter++;
775 $section->setName($originalName . " ($counter)");
776 $success = false;
777 }
778 }
779 while (!$success);
780
781 $section->setName($originalName);
782
783 return $result;
784 };
785
786 $result = $safeCreate($section, $context);
787 if ($result->isSuccess())
788 {
789 $status = Dictionary::SYNC_STATUS['success'];
790 $sectionLink = (new SectionConnection())
791 ->setSection($section)
792 ->setConnection($factory->getConnection())
793 ->setVendorSectionId($result->getData()['id'])
794 ->setVersionId($result->getData()['version'])
795 ->setActive(true)
796 ->setLastSyncStatus($status)
797 ->setLastSyncDate(new Core\Base\Date())
798 ;
799 $this->mapperFactory->getSectionConnection()->create($sectionLink);
800
801 $resultData['sectionConnection'] = $sectionLink;
802 }
803 else
804 {
805 $mainResult->addErrors($result->getErrors());
806 $status = Dictionary::SYNC_STATUS['failed'];
807 }
808 }
809
810 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
811 'status' => $status,
812 ];
813
814 return $mainResult->setData($resultData);
815 }
816
825 public function updateSection(Section $section, SectionContext $context): Result
826 {
827 $mainResult = new Result();
828 $result = new Result();
829 $factory = $this->factory;
830
831 if ($sectionLink = $context->getSectionConnection())
832 {
833 $manager = $factory->getSectionManager();
834 if ($sectionLink->isActive())
835 {
836 try
837 {
838 $result = $manager->update($section, $context);
839 if ($result->isSuccess())
840 {
841 $status = Dictionary::SYNC_STATUS['success'];
842 $sectionLink->setVersionId($result->getData()['version']);
843 }
844 else
845 {
846 $status = Dictionary::SYNC_STATUS['update'];
847 $mainResult->addErrors($result->getErrors());
848 }
849 }
850 catch (NotFoundException $e)
851 {
852 $sectionLink->setActive(false);
853 $status = Dictionary::SYNC_STATUS['inactive'];
854 }
855
856 $sectionLink
857 ->setLastSyncStatus($status)
858 ->setLastSyncDate(new Core\Base\Date());
859 $this->mapperFactory->getSectionConnection()->update($sectionLink);
860 }
861 else
862 {
863 $status = Dictionary::SYNC_STATUS['inactive'];
864 }
865 }
866 else
867 {
868 $status = Dictionary::SYNC_STATUS['create'];
869 // TODO: think, what to do in this case. Call the creation or throw exception
870 }
871
872 $resultData = [
873 $factory->getConnection()->getVendor()->getCode() => [
874 'status' => $status,
875 ],
876 'error' => $result->getData()['error'] ?? '',
877 ];
878
879 return $mainResult->setData($resultData);
880 }
881
888 public function deleteSection(Section $section, SectionContext $context): Result
889 {
890 $mainResult = new Result();
891 $resultData = [];
892 $factory = $this->factory;
893 $manager = $factory->getSectionManager();
894
895 if (($sectionLink = $context->getSectionConnection()) && $sectionLink->isActive())
896 {
897 $sectionLink->setSection($section);
898 $result = $manager->delete($section, $context);
899 if ($result->isSuccess())
900 {
901 $status = Dictionary::SYNC_STATUS['success'];
902 }
903 else
904 {
905 $status = Dictionary::SYNC_STATUS['delete'];
906 $mainResult->addErrors($result->getErrors());
907 $sectionLink
908 ->setLastSyncStatus($status)
909 ->setLastSyncDate(new Core\Base\Date())
910 ;
911 $this->mapperFactory->getSectionConnection()->update($sectionLink);
912 }
913 }
914 else
915 {
916 $status = Dictionary::SYNC_STATUS['delete'];
917 }
918
919 $resultData[$factory->getConnection()->getVendor()->getCode()] = [
920 'status' => $status,
921 ];
922
923 return $mainResult->setData($resultData);
924 }
925
939 public function upEventVersion(Event $event, Connection $connection, string $version)
940 {
941 $link = EventConnectionTable::query()
942 ->setSelect(['ID'])
943 ->addFilter('CONNECTION_ID', $connection->getId())
944 ->addFilter('EVENT_ID', $event->getId())
945 ->exec()
946 ->fetchObject()
947 ;
948
949 if ($link)
950 {
951 EventConnectionTable::update($link->getId(), [
952 'fields' => [
953 'VERSION' => $version,
954 ],
955 ]);
956 }
957 }
958
962 public function canSubscribeSection(): bool
963 {
964 return $this->factory->canSubscribeSection();
965 }
966
970 public function canSubscribeConnection(): bool
971 {
972 return $this->factory->canSubscribeConnection();
973 }
974
981 {
982 return $this->factory->getPushManager()->addSectionPush($link);
983 }
984
988 public function subscribeConnection(): Result
989 {
990 return $this->factory->getPushManager()->addConnectionPush($this->factory->getConnection());
991 }
992
998 public function renewPush(Push $push): Result
999 {
1000 if ($manager = $this->factory->getPushManager())
1001 {
1002 return $manager->renewPush($push);
1003 }
1004
1005 return (new Result())->addError(new Error('Push manager for service not found', 404));
1006 }
1007
1013 public function unsubscribeSection(Push $push): Result
1014 {
1015 return $this->factory->getPushManager()->deletePush($push);
1016 }
1017
1023 public function unsubscribeConnection(Push $push): Result
1024 {
1025 return $this->factory->getPushManager()->deletePush($push);
1026 }
1027
1036 private function getEventConnection(Event $event): ?EventConnection
1037 {
1038 return $this->mapperFactory->getEventConnection()->getMap([
1039 '=EVENT_ID' => $event->getId(),
1040 '=CONNECTION_ID' => $this->factory->getConnection()->getId(),
1041 ])->fetch();
1042 }
1043
1052 private function getMasterEvent(Event $event): ?Event
1053 {
1054 return $this->mapperFactory->getEvent()->getMap([
1055 '=PARENT_ID' => $event->getRecurrenceId(),
1056 '=OWNER_ID' => $event->getOwner()->getId(),
1057 ])->fetch();
1058 }
1059
1067 private function createEventLink(SyncEvent $syncEvent, int $connectionId): \Bitrix\Main\ORM\Data\AddResult
1068 {
1069 // TODO: change to mapper->create;
1070 // $this->mapperFactory->getEventConnection()->create();
1071 return EventConnectionTable::add([
1072 'EVENT_ID' => $syncEvent->getEvent()->getId(),
1073 'CONNECTION_ID' => $connectionId,
1074 'VENDOR_EVENT_ID' => $syncEvent->getEventConnection()
1075 ? $syncEvent->getEventConnection()->getVendorEventId()
1076 : null
1077 ,
1078 'SYNC_STATUS' => Dictionary::SYNC_STATUS['success'],
1079 'VERSION' => $syncEvent->getEvent()->getVersion(),
1080 'ENTITY_TAG' => $syncEvent->getEventConnection()
1081 ? $syncEvent->getEventConnection()->getEntityTag()
1082 : null
1083 ,
1084 ]);
1085 }
1086
1093 private function updateEventLink(SyncEvent $syncEvent): Result
1094 {
1095 $newLink = $this->mapperFactory->getEventConnection()->update($syncEvent->getEventConnection());
1096
1097 return (new Result())->setData(['eventConnection' => $newLink]);
1098 }
1099}
getMap($filter, int $limit=null, array $order=null)
Definition mapper.php:207
create(Core\Base\EntityInterface $entity, array $params=[])
Definition mapper.php:74
upEventVersion(Event $event, Connection $connection, string $version)
updateSection(Section $section, SectionContext $context)
createSection(Section $section, SectionContext $context)
deleteSection(Section $section, SectionContext $context)
add(string $type, string $property, $value)
Definition context.php:53
setEventConnection(?EventConnection $eventConnection)
addError(Error $error)
Definition result.php:50