Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
office365context.php
1<?php
3
19use CSocServOffice365OAuth;
20use Psr\Container\NotFoundExceptionInterface;
21use Psr\Log\LoggerInterface;
22use Psr\Log\NullLogger;
23
28{
30 private static array $instances = [];
31
33 private EventManager $eventManager;
35 private $helper;
37 private VendorSyncService $syncService;
39 private Role $owner;
41 private ApiClient $apiClient;
43 private ApiService $apiService;
45 private Connection $connection;
46
48 private IncomingSectionManagerInterface $incomingManager;
50 private OutgoingEventManagerInterface $outgoingEventManager;
51 private Converter $converter;
52 private PushManager $pushManager;
53
62 public static function getConnectionContext(Connection $connection): Office365Context
63 {
64 if (!array_key_exists($connection->getId(), self::$instances))
65 {
66 self::$instances[$connection->getId()] = new self($connection);
67 }
68 return self::$instances[$connection->getId()];
69 }
70
77 protected function __construct(Connection $connection)
78 {
79 $this->connection = $connection;
80 $this->owner = $connection->getOwner();
81 $this->helper = ServiceLocator::getInstance()->get('calendar.service.office365.helper');
82 }
83
87 public function getEventManager(): EventManager
88 {
89 if (empty($this->eventManager))
90 {
91 $this->eventManager = new EventManager($this);
92 }
93
94 return $this->eventManager;
95 }
96
106 {
107 if (empty($this->syncService))
108 {
109 $this->syncService = new VendorSyncService($this);
110 }
111
112 return $this->syncService;
113 }
114
123 public function getApiService(): ApiService
124 {
125 if(empty($this->apiService))
126 {
127 $this->apiService = new ApiService($this);
128 }
129
130 return $this->apiService;
131 }
132
142 public function getApiClient(): ApiClient
143 {
144 if(empty($this->apiClient))
145 {
146 $httpClient = $this->prepareHttpClient();
147
148 $this->apiClient = new ApiClient($httpClient, $this);
149 }
150
151 return $this->apiClient;
152 }
153
154
155 protected function getMaxPageSize(): ?int
156 {
157 return 100;
158 }
159
163 public function getHelper(): Helper
164 {
165 return $this->helper;
166 }
167
171 public function getConnection(): Connection
172 {
173 return $this->connection;
174 }
175
179 public function getPushManager(): PushManager
180 {
181 if (empty($this->pushManager))
182 {
183 $this->pushManager = new PushManager($this);
184 }
185 return $this->pushManager;
186 }
187
191 public function getConverter(): Converter
192 {
193 if (empty($this->converter))
194 {
195 $this->converter = new Converter($this);
196 }
197 return $this->converter;
198 }
199
208 private function prepareHttpClient(): HttpClient
209 {
210 if (!Loader::includeModule('socialservices'))
211 {
212 throw new LoaderException('Module socialservices is required.');
213 }
214 $httpClient = new HttpClient();
215
216 $oAuthEntity = $this->prepareAuthEntity($this->owner->getId());
217 if ($oAuthEntity->GetAccessToken())
218 {
219 $httpClient->setHeader('Authorization', 'Bearer ' . $oAuthEntity->getToken());
220 $httpClient->setHeader('Content-Type', 'application/json');
221 $httpClient->setHeader('Prefer', 'odata.maxpagesize=' . $this->getMaxPageSize());
222 $httpClient->setRedirect(false);
223 }
224 elseif ($checkUser = $oAuthEntity->GetCurrentUser())
225 {
226 if (!empty($checkUser['access_token']))
227 {
228 $httpClient->setHeader('Authorization', 'Bearer ' . $checkUser['access_token']);
229 $httpClient->setHeader('Content-Type', 'application/json');
230 $httpClient->setHeader('Prefer', 'odata.maxpagesize=' . $this->getMaxPageSize());
231 $httpClient->setRedirect(false);
232 }
233 else
234 {
235 throw new AuthException('Access token not received', 401);
236 }
237 }
238 else
239 {
240 // TODO: maybe move it to the exception handler.
241 // Now it's impossible, because there are many points of call this class
242 (new \Bitrix\Calendar\Core\Mappers\Connection())->update(
243 $this->getConnection()->setDeleted(true)
244 );
245 throw new RemoteAccountException('Office365 account not found', 403);
246 }
247 return $httpClient;
248 }
249
255 public function prepareAuthEntity($userId): \COffice365OAuthInterface
256 {
257 $oauth = new CSocServOffice365OAuth($userId);
258 $oAuthEntity = $oauth->getEntityOAuth();
259 $oAuthEntity->addScope($this->helper::NEED_SCOPE);
260 $oAuthEntity->setUser($this->owner->getId());
261
262 $tokens = $this->getStorageToken($userId);
263 if ($tokens)
264 {
265 $oAuthEntity->setToken($tokens['OATOKEN']);
266 $oAuthEntity->setAccessTokenExpires($tokens['OATOKEN_EXPIRES']);
267 $oAuthEntity->setRefreshToken($tokens['REFRESH_TOKEN']);
268 }
269
270 if (!$oAuthEntity->checkAccessToken())
271 {
272 $oAuthEntity->getNewAccessToken(
273 $oAuthEntity->getRefreshToken(),
274 $this->owner->getId(),
275 true
276 );
277 }
278
279 return $oAuthEntity;
280 }
281
289 public function getStorageToken($userId)
290 {
291 return \Bitrix\Socialservices\UserTable::query()
292 ->setSelect(['USER_ID', 'EXTERNAL_AUTH_ID', 'OATOKEN', 'OATOKEN_EXPIRES', 'REFRESH_TOKEN'])
293 ->where('USER_ID', $userId)
294 ->where('EXTERNAL_AUTH_ID', 'Office365')
295 ->exec()->fetch()
296 ;
297 }
298
299 public function getIncomingManager()
300 {
301 if (empty($this->incomingManager))
302 {
303 $this->incomingManager = new IncomingManager($this);
304 }
305
306 return $this->incomingManager;
307 }
308
310 {
311 if (empty($this->outgoingEventManager))
312 {
313 $this->outgoingEventManager = new OutgoingEventManager($this);
314 }
315 return $this->outgoingEventManager;
316 }
317
321 public function getOwner(): Role
322 {
323 return $this->owner;
324 }
325
329 public function getLogger(): LoggerInterface
330 {
331 if (RequestLogger::isEnabled())
332 {
333 $logger = new RequestLogger($this->getOwner()->getId(), Helper::ACCOUNT_TYPE);
334 }
335 else
336 {
337 $logger = new NullLogger();
338 }
339
340 return $logger;
341 }
342}
static getConnectionContext(Connection $connection)