feat: api endpoints and xml sync for contingents data
This commit is contained in:
@@ -12,7 +12,7 @@ use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\BookingSummaryDto;
|
||||
use App\Form\Model\BookingSummaryPricingDto;
|
||||
use App\Form\Model\BookingSummaryVoucherDto;
|
||||
use App\Model\BookingSummaryCmsHotelDto;
|
||||
use App\Model\BookingSummaryCmsHotelData;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
@@ -126,7 +126,7 @@ class BookingSummaryAssembler
|
||||
* Data is cached for 1 hour. This method can be called early in the booking
|
||||
* flow to warm the cache.
|
||||
*/
|
||||
public function getCmsDataForProduct(?string $productCode, ?string $hotelCode): ?BookingSummaryCmsHotelDto
|
||||
public function getCmsDataForProduct(?string $productCode, ?string $hotelCode): ?BookingSummaryCmsHotelData
|
||||
{
|
||||
if (null === $hotelCode) {
|
||||
return null;
|
||||
@@ -144,7 +144,7 @@ class BookingSummaryAssembler
|
||||
// Fetch CMS images (nice to have)
|
||||
$images = $this->cmsDataService->getProductImages($productCode, $hotelCode);
|
||||
|
||||
return new BookingSummaryCmsHotelDto(
|
||||
return new BookingSummaryCmsHotelData(
|
||||
name: $baseHotel?->name,
|
||||
address: $this->formatHotelAddress($baseHotel),
|
||||
images: $images,
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Model\BpnXmlSnapshotRefreshPlan;
|
||||
use App\Model\BpnXmlSnapshotRefreshResult;
|
||||
use App\BusProNet\XmlLoader\TravelLoader;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
final class BpnXmlSnapshotRefreshManager
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TravelLoader $travelLoader,
|
||||
private readonly TravelDataProvider $travelDataService,
|
||||
private readonly TravelSnapshotManager $snapshotService,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function buildPlan(): ?BpnXmlSnapshotRefreshPlan
|
||||
{
|
||||
try {
|
||||
$xmlFileMap = $this->travelLoader->generateFilesMap();
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('Failed to generate XML file map after sync; snapshot refresh skipped', [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$total = array_sum(array_map(fn (array $entry): int => count($entry['hotels']), $xmlFileMap));
|
||||
|
||||
return new BpnXmlSnapshotRefreshPlan($xmlFileMap, $total);
|
||||
}
|
||||
|
||||
public function refresh(
|
||||
BpnXmlSnapshotRefreshPlan $plan,
|
||||
?callable $onProgress = null,
|
||||
): BpnXmlSnapshotRefreshResult {
|
||||
$snapshotResult = $this->travelDataService->syncSnapshotsFromXml(
|
||||
$plan->xmlFileMap,
|
||||
function () use ($onProgress): void {
|
||||
if (null !== $onProgress) {
|
||||
$onProgress();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
$orphanedDeleted = $this->snapshotService->purgeOrphanedFutureSnapshots(array_keys($plan->xmlFileMap));
|
||||
|
||||
return new BpnXmlSnapshotRefreshResult(
|
||||
$snapshotResult['processed'],
|
||||
$snapshotResult['failed'],
|
||||
$orphanedDeleted,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BusProNet\Model\XmlExportInfo;
|
||||
use App\Model\BpnXmlSyncTarget;
|
||||
use League\Flysystem\FilesystemException;
|
||||
use League\Flysystem\FilesystemOperator;
|
||||
use League\Flysystem\StorageAttributes;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||
|
||||
final class BpnXmlSyncManager
|
||||
{
|
||||
private const CACHE_TAGS_TO_INVALIDATE = [
|
||||
'xml-sync',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly FilesystemOperator $xmlSource,
|
||||
private readonly FilesystemOperator $xmlExport,
|
||||
private readonly FilesystemOperator $xmlSourceContingents,
|
||||
private readonly FilesystemOperator $xmlExportContingents,
|
||||
private readonly TagAwareCacheInterface $bpnCache,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<BpnXmlSyncTarget>
|
||||
*/
|
||||
public function getSyncTargets(): array
|
||||
{
|
||||
return [
|
||||
new BpnXmlSyncTarget('travel', $this->xmlSource, $this->xmlExport),
|
||||
new BpnXmlSyncTarget('contingents', $this->xmlSourceContingents, $this->xmlExportContingents),
|
||||
];
|
||||
}
|
||||
|
||||
public function readRemoteInfo(FilesystemOperator $source, string $datasetName): ?XmlExportInfo
|
||||
{
|
||||
try {
|
||||
$content = $source->read(XmlExportInfo::getFilename());
|
||||
|
||||
return XmlExportInfo::fromString($content);
|
||||
} catch (FilesystemException|\InvalidArgumentException $e) {
|
||||
$this->logger->warning('Could not read remote info file, likely being updated', [
|
||||
'dataset' => $datasetName,
|
||||
'exception' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function readLocalInfo(FilesystemOperator $destination): ?XmlExportInfo
|
||||
{
|
||||
try {
|
||||
if (false === $destination->fileExists(XmlExportInfo::getFilename())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$content = $destination->read(XmlExportInfo::getFilename());
|
||||
|
||||
return XmlExportInfo::fromString($content);
|
||||
} catch (FilesystemException|\InvalidArgumentException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{updated:int,deleted:int}
|
||||
*
|
||||
* @throws FilesystemException
|
||||
*/
|
||||
public function syncFiles(
|
||||
FilesystemOperator $source,
|
||||
FilesystemOperator $destination,
|
||||
?callable $onStart = null,
|
||||
?callable $onProgress = null,
|
||||
): array {
|
||||
$remoteFiles = $source
|
||||
->listContents('.')
|
||||
->filter(fn (StorageAttributes $attributes) => $attributes->isFile())
|
||||
->map(fn (StorageAttributes $attributes) => $attributes->path())
|
||||
->toArray();
|
||||
|
||||
$localFiles = $destination
|
||||
->listContents('.')
|
||||
->filter(fn (StorageAttributes $attributes) => $attributes->isFile())
|
||||
->map(fn (StorageAttributes $attributes) => $attributes->path())
|
||||
->toArray();
|
||||
|
||||
$filesToDelete = array_diff($localFiles, $remoteFiles);
|
||||
|
||||
if (null !== $onStart) {
|
||||
$onStart(count($remoteFiles));
|
||||
}
|
||||
|
||||
foreach ($remoteFiles as $path) {
|
||||
$content = $source->read($path);
|
||||
$destination->write($path, $content);
|
||||
if (null !== $onProgress) {
|
||||
$onProgress();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($filesToDelete as $path) {
|
||||
$destination->delete($path);
|
||||
}
|
||||
|
||||
return [
|
||||
'updated' => count($remoteFiles),
|
||||
'deleted' => count($filesToDelete),
|
||||
];
|
||||
}
|
||||
|
||||
public function invalidateCaches(): int
|
||||
{
|
||||
$this->bpnCache->invalidateTags(self::CACHE_TAGS_TO_INVALIDATE);
|
||||
|
||||
return count(self::CACHE_TAGS_TO_INVALIDATE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BusProNet\Model\ContingentCalendarEvent;
|
||||
use App\BusProNet\Model\ContingentDateSummary;
|
||||
use App\BusProNet\Model\ContingentRoomAvailability;
|
||||
use App\BusProNet\Utility\BookingUrlUtility;
|
||||
use App\BusProNet\XmlLoader\ContingentLoader;
|
||||
use App\BusProNet\XmlLoader\TravelLoader;
|
||||
use App\Exception\HotelNotInTravelException;
|
||||
use App\Exception\TravelNotFoundException;
|
||||
|
||||
class ContingentDataService
|
||||
{
|
||||
private const STATUS_ORDER = [
|
||||
'OK' => 0,
|
||||
'ON_REQUEST' => 1,
|
||||
'BLOCKED' => 2,
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly ContingentLoader $contingentLoader,
|
||||
private readonly TravelLoader $travelLoader,
|
||||
private readonly BookingUrlUtility $bookingUrlUtility,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ContingentDateSummary>
|
||||
*
|
||||
* @throws TravelNotFoundException
|
||||
* @throws HotelNotInTravelException
|
||||
*/
|
||||
public function getAvailableContingents(int $hotelId, int $dateId): array
|
||||
{
|
||||
$travel = $this->travelLoader->loadById($dateId, $hotelId);
|
||||
|
||||
if (null === $travel->dateFrom || null === $travel->dateTo) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = $this->contingentLoader->loadByHotelId($hotelId);
|
||||
$rows = $this->filterRowsByRange($data['rows'], $travel->dateFrom, $travel->dateTo);
|
||||
$belKalStatusByDate = $this->getBelKalStatusByDate($rows);
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
if (true === ($row['isControlRoom'] ?? false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dateKey = $row['date']->format('Y-m-d');
|
||||
|
||||
if (false === isset($result[$dateKey])) {
|
||||
$result[$dateKey] = new ContingentDateSummary($dateKey, null, 0, 0);
|
||||
}
|
||||
|
||||
if (null !== $row['minNights']) {
|
||||
$result[$dateKey]->minNights = null === $result[$dateKey]->minNights
|
||||
? $row['minNights']
|
||||
: min($result[$dateKey]->minNights, $row['minNights']);
|
||||
}
|
||||
|
||||
$result[$dateKey]->total += (int) ($row['available'] ?? 0);
|
||||
$result[$dateKey]->capacity += (int) ($row['pax'] ?? 0);
|
||||
}
|
||||
|
||||
foreach ($belKalStatusByDate as $dateKey => $status) {
|
||||
if (false === isset($result[$dateKey])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('OK' !== $status) {
|
||||
$result[$dateKey]->total = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, ContingentRoomAvailability>
|
||||
*
|
||||
* @throws TravelNotFoundException
|
||||
* @throws HotelNotInTravelException
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getAvailableRooms(
|
||||
string $dateFrom,
|
||||
string $dateTo,
|
||||
int $hotelId,
|
||||
int $dateId,
|
||||
?string $myEpUrl = null,
|
||||
): array {
|
||||
$range = $this->parseDateRange($dateFrom, $dateTo);
|
||||
$travel = $this->travelLoader->loadById($dateId, $hotelId);
|
||||
|
||||
if (null !== $travel->dateFrom && null !== $travel->dateTo) {
|
||||
if ($range['from'] < $travel->dateFrom || $range['to'] > $travel->dateTo) {
|
||||
throw new \InvalidArgumentException('Requested range must be within the travel date range');
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->contingentLoader->loadByHotelId($hotelId);
|
||||
$rows = $this->filterRowsByRange($data['rows'], $range['from'], $range['to']);
|
||||
$belKalStatusByDate = $this->getBelKalStatusByDate($rows);
|
||||
|
||||
$nights = $range['from']->diff($range['to'])->days;
|
||||
$bookingUrl = $this->bookingUrlUtility->build($hotelId, $dateId, $myEpUrl);
|
||||
$result = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
if (true === ($row['isControlRoom'] ?? false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dateKey = $row['date']->format('Y-m-d');
|
||||
$status = $row['status'] ?? 'OK';
|
||||
|
||||
if (isset($belKalStatusByDate[$dateKey]) && 'OK' !== $belKalStatusByDate[$dateKey]) {
|
||||
$status = $belKalStatusByDate[$dateKey];
|
||||
}
|
||||
|
||||
$minNights = $row['minNights'] ?? 0;
|
||||
$additionalNightMinPrice = $row['additionalNightMinPrice'] ?? 0.0;
|
||||
$minPrice = $row['minPrice'] ?? 0.0;
|
||||
$extraNights = max(0, $nights - (int) $minNights);
|
||||
|
||||
$priceForSelection = $minPrice + ($extraNights * $additionalNightMinPrice);
|
||||
|
||||
$result[] = new ContingentRoomAvailability(
|
||||
date: $dateKey,
|
||||
roomCode: (string) $row['roomCode'],
|
||||
roomLabel: (string) $row['roomLabel'],
|
||||
bookingUrl: $bookingUrl,
|
||||
pax: (int) ($row['pax'] ?? 0),
|
||||
available: 'OK' === $status ? (int) ($row['available'] ?? 0) : 0,
|
||||
status: $status,
|
||||
minPrice: $row['minPrice'],
|
||||
minNights: $row['minNights'],
|
||||
additionalNightMinPrice: $row['additionalNightMinPrice'],
|
||||
additionalNightMinNights: $row['additionalNightMinNights'],
|
||||
priceForSelection: (float) $priceForSelection,
|
||||
);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, ContingentCalendarEvent>
|
||||
*/
|
||||
public function getCalendarEvents(int $hotelId, string $dateFrom, string $dateTo): array
|
||||
{
|
||||
$range = $this->parseDateRange($dateFrom, $dateTo);
|
||||
$data = $this->contingentLoader->loadByHotelId($hotelId);
|
||||
$rows = $this->filterRowsByRange($data['rows'], $range['from'], $range['to']);
|
||||
|
||||
$events = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$dateKey = $row['date']->format('Y-m-d');
|
||||
|
||||
if (false === isset($events[$dateKey])) {
|
||||
$events[$dateKey] = new ContingentCalendarEvent($dateKey, 'OK', 0, 0);
|
||||
}
|
||||
|
||||
$events[$dateKey]->status = $this->mergeStatus(
|
||||
$events[$dateKey]->status,
|
||||
$row['status'] ?? 'OK'
|
||||
);
|
||||
|
||||
if (false === ($row['isControlRoom'] ?? false)) {
|
||||
$events[$dateKey]->pax += (int) ($row['pax'] ?? 0);
|
||||
$events[$dateKey]->available += (int) ($row['available'] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
ksort($events);
|
||||
|
||||
return array_values($events);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $rows
|
||||
*
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function filterRowsByRange(array $rows, \DateTimeImmutable $dateFrom, \DateTimeImmutable $dateTo): array
|
||||
{
|
||||
return array_values(array_filter($rows, function (array $row) use ($dateFrom, $dateTo) {
|
||||
if (false === isset($row['date']) || !$row['date'] instanceof \DateTimeImmutable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $row['date'] >= $dateFrom && $row['date'] <= $dateTo;
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $rows
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function getBelKalStatusByDate(array $rows): array
|
||||
{
|
||||
$statuses = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
if (false === ($row['isControlRoom'] ?? false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dateKey = $row['date']->format('Y-m-d');
|
||||
$statuses[$dateKey] = $this->mergeStatus($statuses[$dateKey] ?? 'OK', $row['status'] ?? 'OK');
|
||||
}
|
||||
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
private function mergeStatus(string $current, string $candidate): string
|
||||
{
|
||||
$currentOrder = self::STATUS_ORDER[$current] ?? 0;
|
||||
$candidateOrder = self::STATUS_ORDER[$candidate] ?? 0;
|
||||
|
||||
return $candidateOrder > $currentOrder ? $candidate : $current;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{from: \DateTimeImmutable, to: \DateTimeImmutable}
|
||||
*/
|
||||
private function parseDateRange(string $dateFrom, string $dateTo): array
|
||||
{
|
||||
$from = $this->parseDate($dateFrom);
|
||||
$to = $this->parseDate($dateTo);
|
||||
|
||||
if ($to < $from) {
|
||||
throw new \InvalidArgumentException('dateTo must be on or after dateFrom');
|
||||
}
|
||||
|
||||
return ['from' => $from, 'to' => $to];
|
||||
}
|
||||
|
||||
private function parseDate(string $date): \DateTimeImmutable
|
||||
{
|
||||
$parsed = \DateTimeImmutable::createFromFormat('Y-m-d', $date);
|
||||
|
||||
if (false === $parsed || $parsed->format('Y-m-d') !== $date) {
|
||||
throw new \InvalidArgumentException('Invalid date format, expected Y-m-d');
|
||||
}
|
||||
|
||||
return $parsed->setTime(0, 0, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user