21 $this->factory = $factory;
22 $this->sectionMap = $sectionMap;
28 public function export(): self
31 foreach ($this->sectionMap as $key => $syncSection)
33 if ($syncSection->getSectionConnection() ===
null)
35 $result = $this->safeCreate($syncSection->getSection());
39 $result = $this->safeUpdate($syncSection);
42 if ($result->isSuccess())
44 $exportedSyncSection = $result->getData()[
'syncSection'];
45 if ($exportedSyncSection)
47 $this->sectionMap->updateItem($exportedSyncSection,
48 $exportedSyncSection->getSectionConnection()->getVendorSectionId());
49 $this->sectionMap->remove($key);
62 return $this->sectionMap;
73 $originalName = $section->getName();
74 $sectionManager = $this->factory->getSectionManager();
82 catch (ConflictException $e)
85 $section->setName($originalName .
" ($counter)");
91 $section->setName($originalName);
96 private function safeUpdate(Sync\Entities\SyncSection $syncSection): Sync\Util\Result
100 $result = $this->factory->getSectionManager()->update(
101 $syncSection->getSection(),
102 $this->prepareContextForUpdate($syncSection)
105 catch (Sync\Exceptions\NotFoundException $e)
107 $this->clearBrokenSyncSection($syncSection);
108 $result = $this->safeCreate($syncSection->getSection());
120 private function clearBrokenSyncSection(Sync\Entities\SyncSection $syncSection)
123 $sql =
"DELETE link FROM b_calendar_event_connection link
124 inner join b_calendar_event as event ON event.ID=link.EVENT_ID
125 where event.SECTION_ID = '" . $syncSection->getSection()->getId() .
"'
126 and link.CONNECTION_ID = '". $syncSection->getSectionConnection()->getConnection()->getId() .
"'
129 (
new Core\Mappers\SectionConnection())->
delete(
130 $syncSection->getSectionConnection(),
131 [
'softDelete' =>
false]
133 $syncSection->setSectionConnection(
null);
141 private function prepareContextForUpdate(Sync\Entities\SyncSection $syncSection)
143 return (
new Sync\Util\SectionContext())->setSectionConnection($syncSection->getSectionConnection());