16 use Sync\Internals\HasContextTrait;
18 private array $map = [];
27 $this->context = $context;
51 'syncEventMap' => $syncEventMap,
55 foreach ($syncEventMap as $syncEvent)
58 $syncEvent->getEventConnection()
59 && ($syncEvent->getEvent()->getVersion() === $syncEvent->getEventConnection()->getVersion())
65 if ($syncEvent->isRecurrence())
67 $this->saveRecurrence($syncEvent, $syncSectionMap);
71 $this->saveSingle($syncEvent, $syncSectionMap);
84 private function getSyncSection(SyncSectionMap $syncSectionMap,
int $id): ?SyncSection
86 if (!array_key_exists($id, $this->map))
88 $this->map[$id] =
null;
90 foreach ($syncSectionMap as $syncSection)
92 if ($syncSection->getSection()->getId() === $id)
94 $this->map[$id] = $syncSection;
100 return $this->map[$id];
115 private function saveSingle(SyncEvent $syncEvent, SyncSectionMap $syncSectionMap)
117 if ($syncEvent->getEventConnection() && $syncEvent->getEventConnection()->getVendorEventId())
119 $context = (
new Sync\Util\EventContext())
120 ->setEventConnection($syncEvent->getEventConnection())
124 $result = $this->eventManager->update(
125 $syncEvent->getEvent(),
128 if ($result->isSuccess())
130 $data = $result->getData();
133 ->getEventConnection()
134 ->setEntityTag($data[
'event'][
'etag'])
135 ->setVendorVersionId($data[
'event'][
'version'])
140 catch(Sync\Exceptions\NotFoundException $e)
144 ->getEventConnection()
151 $syncSection = $this->getSyncSection(
153 $syncEvent->getEvent()->getSection()->getId()
155 if ($syncSection && $syncSection->getSectionConnection()->isActive())
157 $context = (
new Sync\Util\EventContext())
158 ->setSectionConnection($syncSection->getSectionConnection())
162 $result = $this->eventManager->create(
163 $syncEvent->getEvent(),
166 if ($result->isSuccess())
168 $data = $result->getData();
169 $eventConnection = (
new Sync\Connection\EventConnection())
170 ->setEvent($syncEvent->getEvent())
171 ->setConnection($syncSection->getSectionConnection()->getConnection())
172 ->setVendorEventId($data[
'event'][
'id'])
173 ->setVendorVersionId($data[
'event'][
'version'])
174 ->setEntityTag($data[
'event'][
'etag'])
175 ->setVersion($syncEvent->getEvent()->getVersion())
180 ->setEventConnection($eventConnection);
183 catch(Sync\Exceptions\NotFoundException $e)
185 $syncSection->getSectionConnection()->setActive(
false);
202 private function saveRecurrence(SyncEvent $syncEvent, SyncSectionMap $syncSectionMap)
204 $syncSection = $this->getSyncSection(
206 $syncEvent->getEvent()->getSection()->getId()
208 $context = (
new Sync\Util\EventContext())
209 ->setSectionConnection($syncSection->getSectionConnection());
210 if ($syncEvent->getEventConnection() && $syncEvent->getEventConnection()->getVendorEventId())
212 $context->setEventConnection($syncEvent->getEventConnection());
213 $this->eventManager->updateRecurrence(
215 $syncSection->getSectionConnection(),
221 $this->eventManager->createRecurrence(
223 $syncSection->getSectionConnection(),