38 private Mappers\Factory $mapperFactory;
40 private static array $outgoingManagersCache = [];
53 $this->user = \Bitrix\Calendar\Core\Role\Helper::getUserRole($userId);
54 $this->mapperFactory = ServiceLocator::getInstance()->get(
'calendar.service.mappers.factory');
67 'message' =>
'Could not finish sync.',
70 $owner = Helper::getRole(\CCalendar::GetUserId(), User::TYPE);
71 $pusher =
static function ($result) use ($owner)
74 'process_sync_connection',
79 if ($result[
'stage'] ===
'export_finished')
93 'status' =>
'success',
94 'message' =>
'CONNECTION_CREATED',
95 'connectionId' => $connection->getId(),
113 $this->connection = $connection = $this->createConnection($this->mapperFactory->getConnection());
116 $factory =
new Factory($this->connection);
146 $factory =
new Factory($connection);
148 $nameResult = $factory->getImportManager()->requestConnectionId();
150 if (!$nameResult->isSuccess() || empty($nameResult->getData()[
'id']))
155 $name = $nameResult->getData()[
'id'];
156 $connectionMap = $mapper->getMap([
157 '%=NAME' =>
'%'. $name .
'%',
158 '=ENTITY_ID' => $this->user->getId(),
160 ],
null, [
'ID' =>
'ASC']);
162 $currentConnection = $connectionMap->fetch();
164 if ($currentConnection && $duplicatedConnection = $connectionMap->fetch())
166 $this->deleteConnectionData($duplicatedConnection->getId());
169 $connection->setName($name);
171 if ($currentConnection)
177 $mapper->
update($currentConnection);
179 return $currentConnection;
182 return $mapper->create($connection);
200 private function sendResult(
string $stage): void
203 'vendorName' =>
'google',
204 'accountName' => $this->connection->getName(),
221 $links = $this->mapperFactory->getSectionConnection()->getMap([
222 '=CONNECTION_ID' => $connection->getId(),
226 $manager = $this->getOutgoingManager($connection);
227 foreach ($links as $link)
229 $manager->subscribeSection($link);
232 $manager->subscribeConnection();
242 private function getOutgoingManager(
Connection $connection)
244 if (empty(static::$outgoingManagersCache[$connection->getId()]))
246 static::$outgoingManagersCache[$connection->getId()] =
new OutgoingManager($connection);
249 return static::$outgoingManagersCache[$connection->getId()];
257 private function deleteConnectionData(
int $connectionId): void
261 DELETE FROM b_calendar_event_connection
262 WHERE CONNECTION_ID = " . $connectionId .
";"
266 DELETE FROM b_calendar_section_connection
267 WHERE CONNECTION_ID = " . $connectionId .
";"
271 DELETE FROM b_dav_connections
272 WHERE ID = " . $connectionId .
";"