1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
dataexchangemanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Managers;
4
5use Bitrix\Calendar\Internals\Counter\CounterService;
6use Bitrix\Calendar\Internals\Counter\Event\EventDictionary;
7use Bitrix\Calendar\Sync\Connection\Connection;
8use Bitrix\Calendar\Sync\Dictionary;
9use Bitrix\Calendar\Sync\Entities\SyncSectionMap;
10use Bitrix\Calendar\Sync\Exceptions\RemoteAccountException;
11use Bitrix\Calendar\Sync\Factories\FactoriesCollection;
12use Bitrix\Calendar\Sync\Factories\FactoryBase;
13use Bitrix\Calendar\Core;
14use Bitrix\Calendar\Sync\Factories\SyncSectionFactory;
15use Bitrix\Calendar\Sync\Google;
16use Bitrix\Calendar\Sync\Office365;
17use Bitrix\Main\ArgumentException;
18use Bitrix\Main\DI\ServiceLocator;
19use Bitrix\Main\Loader;
20use Bitrix\Main\LoaderException;
21use Bitrix\Main\ObjectException;
22use Bitrix\Main\ObjectNotFoundException;
23use Bitrix\Main\ObjectPropertyException;
24use Bitrix\Main\Result;
25use Bitrix\Main\SystemException;
26
28{
29 private const COUNT_CONNECTIONS_FOR_REGULAR_SYNC = 10;
30 private FactoriesCollection $factories;
31
35 public function __construct(FactoriesCollection $factories)
36 {
37 $this->factories = $factories;
38 }
39
46 public static function markDeletedFailedConnection(Connection $connection): void
47 {
49 $mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
50 $mapperFactory
51 ->getConnection()
52 ->patch($connection, ['IS_DELETED' => Core\Mappers\Mapper::POSITIVE_ANSWER])
53 ;
54 }
55
65 public function exchange(): Result
66 {
68 foreach ($this->factories as $factory)
69 {
70 if (!$factory)
71 {
72 continue;
73 }
74
75 $exchangeManager = new VendorDataExchangeManager($factory, self::getSyncSectionMap($factory));
76 $exchangeManager->exchange();
77 }
78
79 return new Result();
80 }
81
92 public function import(): Result
93 {
95 foreach ($this->factories as $factory)
96 {
97 $exchangeManager = new VendorDataExchangeManager($factory, self::getSyncSectionMap($factory));
98 $exchangeManager
99 ->importSections()
100 ->importEvents()
101 ->updateConnection($factory->getConnection())
102 ->clearCache()
103 ;
104
105 try
106 {
107 $exchangeManager->renewSubscription($factory->getConnection());
108 }
109 catch (\Exception $e)
110 {
111 }
112 }
113
114 return new Result();
115 }
116
125 public static function importAgent(): string
126 {
127 if (!Loader::includeModule('calendar') || !Loader::includeModule('dav'))
128 {
129 return "\\Bitrix\\Calendar\\Sync\\Managers\\DataExchangeManager::importAgent();";
130 }
131
132 $userIds = [];
133 $connections = self::getConnections();
135 while ($connection = $connections->fetch())
136 {
137 if ($connection->getOwner() === null)
138 {
139 self::markDeletedFailedConnection($connection);
140 continue;
141 }
142
143 $ownerId = $connection->getOwner()->getId();
144 if ($ownerId)
145 {
146 $userIds[] = $ownerId;
147 }
148
149 try
150 {
152 $factory = FactoriesCollection::createByConnection($connection)->fetch();
153
154 if (!$factory)
155 {
156 continue;
157 }
158
159 $exchangeManager = new VendorDataExchangeManager($factory, self::getSyncSectionMap($factory));
160 $exchangeManager
161 ->importSections()
162 ->importEvents()
163 ->updateConnection($factory->getConnection())
164 ->clearCache();
165 }
166 catch (RemoteAccountException $e)
167 {
168 self::markDeletedFailedConnection($connection);
169 }
170 catch (\Exception $e)
171 {
172 $connection->setSyncStatus(Dictionary::SYNC_STATUS['failed']);
173 }
174 }
175
176 CounterService::addEvent(EventDictionary::SYNC_CHANGED, ['user_ids' => $userIds]);
177
178 return "\\Bitrix\\Calendar\\Sync\\Managers\\DataExchangeManager::importAgent();";
179 }
180
187 private static function getConnections(): Core\Base\Map
188 {
190 $mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
191
192 return $mapperFactory->getConnection()->getMap(
193 [
194 '=ACCOUNT_TYPE' => [
195 Google\Helper::GOOGLE_ACCOUNT_TYPE_API,
196 Office365\Helper::ACCOUNT_TYPE
197 ],
198 '=ENTITY_TYPE' => Core\Role\User::TYPE,
199 '=IS_DELETED' => 'N',
200 ],
201 self::COUNT_CONNECTIONS_FOR_REGULAR_SYNC,
202 ['SYNCHRONIZED' => 'ASC']
203 );
204 }
205
215 private static function getSyncSectionMap(FactoryBase $factory): SyncSectionMap
216 {
217 return (new SyncSectionFactory())->getSyncSectionMapByFactory($factory);
218 }
219}
$connection
Определения actionsdefinitions.php:38
const POSITIVE_ANSWER
Определения mapper.php:13
const SYNC_STATUS
Определения dictionary.php:16
__construct(FactoriesCollection $factories)
Определения dataexchangemanager.php:35