Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
apiservice.php
1<?php
2
4
15use Bitrix\Calendar\Sync\Internals\HasContextTrait;
22use CCalendar;
23use Generator;
24
26{
27 use HasContextTrait;
28
30 private $helper;
32 private $apiClient;
33
42 public function __construct(ContextInterface $context)
43 {
45 $this->context = $context;
46 $this->apiClient = $this->context->getApiClient();
47 $this->helper = $this->context->getHelper();
48 }
49
62 public function getCalendarList(array $params): array
63 {
64 $response = $this->apiClient->get('me/calendars', $params);
65
66 return (array) $response['value'];
67 }
68
81 public function getEventList(array $params): array
82 {
83 $response = $this->apiClient->get(
84 'me/calendars/' . $params['filter']['section_id'] . '/events',
85 $params
86 );
87
88 return (array) $response['value'];
89 }
90
104 public function createSection(SectionDto $sectionDto): array
105 {
106 return $this->apiClient->post('me/calendars?', array_filter($sectionDto->toArray()));
107 }
108
121 public function updateSection(SectionDto $sectionDto): array
122 {
123 return $this->apiClient->patch('me/calendars/' . $sectionDto->id, array_filter($sectionDto->toArray()));
124 }
125
139 public function createEvent(EventDto $eventDto, string $sectionId): array
140 {
141 return $this->apiClient->post(
142 'me/calendars/' . $sectionId . '/events',
143 array_filter($eventDto->toArray(true), static function ($val) {
144 return $val !== [] && $val !== null;
145 })
146 );
147 }
148
161 public function getCalendarDelta(SectionConnection $sectionConnection): Generator
162 {
163 $baseUri = 'me/calendars/' . $sectionConnection->getVendorSectionId() . '/calendarView/delta?';
164 $breakingFlag = false;
165 do {
166 $uri = $this->getDeltaUri($sectionConnection, $baseUri);
167
168 try
169 {
170 $response = $this->apiClient->get($uri);
171 if (!empty($response))
172 {
173 $breakingFlag = $this->processResponseAfterDelta($sectionConnection, $response);
174 }
175 if (!empty($response['value']))
176 {
177 yield $response['value'];
178 }
179 else
180 {
181 break;
182 }
183 }
184 catch(GoneException $e)
185 {
186 if ($sectionConnection->getPageToken())
187 {
188 $sectionConnection->setPageToken(null);
189 }
190 elseif ($sectionConnection->getSyncToken())
191 {
192 $sectionConnection->setSyncToken(null);
193 }
194 }
195 }
196 while(!$breakingFlag);
197 }
198
205 private function getUriParam(string $uri, string $name): ?string
206 {
207 $result = null;
208 if ($urlData = parse_url($uri))
209 {
210 parse_str($urlData['query'], $params);
211 $result = $params[$name] ?? null;
212 }
213
214 return $result;
215 }
216
229 public function getEvent(array $params): array
230 {
231 return $this->apiClient->get(
232 'me/events/' . $params['filter']['event_id'],
233 $params
234 );
235 }
236
250 public function updateEvent(EventDto $eventDto, string $vendorEventId): array
251 {
252 if ($eventDto->isCancelled)
253 {
254 $response = $this->apiClient->post(
255 'me/events/' . $vendorEventId . '/cancel',
256 [
257 'Comment' => 'Deleted from Bitrix',
258 ]
259 );
260 }
261 else
262 {
263 try
264 {
265 $response = $this->apiClient->patch(
266 'me/events/' . $vendorEventId,
267 array_filter($eventDto->toArray(true), static function ($val) {
268 return $val !== [] && $val !== null;
269 })
270 );
271 }
273 {
274 return [];
275 }
276 }
277
278 return $response;
279 }
280
293 public function getEventInstances(array $params): array
294 {
295 return $this->apiClient->get(
296 'me/events/' . $params['filter']['event_id'] . '/instances',
297 [
298 'startDateTime' => $params['filter']['from'],
299 'endDateTime' => $params['filter']['to'],
300 ]
301 );
302 }
303
316 public function deleteEvent(string $vendorEventId)
317 {
318 try
319 {
320 $this->apiClient->delete(
321 'me/events/' . $vendorEventId,
322 );
323 }
324 catch (NotFoundException $exception)
325 {
326 return;
327 }
328 }
329
342 public function deleteSection(string $vendorSectionId)
343 {
344 $this->apiClient->delete(
345 'me/calendars/' . $vendorSectionId,
346 );
347 }
348
362 public function addSectionSubscription(string $vendorSectionId, string $state = ''): array
363 {
364 $data = [
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',
371 ];
372
373 return $this->apiClient->post('subscriptions', $data);
374 }
375
388 public function renewSectionSubscription(string $subscriptionId): array
389 {
390 return $this->apiClient->patch('subscriptions/' . $subscriptionId, [
391 'expirationDateTime' => $this->getExpirationDateTime(),
392 ]);
393 }
394
406 public function deleteSectionSubscription(string $subscriptionId): array
407 {
408 try
409 {
410 return $this->apiClient->delete('subscriptions/' . $subscriptionId);
411 }
412 catch (NotFoundException|AuthException $exception)
413 {
414 return [];
415 }
416 }
417
423 public function getDeltaUri(SectionConnection $sectionConnection, string $baseUri): string
424 {
425 if ($sectionConnection->getPageToken())
426 {
427 $uri = $baseUri . '$skiptoken=' . $sectionConnection->getPageToken();
428 }
429 elseif ($sectionConnection->getSyncToken())
430 {
431 $uri = $baseUri . '$deltatoken=' . $sectionConnection->getSyncToken();
432 }
433 else
434 {
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);
438 }
439 return $uri;
440 }
441
448 private function processResponseAfterDelta(SectionConnection $sectionConnection, array $response): bool
449 {
450 $sectionConnection->setLastSyncStatus(Dictionary::SYNC_STATUS['success']);
451 $breakingFlag = true;
452
453 if ($token = $this->getPageToken($response))
454 {
455 $sectionConnection->setPageToken($token);
456 $breakingFlag = false;
457 }
458 elseif ($token = $this->getSyncToken($response))
459 {
460 $sectionConnection->setPageToken(null);
461 $sectionConnection->setSyncToken($token);
462 }
463 else
464 {
465 $sectionConnection->setPageToken(null);
466 $sectionConnection->setSyncToken(null);
467 }
468
469 return $breakingFlag;
470 }
471
477 private function getPageToken(array $response): ?string
478 {
479 return !empty($response['@odata.nextLink'])
480 ? $this->getUriParam($response['@odata.nextLink'], '$skiptoken')
481 : null
482 ;
483 }
489 private function getSyncToken(array $response): ?string
490 {
491 return !empty($response['@odata.deltaLink'])
492 ? $this->getUriParam($response['@odata.deltaLink'], '$deltatoken')
493 : null
494 ;
495 }
496
500 private function getNotificationUrl(): string
501 {
502 return str_replace('http:', 'https:', CCalendar::GetServerPath())
503 . $this->helper::PUSH_PATH;
504 }
505
509 private function getExpirationDateTime(): string
510 {
511 $time = time() + 70 * 60 * 60;
512 return date("c", $time);
513 }
514}
getCalendarDelta(SectionConnection $sectionConnection)
renewSectionSubscription(string $subscriptionId)
deleteSection(string $vendorSectionId)
updateEvent(EventDto $eventDto, string $vendorEventId)
getDeltaUri(SectionConnection $sectionConnection, string $baseUri)
addSectionSubscription(string $vendorSectionId, string $state='')
deleteSectionSubscription(string $subscriptionId)
createEvent(EventDto $eventDto, string $sectionId)
toArray(bool $filterEmptyValue=false)
Definition dto.php:33