30 private static array $instances = [];
64 if (!array_key_exists($connection->getId(), self::$instances))
66 self::$instances[$connection->getId()] =
new self($connection);
68 return self::$instances[$connection->getId()];
79 $this->connection = $connection;
80 $this->owner = $connection->getOwner();
81 $this->helper = ServiceLocator::getInstance()->get(
'calendar.service.office365.helper');
89 if (empty($this->eventManager))
107 if (empty($this->syncService))
112 return $this->syncService;
125 if(empty($this->apiService))
130 return $this->apiService;
144 if(empty($this->apiClient))
146 $httpClient = $this->prepareHttpClient();
148 $this->apiClient =
new ApiClient($httpClient, $this);
151 return $this->apiClient;
165 return $this->helper;
181 if (empty($this->pushManager))
185 return $this->pushManager;
193 if (empty($this->converter))
197 return $this->converter;
208 private function prepareHttpClient():
HttpClient
210 if (!Loader::includeModule(
'socialservices'))
217 if ($oAuthEntity->GetAccessToken())
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);
224 elseif ($checkUser = $oAuthEntity->GetCurrentUser())
226 if (!empty($checkUser[
'access_token']))
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);
235 throw new AuthException(
'Access token not received', 401);
242 (new \Bitrix\Calendar\Core\Mappers\Connection())->update(
245 throw new RemoteAccountException(
'Office365 account not found', 403);
257 $oauth =
new CSocServOffice365OAuth(
$userId);
258 $oAuthEntity = $oauth->getEntityOAuth();
259 $oAuthEntity->addScope($this->helper::NEED_SCOPE);
260 $oAuthEntity->setUser($this->owner->getId());
265 $oAuthEntity->setToken($tokens[
'OATOKEN']);
266 $oAuthEntity->setAccessTokenExpires($tokens[
'OATOKEN_EXPIRES']);
267 $oAuthEntity->setRefreshToken($tokens[
'REFRESH_TOKEN']);
270 if (!$oAuthEntity->checkAccessToken())
272 $oAuthEntity->getNewAccessToken(
273 $oAuthEntity->getRefreshToken(),
274 $this->owner->getId(),
291 return \Bitrix\Socialservices\UserTable::query()
292 ->setSelect([
'USER_ID',
'EXTERNAL_AUTH_ID',
'OATOKEN',
'OATOKEN_EXPIRES',
'REFRESH_TOKEN'])
294 ->where(
'EXTERNAL_AUTH_ID',
'Office365')
301 if (empty($this->incomingManager))
306 return $this->incomingManager;
311 if (empty($this->outgoingEventManager))
315 return $this->outgoingEventManager;
331 if (RequestLogger::isEnabled())
337 $logger =
new NullLogger();
__construct(Connection $connection)
static getConnectionContext(Connection $connection)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)