Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dataexchangemanager.php
1<?php
2
4
11use Bitrix\Calendar\Core;
25
27{
28 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 $connections = self::getConnections();
134 while ($connection = $connections->fetch())
135 {
136 if ($connection->getOwner() === null)
137 {
138 self::markDeletedFailedConnection($connection);
139 continue;
140 }
141
142 try
143 {
145 $factory = FactoriesCollection::createByConnection($connection)->fetch();
146
147 if (!$factory)
148 {
149 continue;
150 }
151
152 $exchangeManager = new VendorDataExchangeManager($factory, self::getSyncSectionMap($factory));
153 $exchangeManager
154 ->importSections()
155 ->importEvents()
156 ->updateConnection($factory->getConnection())
157 ->clearCache();
158 }
159 catch (RemoteAccountException $e)
160 {
161 self::markDeletedFailedConnection($connection);
162 }
163 catch (\Exception $e)
164 {
165 $connection->setSyncStatus(Dictionary::SYNC_STATUS['failed']);
166 }
167 }
168
169 return "\\Bitrix\\Calendar\\Sync\\Managers\\DataExchangeManager::importAgent();";
170 }
171
178 private static function getConnections(): Core\Base\Map
179 {
181 $mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
182
183 return $mapperFactory->getConnection()->getMap(
184 [
185 '=ACCOUNT_TYPE' => [
186 Google\Helper::GOOGLE_ACCOUNT_TYPE_API,
187 Office365\Helper::ACCOUNT_TYPE
188 ],
189 '=ENTITY_TYPE' => Core\Role\User::TYPE,
190 '=IS_DELETED' => 'N',
191 ],
192 self::COUNT_CONNECTIONS_FOR_REGULAR_SYNC,
193 ['SYNCHRONIZED' => 'ASC']
194 );
195 }
196
206 private static function getSyncSectionMap(FactoryBase $factory): SyncSectionMap
207 {
208 return (new SyncSectionFactory())->getSyncSectionMapByFactory($factory);
209 }
210}