1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
startsynchronizationmanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Google;
4
5use Bitrix\Calendar\Core\Base\BaseException;
6use Bitrix\Calendar\Core\Base\Result;
7use Bitrix\Calendar\Core\Mappers;
8use Bitrix\Calendar\Core\Role\Helper;
9use Bitrix\Calendar\Core\Role\Role;
10use Bitrix\Calendar\Core\Role\User;
11use Bitrix\Calendar\Integration\Pull\PushCommand;
12use Bitrix\Calendar\Sync\Managers\NotificationManager;
13use Bitrix\Calendar\Sync\Util\HandleStatusTrait;
14use Bitrix\Calendar\Sync\Connection\Connection;
15use Bitrix\Calendar\Sync\Factories\SyncSectionFactory;
16use Bitrix\Calendar\Sync\Handlers\MasterPushHandler;
17use Bitrix\Calendar\Sync\Managers;
18use Bitrix\Calendar\Sync\Managers\OutgoingManager;
19use Bitrix\Calendar\Sync\Managers\StartSynchronization;
20use Bitrix\Calendar\Sync\Managers\VendorDataExchangeManager;
21use Bitrix\Calendar\Util;
22use Bitrix\Main\ArgumentException;
23use Bitrix\Main\DI\ServiceLocator;
24use Bitrix\Main\ObjectException;
25use Bitrix\Main\ObjectNotFoundException;
26use Bitrix\Main\ObjectPropertyException;
27use Bitrix\Main\SystemException;
28use Exception;
29use Psr\Container\NotFoundExceptionInterface;
30
32{
33 use HandleStatusTrait;
34
35 private Role $user;
39 private Mappers\Factory $mapperFactory;
40 private Connection $connection;
41 private static array $outgoingManagersCache = [];
42
52 public function __construct($userId)
53 {
55 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
56 }
57
64 public function synchronize(): array
65 {
66 $response = [
67 'status' => 'error',
68 'message' => 'Could not finish sync.',
69 ];
70
71 $owner = Helper::getRole(\CCalendar::GetUserId(), User::TYPE);
72 $pusher = static function ($result) use ($owner)
73 {
75 PushCommand::ProcessSyncConnection,
76 $owner->getId(),
78 );
79
80 if ($result['stage'] === 'export_finished')
81 {
82 NotificationManager::addFinishedSyncNotificationAgent(
83 $owner->getId(),
84 $result['vendorName']
85 );
86 }
87 };
88
89 try
90 {
91 if ($connection = $this->addStatusHandler($pusher)->start())
92 {
93 $response = [
94 'status' => 'success',
95 'message' => 'CONNECTION_CREATED',
96 'connectionId' => $connection->getId(),
97 ];
98 }
99 }
100 catch (BaseException $e)
101 {
102 }
103
104 return $response;
105 }
106
112 public function start(): ?Connection
113 {
114 $this->connection = $connection = $this->createConnection($this->mapperFactory->getConnection());
115 $this->sendResult(MasterPushHandler::MASTER_STAGE[0]);
116
117 $factory = new Factory($this->connection);
118 $exchangeManager = new VendorDataExchangeManager(
119 $factory,
120 (new SyncSectionFactory())->getSyncSectionMapByFactory($factory)
121 );
122 $exchangeManager
123 ->addStatusHandlerList($this->getStatusHandlerList())
124 ->exchange();
125
126 // TODO: this results must be sent from $exchangeManager,
127 // but it looks like it's not happening
128 $this->sendResult(MasterPushHandler::MASTER_STAGE[2]);
129 $this->sendResult(MasterPushHandler::MASTER_STAGE[3]);
130
131 $this->initSubscription($connection);
132
133 return $connection;
134 }
135
144 public function createConnection(Mappers\Connection $mapper): Connection
145 {
146 $connectionManager = new Managers\ConnectionManager();
147 $connections = $connectionManager->getConnectionsData($this->user, [Factory::SERVICE_NAME]);
148 $connectionManager->deactivateConnections($connections);
149
150 $connection = (new Builders\BuilderConnectionFromExternalData($this->user))->build();
151 $factory = new Factory($connection);
153 $nameResult = $factory->getImportManager()->requestConnectionId();
154
155 if (!$nameResult->isSuccess() || empty($nameResult->getData()['id']))
156 {
157 throw new BaseException('Can not connect with google');
158 }
159
160 $name = $nameResult->getData()['id'];
161 $connectionMap = $mapper->getMap([
162 '%=NAME' => '%'. $name .'%',
163 '=ENTITY_ID' => $this->user->getId(),
164 '=ACCOUNT_TYPE' => Factory::SERVICE_NAME,
165 ], null, ['ID' => 'ASC']);
166
167 $currentConnection = $connectionMap->fetch();
168
169 if ($currentConnection && $duplicatedConnection = $connectionMap->fetch())
170 {
171 $this->deleteConnectionData($duplicatedConnection->getId());
172 }
173
174 $connection->setName($name);
175
176 if ($currentConnection)
177 {
178 $currentConnection
179 ->setDeleted(false)
180 ->setName($name)
181 ;
182 $mapper->update($currentConnection);
183
184 return $currentConnection;
185 }
186
187 return $mapper->create($connection);
188 }
189
195 public function sendPushNotification(Connection $connection): void
196 {
197 (new MasterPushHandler($this->user, 'google', $connection->getName()))(MasterPushHandler::MASTER_STAGE[0]);
198 }
199
205 private function sendResult(string $stage): void
206 {
207 $this->sendStatus([
208 'vendorName' => 'google',
209 'accountName' => $this->connection->getName(),
210 'stage' => $stage,
211 ]);
212 }
213
224 public function initSubscription(Connection $connection): void
225 {
226 $links = $this->mapperFactory->getSectionConnection()->getMap([
227 '=CONNECTION_ID' => $connection->getId(),
228 '=ACTIVE' => 'Y'
229 ]);
230
231 $manager = $this->getOutgoingManager($connection);
232 foreach ($links as $link)
233 {
234 $manager->subscribeSection($link);
235 }
236
237 $manager->subscribeConnection();
238 }
239
247 private function getOutgoingManager(Connection $connection)
248 {
249 if (empty(static::$outgoingManagersCache[$connection->getId()]))
250 {
251 static::$outgoingManagersCache[$connection->getId()] = new OutgoingManager($connection);
252 }
253
254 return static::$outgoingManagersCache[$connection->getId()];
255 }
256
262 private function deleteConnectionData(int $connectionId): void
263 {
264 global $DB;
265 $DB->Query("
266 DELETE FROM b_calendar_event_connection
267 WHERE CONNECTION_ID = " . $connectionId . ";"
268 );
269
270 $DB->Query("
271 DELETE FROM b_calendar_section_connection
272 WHERE CONNECTION_ID = " . $connectionId . ";"
273 );
274
275 $DB->Query("
276 DELETE FROM b_dav_connections
277 WHERE ID = " . $connectionId . ";"
278 );
279 }
280}
$connection
Определения actionsdefinitions.php:38
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getUserRole(int $id)
Определения helper.php:42
static addPullEvent(PushCommand $command, int $userId, array $params=[])
Определения util.php:385
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
global $DB
Определения cron_frame.php:29
$name
Определения menu_edit.php:35
$manager
Определения office365push.php:39
$response
Определения result.php:21