• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/lib/sync/managers/vendorsynchronization.php
  • Класс: BitrixCalendarSyncManagersVendorSynchronization
  • Вызов: VendorSynchronization::createSection
public function createSection(Section $section, SectionContext $context): Result
{
	$mainResult = new Result();
	$resultData = [];
	$factory = $this->factory;
	$manager = $factory->getSectionManager();
	$context->add('sync', 'connection', $factory->getConnection());

	if ($sectionLink = $context->getSectionConnection())
	{
		$resultData['sectionConnection'] = $sectionLink;
		$status = Dictionary::SYNC_STATUS['update'];
		// TODO: what to do?
	}
	else
	{
		$safeCreate = static function (Section $section, SectionContext $context) use ($manager)
		{
			$result = new Result();
			$counter = 0;
			$originalName = $section->getName();
			do
			{
				try
				{
					$result = $manager->create($section, $context);
					$success = true;
				}
				catch (ConflictException $e)
				{
					$counter++;
					$section->setName($originalName . " ($counter)");
					$success = false;
				}
			}
			while (!$success);

			$section->setName($originalName);

			return $result;
		};

		$result = $safeCreate($section, $context);
		if ($result->isSuccess())
		{
			$status = Dictionary::SYNC_STATUS['success'];
			$sectionLink = (new SectionConnection())
				->setSection($section)
				->setConnection($factory->getConnection())
				->setVendorSectionId($result->getData()['id'])
				->setVersionId($result->getData()['version'])
				->setActive(true)
				->setLastSyncStatus($status)
				->setLastSyncDate(new CoreBaseDate())
			;
			$this->mapperFactory->getSectionConnection()->create($sectionLink);

			$resultData['sectionConnection'] = $sectionLink;
		}
		else
		{
			$mainResult->addErrors($result->getErrors());
			$status = Dictionary::SYNC_STATUS['failed'];
		}
	}

	$resultData[$factory->getConnection()->getVendor()->getCode()] = [
		'status' => $status,
	];

	return $mainResult->setData($resultData);
}