Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
importeventmanager.php
1<?php
2
4
5use Bitrix\Calendar\Core;
10
12{
13 private Sync\Entities\SyncEventMap $externalEventMap;
14 private Core\Base\Map $syncSectionCollection;
15 private IncomingEventManagerInterface $importManager;
19 private $mapperFactory;
20
27 public function __construct(Sync\Factories\FactoryBase $factory, Core\Base\Map $syncSectionCollection)
28 {
29 $this->importManager = $factory->getIncomingEventManager();
30 $this->syncSectionCollection = $syncSectionCollection;
31 $this->externalEventMap = new Sync\Entities\SyncEventMap();
32 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
33 }
34
41 public function import(): ImportEventManager
42 {
44 foreach ($this->syncSectionCollection as $syncSection)
45 {
46 if (
47 $syncSection->getSectionConnection() === null
48 || !$syncSection->getSectionConnection()->isActive()
49 )
50 {
51 continue;
52 }
53
54 try
55 {
56 $result = $this->importManager->getEvents($syncSection);
57
58 if ($result->isSuccess())
59 {
60 $this->handleCalendarChange(($result->getData()['externalSyncEventMap'])->getCollection());
61 $this->externalEventMap->addItems(($result->getData()['externalSyncEventMap'])->getCollection());
62 $syncSection
63 ->getSectionConnection()
64 ->setLastSyncDate(new Core\Base\Date())
65 ->setSyncToken($this->importManager->getSyncToken())
66 ->setPageToken($this->importManager->getPageToken())
67 ->setLastSyncStatus($this->importManager->getStatus())
68 ->setVersionId($this->importManager->getEtag())
69 ;
70 $syncSection->setSectionConnection(
71 $this->saveSectionConnection($syncSection->getSectionConnection())
72 );
73 }
74 }
75 catch (Sync\Exceptions\NotFoundException $e)
76 {
77 $syncSection->getSectionConnection()
78 ->setActive(false)
79 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['deleted']);
80 $syncSection->setSectionConnection(
81 $this->saveSectionConnection($syncSection->getSectionConnection())
82 );
83 }
84 catch (Sync\Exceptions\AuthException | Sync\Exceptions\RemoteAccountException $e)
85 {
86 $syncSection->getSectionConnection()
87 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['failed']);
88 $syncSection->setSectionConnection(
89 $this->saveSectionConnection($syncSection->getSectionConnection())
90 );
91 }
92 }
93
94 return $this;
95 }
96
105 private function saveSectionConnection(Sync\Connection\SectionConnection $link): Sync\Connection\SectionConnection
106 {
107 $mapper = $this->mapperFactory->getSectionConnection();
108 return $link->getId()
109 ? $mapper->update($link)
110 : $mapper->create($link)
111 ;
112 }
113
117 public function getEvents(): Sync\Entities\SyncEventMap
118 {
119 return $this->externalEventMap;
120 }
121
125 private function handleCalendarChange(array $collection)
126 {
127 $handledCollection = $this->externalEventMap->getCollection();
128
133 foreach ($collection as $key => $value)
134 {
135 if (
136 array_key_exists($key, $handledCollection)
137 && $value->getAction() === 'save'
138 )
139 {
140 $this->externalEventMap->remove($key);
141 $this->externalEventMap->add($value, $key);
142 }
143 }
144 }
145}
__construct(Sync\Factories\FactoryBase $factory, Core\Base\Map $syncSectionCollection)