• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/lib/sync/googleapipush.php
  • Класс: BitrixCalendarSyncGoogleApiPush
  • Вызов: GoogleApiPush::checkPushConnectionChannel
static function checkPushConnectionChannel(array $connectionIds, array $connections): void
{
	$existedConnectionChannels = [];
	$pushConnectionChannelsDb = PushTable::getList(
		[
			'filter' => [
				'=ENTITY_TYPE' => self::TYPE_CONNECTION,
				'=ENTITY_ID' => $connectionIds
			]
		]
	);

	while ($row = $pushConnectionChannelsDb->fetch())
	{
		$channelInfo = null;
		if (!empty($connections[$row['ENTITY_ID']]))
		{
			$connectionData = $connections[$row['ENTITY_ID']];
			if (!self::isConnectionError($connectionData['LAST_RESULT']))
			{
				$existedConnectionChannels[] = $row['ENTITY_ID'];
				continue;
			}

			$googleApiConnection = new GoogleApiSync($connectionData['ENTITY_ID'], $connectionData['ID']);
			if ($googleApiConnection->stopChannel($row['CHANNEL_ID'], $row['RESOURCE_ID']))
			{
				self::deletePushChannel($row);
				$channelInfo = $googleApiConnection->startWatchCalendarList($connectionData['NAME']);
			}

			if (is_string($googleApiConnection->getTransportConnectionError()))
			{
				self::deletePushChannel($row);
			}

			if ($channelInfo && isset($channelInfo['id'], $channelInfo['resourceId']))
			{
				$existedConnectionChannels[] = $row['ENTITY_ID'];
				$googleApiConnection->updateSuccessLastResultConnection();
				PushTable::add([
					'ENTITY_TYPE' => $row['ENTITY_TYPE'],
					'ENTITY_ID' => $row['ENTITY_ID'],
					'CHANNEL_ID' => $channelInfo['id'],
					'RESOURCE_ID' => $channelInfo['resourceId'],
					'EXPIRES' => $channelInfo['expiration'],
					'NOT_PROCESSED' => 'N'
				]);
			}
		}
	}

	//create new channel for connections
	$missedChannelConnections = array_diff($connectionIds, $existedConnectionChannels);
	if (!empty($missedChannelConnections))
	{
		foreach ($missedChannelConnections as $missedConnection)
		{
			if (self::isAuthError($missedConnection['LAST_RESULT']))
			{
				continue;
			}

			$channelInfo = null;
			$connectionData = $connections[$missedConnection];
			$googleApiConnection = new GoogleApiSync($connectionData['ENTITY_ID'], $connectionData['ID']);
			$channelInfo = $googleApiConnection->startWatchCalendarList($connectionData['NAME']);
			if ($channelInfo && isset($channelInfo['id'], $channelInfo['resourceId']))
			{
				$googleApiConnection->updateSuccessLastResultConnection();
				PushTable::add([
					'ENTITY_TYPE' => self::TYPE_CONNECTION,
					'ENTITY_ID' => $connectionData['ID'],
					'CHANNEL_ID' => $channelInfo['id'],
					'RESOURCE_ID' => $channelInfo['resourceId'],
					'EXPIRES' => $channelInfo['expiration'],
					'NOT_PROCESSED' => 'N'
				]);
			}
			else
			{
				$error = $googleApiConnection->getTransportConnectionError();
				if (is_string($error))
				{
					$googleApiConnection->updateLastResultConnection($error);
				}
			}
		}
	}
}