• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/lib/sync/google/importmanager.php
  • Класс: BitrixCalendarSyncGoogleImportManager
  • Вызов: ImportManager::getEvents
public function getEvents(SyncSection $syncSection): Result
{
	$map = new SyncEventMap();
	$result = (new Result())->setData([
		'externalSyncEventMap' => $map,
	]);

	try
	{
		// TODO: Remake it: move this logic to parent::request().
		// Or, better, in separate class.
		$this->syncSectionConnection = $syncSection->getSectionConnection();
		$this->httpClient->query(
			HttpClient::HTTP_GET,
			$this->prepareEventListUrl(
				$this->syncSectionConnection->getVendorSectionId(),
				$this->prepareRequestParams($this->syncSectionConnection)
			)
		);

		try
		{
			$externalResult = $this->parseResponse($this->httpClient->getResult());
		}
		catch (Exception $e)
		{
			return $result->addError(new Error($e->getMessage()));
		}


		if ($this->isRequestSuccess())
		{
			$impatientInstanceListByUid = [];

			$this->etag = $externalResult['etag'];
			$this->syncToken = $externalResult['nextSyncToken'] ?? null;
			$this->pageToken = $externalResult['nextPageToken'] ?? null;
			$this->lastSyncStatus = BitrixCalendarSyncDictionary::SYNC_SECTION_ACTION['success'];

			$this->handleSuccessBehavior($externalResult);

			if (!empty($externalResult['items']) && is_array($externalResult['items']))
			{
				foreach ($externalResult['items'] as $item)
				{
					$syncEvent = (new BuilderSyncEventFromExternalData($item, $this->connection, $syncSection))
						->build();

					if ($syncEvent->isInstance() || $syncEvent->getVendorRecurrenceId())
					{
						/** @var SyncEvent $masterEvent */
						$masterEvent = $map->has($syncEvent->getVendorRecurrenceId())
							? $map->getItem($syncEvent->getVendorRecurrenceId())
							: null
						;

						if (!$masterEvent)
						{
							$impatientInstanceListByUid[$syncEvent->getVendorRecurrenceId()][] = $syncEvent;
							continue;
						}

						$masterEvent->addInstance($syncEvent);
					}
					else
					{
						if ($syncEvent->isRecurrence()
							&& ($instanceList = ($impatientInstanceListByUid[$syncEvent->getUid()] ?? null))
						)
						{
							$syncEvent->addInstanceList($instanceList);
							unset($impatientInstanceListByUid[$syncEvent->getUid()]);
						}

						$map->add($syncEvent, $syncEvent->getVendorEventId());
					}
				}
			}

			foreach ($impatientInstanceListByUid as $syncEventList)
			{
				foreach($syncEventList as $syncEvent)
				{
					$map->add($syncEvent, $syncEvent->getVendorEventId());
				}
			}

			return $result;
		}

		$helper = new Helper();

		if ($helper->isNotValidSyncTokenError($this->prepareError($externalResult)))
		{
			$syncSection->getSectionConnection()->setSyncToken(null);

			return $this->getEvents($syncSection);
		}

		if ($helper->isMissingRequiredAuthCredential($this->prepareError($externalResult)))
		{
			$this->handleUnauthorize($this->connection);
			$result->addError(new Error('Auth error on getting events', 401));

			return $result;
		}

		$this->handleErroneousBehavior($syncSection);
	}
	catch (BaseException $e)
	{
		$result->addError(new Error($e->getMessage()));
	}

	return $result;
}