Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
startsynchronizationmanager.php
1<?php
2
4
13use Bitrix\Calendar\Sync\Util\HandleStatusTrait;
27use Exception;
28use Psr\Container\NotFoundExceptionInterface;
29
31{
33
34 private Role $user;
38 private Mappers\Factory $mapperFactory;
39 private Connection $connection;
40 private static array $outgoingManagersCache = [];
41
51 public function __construct($userId)
52 {
53 $this->user = \Bitrix\Calendar\Core\Role\Helper::getUserRole($userId);
54 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
55 }
56
63 public function synchronize(): array
64 {
65 $response = [
66 'status' => 'error',
67 'message' => 'Could not finish sync.',
68 ];
69
70 $owner = Helper::getRole(\CCalendar::GetUserId(), User::TYPE);
71 $pusher = static function ($result) use ($owner)
72 {
74 'process_sync_connection',
75 $owner->getId(),
76 (array) $result
77 );
78
79 if ($result['stage'] === 'export_finished')
80 {
82 $owner->getId(),
83 $result['vendorName']
84 );
85 }
86 };
87
88 try
89 {
90 if ($connection = $this->addStatusHandler($pusher)->start())
91 {
92 $response = [
93 'status' => 'success',
94 'message' => 'CONNECTION_CREATED',
95 'connectionId' => $connection->getId(),
96 ];
97 }
98 }
99 catch (BaseException $e)
100 {
101 }
102
103 return $response;
104 }
105
111 public function start(): ?Connection
112 {
113 $this->connection = $connection = $this->createConnection($this->mapperFactory->getConnection());
114 $this->sendResult(MasterPushHandler::MASTER_STAGE[0]);
115
116 $factory = new Factory($this->connection);
117 $exchangeManager = new VendorDataExchangeManager(
118 $factory,
119 (new SyncSectionFactory())->getSyncSectionMapByFactory($factory)
120 );
121 $exchangeManager
122 ->addStatusHandlerList($this->getStatusHandlerList())
123 ->exchange();
124
125 // TODO: this results must be sent from $exchangeManager,
126 // but it looks like it's not happening
127 $this->sendResult(MasterPushHandler::MASTER_STAGE[2]);
128 $this->sendResult(MasterPushHandler::MASTER_STAGE[3]);
129
130 $this->initSubscription($connection);
131
132 return $connection;
133 }
134
143 public function createConnection(Mappers\Connection $mapper): Connection
144 {
145 $connection = (new Builders\BuilderConnectionFromExternalData($this->user))->build();
146 $factory = new Factory($connection);
148 $nameResult = $factory->getImportManager()->requestConnectionId();
149
150 if (!$nameResult->isSuccess() || empty($nameResult->getData()['id']))
151 {
152 throw new BaseException('Can not connect with google');
153 }
154
155 $name = $nameResult->getData()['id'];
156 $connectionMap = $mapper->getMap([
157 '%=NAME' => '%'. $name .'%',
158 '=ENTITY_ID' => $this->user->getId(),
159 '=ACCOUNT_TYPE' => Factory::SERVICE_NAME,
160 ], null, ['ID' => 'ASC']);
161
162 $currentConnection = $connectionMap->fetch();
163
164 if ($currentConnection && $duplicatedConnection = $connectionMap->fetch())
165 {
166 $this->deleteConnectionData($duplicatedConnection->getId());
167 }
168
169 $connection->setName($name);
170
171 if ($currentConnection)
172 {
173 $currentConnection
174 ->setDeleted(false)
175 ->setName($name)
176 ;
177 $mapper->update($currentConnection);
178
179 return $currentConnection;
180 }
181
182 return $mapper->create($connection);
183 }
184
190 public function sendPushNotification(Connection $connection): void
191 {
192 (new MasterPushHandler($this->user, 'google', $connection->getName()))(MasterPushHandler::MASTER_STAGE[0]);
193 }
194
200 private function sendResult(string $stage): void
201 {
202 $this->sendStatus([
203 'vendorName' => 'google',
204 'accountName' => $this->connection->getName(),
205 'stage' => $stage,
206 ]);
207 }
208
219 public function initSubscription(Connection $connection): void
220 {
221 $links = $this->mapperFactory->getSectionConnection()->getMap([
222 '=CONNECTION_ID' => $connection->getId(),
223 '=ACTIVE' => 'Y'
224 ]);
225
226 $manager = $this->getOutgoingManager($connection);
227 foreach ($links as $link)
228 {
229 $manager->subscribeSection($link);
230 }
231
232 $manager->subscribeConnection();
233 }
234
242 private function getOutgoingManager(Connection $connection)
243 {
244 if (empty(static::$outgoingManagersCache[$connection->getId()]))
245 {
246 static::$outgoingManagersCache[$connection->getId()] = new OutgoingManager($connection);
247 }
248
249 return static::$outgoingManagersCache[$connection->getId()];
250 }
251
257 private function deleteConnectionData(int $connectionId): void
258 {
259 global $DB;
260 $DB->Query("
261 DELETE FROM b_calendar_event_connection
262 WHERE CONNECTION_ID = " . $connectionId . ";"
263 );
264
265 $DB->Query("
266 DELETE FROM b_calendar_section_connection
267 WHERE CONNECTION_ID = " . $connectionId . ";"
268 );
269
270 $DB->Query("
271 DELETE FROM b_dav_connections
272 WHERE ID = " . $connectionId . ";"
273 );
274 }
275}
update(Core\Base\EntityInterface $entity, array $params=[])
Definition mapper.php:99
static addFinishedSyncNotificationAgent(int $userId, string $vendorName)
static addPullEvent(string $command, int $userId, array $params=[])
Definition util.php:373
addStatusHandler(callable $handler)