38 private const STATUSES = [
39 'connection_created' =>
'connection_created',
40 'connection_renamed' =>
'connection_renamed',
41 'sections_sync_finished' =>
'sections_sync_finished',
42 'events_sync_finished' =>
'events_sync_finished',
43 'subscribe_finished' =>
'subscribe_finished',
44 'all_finished' =>
'all_finished',
45 'export_finished' =>
'export_finished',
51 private string $accountName =
'';
62 $this->owner = $owner;
74 'status' =>
'success',
75 'message' =>
'CONNECTION_CREATED'
78 $owner = \Bitrix\Calendar\Core\Role\Helper::getRole(\CCalendar::GetUserId(), User::TYPE);
79 $pusher =
static function ($result) use ($owner)
82 'process_sync_connection',
87 if ($result[
'stage'] === self::STATUSES[
'export_finished'])
89 NotificationManager::addFinishedSyncNotificationAgent(
101 $response[
'connectionId'] = $connection->getId();
105 $response[
'connectionId'] =
null;
108 catch (\Throwable $e)
112 'message' =>
'Could not finish sync: '.$e->getMessage()
131 if ($connection = $this->initConnection())
134 $this->muteConnection($connection,
true);
135 $status = self::STATUSES[
'connection_created'];
138 $connection = $this->fixUglyAccountName($connection);
139 $this->sendResult($status);
142 $factory =
new Factory($connection);
153 $status = self::STATUSES[
'export_finished'];
154 $this->sendResult($status);
156 if ($this->isPushEnabled())
158 $this->initSubscription($connection);
165 $this->muteConnection($connection,
false);
168 catch (SyncException|Throwable $e)
171 $this->rollBack($connection);
176 throw new SyncException(
'Error of create connection');
185 private function muteConnection(Connection $connection,
bool $state)
187 $original = $connection->isDeleted();
188 $connection->setDeleted($state);
189 (
new Core\Mappers\Connection())->update($connection);
190 $connection->setDeleted($original);
196 private function initConnection(): ?Connection
198 $connectionManager =
new ConnectionManager();
199 $result = $connectionManager->initConnection(
204 if ($result->isSuccess())
206 return $result->getData()[
'connection'];
217 private function sendResult(
string $stage)
221 'accountName' => $this->getAccountName(),
229 private function getAccountName(): string
231 return $this->accountName ??
'';
234 private array $outgoingManagersCache = [];
242 private function getOutgoingManager(Connection $connection)
245 if (empty($this->outgoingManagersCache[$connection->getId()]))
247 $this->outgoingManagersCache[$connection->getId()] =
new OutgoingManager($connection);
250 return $this->outgoingManagersCache[$connection->getId()];
258 private function initSubscription(Connection $connection): Result
260 $result =
new Result();
263 $links = (
new Core\Mappers\SectionConnection())->getMap([
264 '=CONNECTION_ID' => $connection->getId(),
267 $manager = $this->getOutgoingManager($connection);
268 foreach ($links as $link)
272 $manager->subscribeSection($link);
276 $result->addError(
new Error($e->getMessage(), $e->getCode()));
282 $result->addError(
new Error($e->getMessage(), $e->getCode()));
296 private function setConnectionStatus(Connection $connection,
string $status)
298 DavConnectionTable::update($connection->getId(), [
299 'LAST_RESULT' => $status,
310 private function fixUglyAccountName(Connection $connection): Connection
312 if (substr($connection->getName(), 0,9) ===
'Office365')
314 $currentName = $connection->getName();
317 $userData = $context->getApiClient()->get(
'me');
318 if (!empty($userData[
'userPrincipalName']))
320 if ($oldConnection = $this->getConnection(
321 $connection->getOwner(),
323 $userData[
'userPrincipalName']
326 $oldConnection->setDeleted(
false);
327 (
new Core\Mappers\Connection())->
delete($connection, [
'softDelete' =>
false]);
328 $connection = $oldConnection;
332 $connection->setName($userData[
'userPrincipalName']);
333 $result = (
new ConnectionManager())->update($connection);
334 if (!$result->isSuccess())
336 $connection->setName($currentName);
340 }
catch (Exception $e) {
341 $connection->setName($currentName);
344 $this->accountName = $connection->getName();
356 private function getConnection(Role $owner,
string $serviceName,
string $name): ?Connection
360 return (
new Core\Mappers\Connection())->getMap([
361 '=ENTITY_TYPE' => $owner->getType(),
362 '=ENTITY_ID' => $owner->getId(),
363 '=ACCOUNT_TYPE' => $serviceName,
367 catch (BaseException|ArgumentException|SystemException $e)
380 private function rollBack(Connection $connection)
382 (
new ConnectionManager())->disableConnection($connection);
384 NotificationManager::sendRollbackSyncNotification(
385 $connection->getOwner()->getId(),
386 $connection->getVendor()->getCode()
393 private function isPushEnabled(): bool
395 return CCalendar::IsBitrix24() || COption::GetOptionString(
'calendar',
'sync_by_push',
false);