37 use HandleStatusTrait;
39 private const STATUSES = [
40 'connection_created' =>
'connection_created',
41 'connection_renamed' =>
'connection_renamed',
42 'sections_sync_finished' =>
'sections_sync_finished',
43 'events_sync_finished' =>
'events_sync_finished',
44 'subscribe_finished' =>
'subscribe_finished',
45 'all_finished' =>
'all_finished',
46 'export_finished' =>
'export_finished',
52 private string $accountName =
'';
63 $this->owner = $owner;
75 'status' =>
'success',
76 'message' =>
'CONNECTION_CREATED'
80 $pusher =
static function (
$result) use ($owner)
83 PushCommand::ProcessSyncConnection,
88 if (
$result[
'stage'] === self::STATUSES[
'export_finished'])
90 NotificationManager::addFinishedSyncNotificationAgent(
109 catch (\Throwable $e)
113 'message' =>
'Could not finish sync: '.$e->getMessage()
136 $status = self::STATUSES[
'connection_created'];
151 ->addStatusHandlerList($this->getStatusHandlerList())
154 $status = self::STATUSES[
'export_finished'];
157 if ($this->isPushEnabled())
169 catch (SyncException|Throwable $e)
177 throw new SyncException(
'Error of create connection');
186 private function muteConnection(Connection
$connection,
bool $state)
190 (
new Core\Mappers\Connection())->update(
$connection);
197 private function initConnection(): ?Connection
199 $connectionManager =
new ConnectionManager();
200 $connections = $connectionManager->getConnectionsData($this->owner, [Helper::ACCOUNT_TYPE]);
201 $connectionManager->deactivateConnections($connections);
203 $result = $connectionManager->initConnection(
205 Helper::ACCOUNT_TYPE,
210 return $result->getData()[
'connection'];
221 private function sendResult(
string $stage)
224 'vendorName' => Helper::ACCOUNT_TYPE,
225 'accountName' => $this->getAccountName(),
233 private function getAccountName(): string
235 return $this->accountName ??
'';
238 private array $outgoingManagersCache = [];
246 private function getOutgoingManager(Connection
$connection)
249 if (empty($this->outgoingManagersCache[
$connection->getId()]))
254 return $this->outgoingManagersCache[
$connection->getId()];
262 private function initSubscription(Connection
$connection): Result
267 $links = (
new Core\Mappers\SectionConnection())->getMap([
272 foreach ($links as $link)
280 $result->addError(
new Error($e->getMessage(), $e->getCode()));
286 $result->addError(
new Error($e->getMessage(), $e->getCode()));
314 private function fixUglyAccountName(Connection
$connection): Connection
316 if (substr(
$connection->getName(), 0,9) ===
'Office365')
321 $userData =
$context->getApiClient()->get(
'me');
322 if (!empty($userData[
'userPrincipalName']))
324 if ($oldConnection = $this->getConnection(
326 Helper::ACCOUNT_TYPE,
327 $userData[
'userPrincipalName']
330 $oldConnection->setDeleted(
false);
331 (
new Core\Mappers\Connection())->
delete(
$connection, [
'softDelete' =>
false]);
336 $connection->setName($userData[
'userPrincipalName']);
344 }
catch (Exception $e) {
360 private function getConnection(Role $owner,
string $serviceName,
string $name): ?Connection
364 return (
new Core\Mappers\Connection())->getMap([
365 '=ENTITY_TYPE' => $owner->getType(),
366 '=ENTITY_ID' => $owner->getId(),
367 '=ACCOUNT_TYPE' => $serviceName,
371 catch (BaseException|ArgumentException|SystemException $e)
386 (
new ConnectionManager())->disableConnection(
$connection);
388 NotificationManager::sendRollbackSyncNotification(
397 private function isPushEnabled(): bool
static getConnectionContext(Connection $connection)