45 $this->context = $context;
46 $this->apiClient = $this->context->getApiClient();
47 $this->helper = $this->context->getHelper();
64 $response = $this->apiClient->get(
'me/calendars', $params);
66 return (array) $response[
'value'];
83 $response = $this->apiClient->get(
84 'me/calendars/' . $params[
'filter'][
'section_id'] .
'/events',
88 return (array) $response[
'value'];
106 return $this->apiClient->post(
'me/calendars?', array_filter($sectionDto->
toArray()));
123 return $this->apiClient->patch(
'me/calendars/' . $sectionDto->id, array_filter($sectionDto->
toArray()));
141 return $this->apiClient->post(
142 'me/calendars/' . $sectionId .
'/events',
143 array_filter($eventDto->
toArray(
true),
static function ($val) {
144 return $val !== [] && $val !==
null;
163 $baseUri =
'me/calendars/' . $sectionConnection->
getVendorSectionId() .
'/calendarView/delta?';
164 $breakingFlag =
false;
166 $uri = $this->
getDeltaUri($sectionConnection, $baseUri);
170 $response = $this->apiClient->get($uri);
171 if (!empty($response))
173 $breakingFlag = $this->processResponseAfterDelta($sectionConnection, $response);
175 if (!empty($response[
'value']))
177 yield $response[
'value'];
196 while(!$breakingFlag);
205 private function getUriParam(
string $uri,
string $name): ?string
208 if ($urlData = parse_url($uri))
210 parse_str($urlData[
'query'], $params);
211 $result = $params[$name] ??
null;
231 return $this->apiClient->get(
232 'me/events/' . $params[
'filter'][
'event_id'],
252 if ($eventDto->isCancelled)
254 $response = $this->apiClient->post(
255 'me/events/' . $vendorEventId .
'/cancel',
257 'Comment' =>
'Deleted from Bitrix',
265 $response = $this->apiClient->patch(
266 'me/events/' . $vendorEventId,
267 array_filter($eventDto->
toArray(
true),
static function ($val) {
268 return $val !== [] && $val !==
null;
295 return $this->apiClient->get(
296 'me/events/' . $params[
'filter'][
'event_id'] .
'/instances',
298 'startDateTime' => $params[
'filter'][
'from'],
299 'endDateTime' => $params[
'filter'][
'to'],
320 $this->apiClient->delete(
321 'me/events/' . $vendorEventId,
344 $this->apiClient->delete(
345 'me/calendars/' . $vendorSectionId,
365 'changeType' =>
'created,updated,deleted',
366 'notificationUrl' => $this->getNotificationUrl(),
367 'resource' =>
"me/calendars/$vendorSectionId/events",
368 'expirationDateTime' => $this->getExpirationDateTime(),
369 'clientState' => $state,
370 'latestSupportedTlsVersion' =>
'v1_2',
373 return $this->apiClient->post(
'subscriptions', $data);
390 return $this->apiClient->patch(
'subscriptions/' . $subscriptionId, [
391 'expirationDateTime' => $this->getExpirationDateTime(),
410 return $this->apiClient->delete(
'subscriptions/' . $subscriptionId);
427 $uri = $baseUri .
'$skiptoken=' . $sectionConnection->
getPageToken();
431 $uri = $baseUri .
'$deltatoken=' . $sectionConnection->
getSyncToken();
435 $interval = $this->helper->getDeltaInterval();
436 $uri = $baseUri .
'startDateTime=' . $interval[
'from']->format($this->helper::TIME_FORMAT_LONG)
437 .
'&endDateTime=' . $interval[
'to']->format($this->helper::TIME_FORMAT_LONG);
448 private function processResponseAfterDelta(
SectionConnection $sectionConnection, array $response): bool
451 $breakingFlag =
true;
453 if ($token = $this->getPageToken($response))
456 $breakingFlag =
false;
458 elseif ($token = $this->getSyncToken($response))
469 return $breakingFlag;
477 private function getPageToken(array $response): ?string
479 return !empty($response[
'@odata.nextLink'])
480 ? $this->getUriParam($response[
'@odata.nextLink'],
'$skiptoken')
489 private function getSyncToken(array $response): ?string
491 return !empty($response[
'@odata.deltaLink'])
492 ? $this->getUriParam($response[
'@odata.deltaLink'],
'$deltatoken')
500 private function getNotificationUrl(): string
502 return str_replace(
'http:',
'https:', CCalendar::GetServerPath())
503 . $this->helper::PUSH_PATH;
509 private function getExpirationDateTime(): string
511 $time = time() + 70 * 60 * 60;
512 return date(
"c", $time);