30 public const CREATE_PATH =
'/calendars/%CALENDAR_ID%/events/';
31 public const EVENT_PATH =
'/calendars/%CALENDAR_ID%/events/%EVENT_ID%';
49 $this->httpClient->post(
54 if ($this->isRequestSuccess())
56 $requestResult = $this->prepareResult($this->httpClient->getResult(), $event);
58 $result->setData($requestResult);
62 $response = Json::decode($this->httpClient->getResult());
63 if (!empty($response[
'error']))
65 $error = $response[
'error'];
66 switch ($error[
'code'])
71 $this->handleUnauthorize($this->connection);
72 $result->addError(
new Error($error[
'message'], $error[
'code']));
75 if (!empty($error[
'code']))
77 $result->addError(
new Error($error[
'message'], $error[
'code']));
81 $result->addError(
new Error(
'Uncknown Google API error', 400));
92 return $this->
create($event, $context);
96 $result->addError(
new Error($e->getMessage(), $e->getCode()));
101 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
102 $result->addError(
new Error(
'failed to create an event in google'));
106 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
107 $result->addError(
new Error(
'failed to convert event'));
131 $this->httpClient->query(
137 if ($this->isRequestSuccess())
139 $requestResult = $this->prepareResult($this->httpClient->getResult(), $event);
141 if ($childList = ($context->sync[
'childList'] ??
null))
147 foreach ($childList as $eventId => $childEvent)
150 if ($childResult->isSuccess())
152 $data = $childResult->getData()[
'event'];
155 $link = $mapper->getMap([
156 '=EVENT_ID' => $childEvent->getId(),
157 '=CONNECTION_ID' => $this->connection->getId(),
162 ->setVendorEventId($data[
'id'])
163 ->setEntityTag($data[
'etag'])
164 ->setVendorVersionId($data[
'version'])
165 ->setRecurrenceId($requestResult[
'event'][
'id'])
167 $mapper->update($link);
172 $result->setData($requestResult);
176 $response = Json::decode($this->httpClient->getResult());
177 if (!empty($response[
'error']) && $response[
'error'][
'code'] === 401)
179 $this->handleUnauthorize($this->connection);
180 $result->addError(
new Error($response[
'error'][
'message'], $response[
'error'][
'code']));
184 $result->addError(
new Error(
'error of updating event', $this->httpClient->getStatus()));
188 catch (ArgumentException $e)
190 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
191 $result->addError(
new Error(
'failed to update an event in google'));
193 catch (ObjectException $e)
195 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
196 $result->addError(
new Error(
'failed to convert event'));
218 $this->httpClient->query(
224 if ($this->isRequestDeleteSuccess())
226 if ($response = $this->httpClient->getResult())
228 $requestResult = Json::decode($response);
229 $result->setData($requestResult);
234 $response = Json::decode($this->httpClient->getResult());
235 if (!empty($response[
'error']) && $response[
'error'][
'code'] === 401)
237 $this->handleUnauthorize($this->connection);
238 $result->addError(
new Error($response[
'error'][
'message'], $response[
'error'][
'code']));
242 $result->addError(
new Error(
'error of deleting event'));
249 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
250 $result->addError(
new Error(
'failed to delete an event in google'));
254 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
255 $result->addError(
new Error(
'failed to convert event'));
271 if ($instanceContext ===
null)
273 AddMessage2Log(
'failed to create instance. id='. $event->getId(),
'calendar', 2,
true);
274 return $result->addError(
new Error(
'failed to create an instance in google'));
281 $this->httpClient->query(
284 $this->encode((
new EventConverter($event, $instanceContext->getEventConnection()))->convertForUpdate())
287 if ($this->isRequestSuccess())
289 $requestResult = $this->prepareResult($this->httpClient->getResult(), $event);
291 $result->setData($requestResult);
295 $response = Json::decode($this->httpClient->getResult());
296 if (!empty($response[
'error']) && $response[
'error'][
'code'] === 401)
298 $this->handleUnauthorize($this->connection);
299 $result->addError(
new Error($response[
'error'][
'message'], $response[
'error'][
'code']));
303 $result->addError(
new Error(
'error of creating instance'));
309 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
310 $result->addError(
new Error(
'failed to create an instance in google'));
322 $eventLink = $context->sync[
'instanceLink'];
328 ->setEventConnection($eventLink)
331 $result = $this->
update($event, $eventContext);
332 if (!$result->isSuccess() && $result->getErrorCollection()->getErrorByCode(403))
340 return (
new Result())->addError(
new Error(
'Not found link for instance'));
355 $excludeDate = $context->sync[
'excludeDate'] ??
null;
356 $originalDate = $context->sync[
'originalDate'] ??
null;
361 if ($instanceContext ===
null)
363 AddMessage2Log(
'failed to create instance. id='. $event->
getId(),
'calendar', 2,
true);
364 return $result->addError(
new Error(
'failed to delete an instance in google'));
369 $this->httpClient->query(
372 $this->encode((
new EventConverter($instance, $instanceContext->getEventConnection()))->convertForDeleteInstance())
375 if ($this->isRequestSuccess())
377 $requestResult = $this->prepareResult($this->httpClient->getResult(), $event);
379 $result->setData($requestResult);
383 $response = Json::decode($this->httpClient->getResult());
384 if (!empty($response[
'error']) && $response[
'error'][
'code'] === 401)
386 $this->handleUnauthorizeException();
389 $result->addError(
new Error(
'error of delete instance'));
395 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
396 $result->addError(
new Error(
'failed to delete an instance in google'));
408 return Server::mapUri(
409 $this->connection->getServer()->getFullPath() . self::CREATE_PATH,
411 '%CALENDAR_ID%' => Server::getEncodePath($context->
getSectionConnection()->getVendorSectionId()),
423 return Server::mapUri(
424 $this->connection->getServer()->getFullPath() . self::EVENT_PATH,
426 '%CALENDAR_ID%' => Server::getEncodePath($context->
getSectionConnection()->getVendorSectionId()),
427 '%EVENT_ID%' => Server::getEncodePath($context->
getEventConnection()->getVendorEventId()),
442 public function saveRecurrence(
450 if ($recurrenceEvent->getEventConnection())
452 $masterResult = $this->updateRecurrenceEntity($recurrenceEvent, $context);
456 $masterResult = $this->createRecurrenceEntity($recurrenceEvent, $sectionConnection, $context);
459 if (!$masterResult->isSuccess())
461 $result->addErrors($masterResult->getErrors());
465 if ($recurrenceEvent->getInstanceMap())
468 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
470 if ($instance->getEventConnection())
472 $instanceResult = $this->updateRecurrenceEntity($instance, $context, $recurrenceEvent->getEventConnection());
476 $instanceResult = $this->createRecurrenceEntity(
480 $recurrenceEvent->getEventConnection()
484 if (!$result->isSuccess())
486 $result->addErrors($instanceResult->getErrors());
510 return $this->saveRecurrence($recurrenceEvent, $sectionConnection, $context);
529 return $this->saveRecurrence($recurrenceEvent, $sectionConnection, $context);
558 ->setEventConnection(
561 $this->getInstanceId($masterVendorEventId, $event)
563 ->setRecurrenceId($masterVendorEventId)
573 private function getInstanceId(
string $masterId,
Event $event): string
575 $base = $masterId .
'_';
595 ?Core\
Base\
Date $originalDate =
null
598 $instanceEvent = clone $event;
600 $instanceEvent->
getStart()->getDate()->setDate(
601 $excludeDate->getYear(),
602 $excludeDate->getMonth(),
603 $excludeDate->getDay()
605 $instanceEvent->getEnd()->getDate()->setDate(
606 $excludeDate->getYear(),
607 $excludeDate->getMonth(),
608 $excludeDate->getDay()
611 ->setOriginalDateFrom($originalDate ?? $event->
getStart())
612 ->setRecurringRule(
null)
615 return $instanceEvent;
629 private function createRecurrenceEntity(
637 $eventContext->merge($context);
639 $eventContext->setSectionConnection($sectionConnection);
643 $eventContext->setEventConnection($masterLink);
644 $result = $this->
createInstance($syncEvent->getEvent(), $eventContext);
648 $result = $this->
create($syncEvent->getEvent(), $eventContext);
651 if ($result->isSuccess())
653 if (!$syncEvent->getEvent()->isDeleted())
655 $link = (
new EventConnection())
656 ->setEvent($syncEvent->getEvent())
657 ->setConnection($sectionConnection->getConnection())
658 ->setVersion($syncEvent->getEvent()->getVersion())
659 ->setVendorEventId($result->getData()[
'event'][
'id'])
660 ->setEntityTag($result->getData()[
'event'][
'etag'])
662 $syncEvent->setEventConnection($link);
682 private function updateRecurrenceEntity(
683 SyncEvent $syncEvent,
685 EventConnection $masterLink =
null
688 $eventContext =
new EventContext();
689 $eventContext->merge($context);
692 $eventContext->setEventConnection($masterLink);
693 $result = $this->
updateInstance($syncEvent->getEvent(), $eventContext);
697 $eventContext->setEventConnection($syncEvent->getEventConnection());
698 $result = $this->
update($syncEvent->getEvent(), $eventContext);
700 if ($result->isSuccess())
702 $syncEvent->getEventConnection()->setEntityTag($result->getData()[
'event'][
'etag']);
711 private function encode(array $event)
713 return Json::encode($event, JSON_UNESCAPED_SLASHES);
724 private function prepareResult(
string $result, Event $event): array
726 $externalEvent = Json::decode($result);
736 'id' => $externalEvent[
'id'],
737 'etag' => $externalEvent[
'etag'],
738 'version' => $externalEvent[
'etag'],
739 'recurrence' => $externalEvent[
'recurringEventId'] ??
null,
743 private function handleUnauthorizeException()
745 $this->handleUnauthorize($this->connection);