feat: persist travel snapshots and refresh extended availability

This commit is contained in:
Björn Fromme
2026-03-23 17:29:10 +01:00
parent c1ab6d8687
commit bc7fb794bf
31 changed files with 2328 additions and 91 deletions
+4 -4
View File
@@ -112,7 +112,7 @@ class BookingService
* Retrieves the booking DTO from the session and restores the Travel object.
*
* After deserialization the DTO contains only a Travel skeleton with the ID.
* This method replaces it with the full Travel from cache via hydrate().
* This method replaces it with the full Travel via hydrate().
*
* @param Request $request The HTTP request containing session data
* @param string $mode The booking mode (create/edit)
@@ -159,11 +159,11 @@ class BookingService
}
/**
* Restores the full Travel object from cache after session deserialization.
* Restores the full Travel object after session deserialization.
*
* BookingDto::__serialize() replaces Travel with just its ID to keep session
* payloads small. This method fetches the complete Travel from the
* TravelDataService cache and sets it on both the DTO and the Booking reference.
* payloads small. This method fetches the complete Travel (from DB snapshot or
* XML fallback) and sets it on both the DTO and the Booking reference.
*/
private function hydrate(BookingDto $bookingDto): void
{
+126 -60
View File
@@ -7,6 +7,7 @@ namespace App\Service;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\BaseData;
use App\BusProNet\Model\Insurance;
use App\BusProNet\Model\Notification;
use App\BusProNet\Model\ServiceAvailabilityResponse;
use App\BusProNet\Model\Travel;
@@ -17,17 +18,19 @@ use App\BusProNet\XmlLoader\TravelLoader;
use App\Exception\HotelNotFoundException;
use App\Exception\HotelNotInTravelException;
use App\Exception\TravelNotFoundException;
use Flagception\Manager\FeatureManagerInterface;
use Psr\Cache\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
/**
* Unified service for retrieving travel data from both local XML files and remote API.
* Unified service for retrieving travel data from local persisted sources and remote API.
*
* This service provides a unified interface for accessing travel data regardless of source,
* supporting automatic fallback between local XML files and remote API calls. It handles caching,
* error recovery, and data enrichment for both data sources.
* supporting automatic fallback between local persisted data and remote API calls. Local reads
* prefer snapshots for performance and refresh those snapshots from XML during sync. It handles
* caching, error recovery, and data enrichment for both data sources.
*/
class TravelDataService
{
@@ -44,6 +47,8 @@ class TravelDataService
private readonly ApiClient $apiClient,
private readonly CacheInterface $cache,
private readonly LoggerInterface $logger,
private readonly TravelSnapshotService $travelSnapshotService,
private readonly FeatureManagerInterface $featureManager,
private readonly bool $preferRemote = false,
private readonly bool $enableFallback = true,
) {
@@ -53,13 +58,11 @@ class TravelDataService
* Retrieve travel data with automatic source selection and fallback.
*
* Attempts to load travel data from the preferred source first, then falls back
* to the alternative source if the primary fails. Handles caching and enrichment
* of data from both sources.
* to the alternative source if the primary fails.
*
* @param int $dateId The travel date ID to retrieve
* @param int|null $hotelId Optional hotel ID for specific hotel data
* @param bool $preferRemote Whether to prefer remote API over XML for this call
* @param bool $enableCache Whether to use caching for this request
*
* @return Travel|null The travel data or null if not found in any source
*/
@@ -67,30 +70,8 @@ class TravelDataService
int $dateId,
?int $hotelId = null,
?bool $preferRemote = null,
bool $enableCache = true,
): ?Travel {
$preferRemote = $preferRemote ?? $this->preferRemote;
$cacheKey = sprintf('travel_unified_%d_%d_%s', $dateId, $hotelId ?? 0, $preferRemote ? 'remote' : 'local');
if (!$enableCache) {
return $this->loadTravelDataUncached($dateId, $hotelId, $preferRemote);
}
try {
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($dateId, $hotelId, $preferRemote) {
$item->expiresAfter(300); // 5 minutes cache
return $this->loadTravelDataUncached($dateId, $hotelId, $preferRemote);
});
} catch (InvalidArgumentException $e) {
$this->logger->error('Cache error in TravelDataService', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'error' => $e->getMessage(),
]);
return $this->loadTravelDataUncached($dateId, $hotelId, $preferRemote);
}
return $this->loadTravelDataUncached($dateId, $hotelId, $preferRemote ?? $this->preferRemote);
}
/**
@@ -110,13 +91,6 @@ class TravelDataService
$travel = $this->travelLoader->loadById($dateId, $hotelId);
$this->enrichTravelData($travel);
$this->logger->debug('Travel data loaded from XML', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'travelId' => $travel->id,
]);
return $travel;
} catch (TravelNotFoundException $e) {
$this->logger->debug('Travel not found in XML', [
'dateId' => $dateId,
@@ -140,6 +114,75 @@ class TravelDataService
return null;
}
// Persist snapshot separately so a DB/serializer failure does not discard a
// successfully loaded travel.
try {
$this->travelSnapshotService->upsertFromTravel($travel);
} catch (\Throwable $e) {
$this->logger->warning('Failed to persist travel snapshot after XML load', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'error' => $e->getMessage(),
]);
}
$this->logger->debug('Travel data loaded from XML', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'travelId' => $travel->id,
]);
return $travel;
}
/**
* Retrieve travel data from locally persisted sources.
*
* Runtime local reads prefer snapshots for performance and only fall back to XML
* when no snapshot payload is available.
*/
public function getTravelDataFromLocal(int $dateId, ?int $hotelId = null): ?Travel
{
$travel = $this->getTravelDataFromSnapshot($dateId, $hotelId)
?? $this->getTravelDataFromXml($dateId, $hotelId);
if (null !== $travel) {
$this->hydrateInsurancePackageRelationships($travel->insurances);
}
return $travel;
}
private function getTravelDataFromSnapshot(int $dateId, ?int $hotelId): ?Travel
{
if (!$this->featureManager->isActive('travel_snapshot')) {
return null;
}
try {
$travel = $this->travelSnapshotService->loadTravel($dateId, $hotelId);
} catch (\Throwable $e) {
$this->logger->warning('Snapshot lookup failed, falling back to XML', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'error' => $e->getMessage(),
]);
return null;
}
if (null === $travel) {
return null;
}
$this->logger->debug('Travel data loaded from local snapshot', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'travelId' => $travel->id,
]);
return $travel;
}
/**
@@ -200,18 +243,22 @@ class TravelDataService
}
/**
* Check if travel data exists in XML files.
* Check if travel data exists in local persisted sources.
*
* Performs a lightweight check to determine if travel data exists in XML
* files without loading the full travel object.
* Performs a lightweight check to determine if travel data can be served from
* local sources without loading the full travel object.
*
* @param int $dateId The travel date ID to check
* @param int|null $hotelId Optional hotel ID for specific hotel data
*
* @return bool True if travel data exists in XML files
* @return bool True if travel data exists in local sources
*/
public function existsInXml(int $dateId, ?int $hotelId = null): bool
public function existsLocally(int $dateId, ?int $hotelId = null): bool
{
if (true === $this->travelSnapshotService->exists($dateId, $hotelId)) {
return true;
}
$mapping = $this->generateFilesMap();
if (false === isset($mapping[$dateId])) {
@@ -229,7 +276,7 @@ class TravelDataService
/**
* Get information about available data sources for a travel.
*
* Returns information about which data sources (local XML, remote API, or both) have
* Returns information about which data sources (local persisted, remote API, or both) have
* data available for the specified travel.
*
* @param int $dateId The travel date ID to check
@@ -240,7 +287,7 @@ class TravelDataService
public function getAvailableSources(int $dateId, ?int $hotelId = null): array
{
return [
static::SOURCE_LOCAL => $this->existsInXml($dateId, $hotelId),
static::SOURCE_LOCAL => $this->existsLocally($dateId, $hotelId),
static::SOURCE_REMOTE => null !== $this->mapDateIdToProductId($dateId),
];
}
@@ -253,7 +300,7 @@ class TravelDataService
*
* @param int $dateId The travel date ID to retrieve
* @param int|null $hotelId Optional hotel ID for specific hotel data
* @param bool $preferRemote Whether to prefer remote API over XML
* @param bool $preferRemote Whether to prefer remote API over local sources
*
* @return Travel|null The travel data or null if not found
*/
@@ -298,7 +345,7 @@ class TravelDataService
private function loadFromSource(int $dateId, ?int $hotelId, string $source): ?Travel
{
return match ($source) {
self::SOURCE_LOCAL => $this->getTravelDataFromXml($dateId, $hotelId),
self::SOURCE_LOCAL => $this->getTravelDataFromLocal($dateId, $hotelId),
self::SOURCE_REMOTE => $this->getTravelDataFromApi($dateId, $hotelId),
default => null,
};
@@ -317,14 +364,20 @@ class TravelDataService
public function mapDateCodeToId(string $dateCode): ?int
{
try {
$dateId = $this->travelLoader->mapCodeToId($dateCode);
$mapping = $this->generateFilesMap();
$dateCodes = array_column($mapping, 'code', 'id');
$dateId = array_search($dateCode, $dateCodes, true);
if (false === $dateId) {
return null;
}
$this->logger->debug('Date code mapping', [
'dateCode' => $dateCode,
'dateId' => $dateId,
]);
return $dateId;
return (int) $dateId;
} catch (\Exception $e) {
$this->logger->error('Failed to map date code to ID', [
'dateCode' => $dateCode,
@@ -380,7 +433,8 @@ class TravelDataService
public function mapDateIdToProductId(int $dateId): ?int
{
try {
$productId = $this->travelLoader->mapDateIdToProductId($dateId);
$productId = $this->travelSnapshotService->findProductIdByDateId($dateId);
$productId = $productId ?? $this->travelLoader->mapDateIdToProductId($dateId);
$this->logger->debug('Date ID to product ID mapping', [
'dateId' => $dateId,
@@ -409,21 +463,36 @@ class TravelDataService
*/
public function generateFilesMap(): array
{
$mapping = [];
try {
$mapping = $this->travelLoader->generateFilesMap();
$this->logger->debug('Generated files mapping', [
'count' => count($mapping),
]);
return $mapping;
} catch (\Exception $e) {
$this->logger->error('Failed to generate files mapping', [
$this->logger->warning('Failed to generate XML files mapping, fallback to snapshots only', [
'error' => $e->getMessage(),
]);
return [];
}
$snapshotMapping = $this->travelSnapshotService->generateMapping();
foreach ($snapshotMapping as $dateId => $snapshotEntry) {
if (false === isset($mapping[$dateId])) {
$mapping[$dateId] = $snapshotEntry;
continue;
}
foreach ($snapshotEntry['hotels'] as $hotelId => $hotelData) {
if (false === isset($mapping[$dateId]['hotels'][$hotelId])) {
$mapping[$dateId]['hotels'][$hotelId] = $hotelData;
}
}
}
$this->logger->debug('Generated files mapping', [
'count' => count($mapping),
]);
return $mapping;
}
/**
@@ -671,9 +740,6 @@ class TravelDataService
// Keep insurances indexed by ID for efficient lookups
$travel->insurances = $insurances;
// Hydrate package relationships after loading
// Packages lose their containedInsurances during serialization, so rebuild them
$this->hydrateInsurancePackageRelationships($travel->insurances);
} catch (\Exception $e) {
$this->logger->warning('Failed to load insurance data', [
'travelId' => $travel->id,
+376
View File
@@ -0,0 +1,376 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\ExtendedAvailability;
use App\BusProNet\Model\ExtendedServiceAvailabilityResponse;
use App\BusProNet\Model\Notification;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\BusProNet\Utility\DayTimeUtility;
use App\Entity\TravelSnapshot;
use App\Repository\TravelSnapshotRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Application service for DB-backed travel snapshots.
*
* Responsibilities:
* - Store and load Travel graphs as JSON payloads.
* - Expose snapshot-derived mapping/product lookup helpers.
* - Enrich snapshots with extended availability data.
* - Purge expired snapshot records.
*/
class TravelSnapshotService
{
public function __construct(
private readonly TravelSnapshotRepository $snapshotRepository,
private readonly EntityManagerInterface $entityManager,
private readonly ApiClient $apiClient,
private readonly LoggerInterface $logger,
private readonly SerializerInterface $serializer,
private readonly int $retentionBufferDays = 14,
) {
}
/**
* Checks whether a snapshot exists for the given date/hotel combination.
*/
public function exists(int $dateId, ?int $hotelId = null): bool
{
return null !== $this->findSnapshot($dateId, $hotelId);
}
/**
* Loads a Travel aggregate from snapshot payload.
*
* Returns null when no snapshot exists or payload deserialization fails.
*/
public function loadTravel(int $dateId, ?int $hotelId = null): ?Travel
{
$snapshot = $this->findSnapshot($dateId, $hotelId);
if (null === $snapshot) {
return null;
}
try {
$travel = $this->serializer->deserialize(
$snapshot->getPayload(),
Travel::class,
'json'
);
} catch (\Throwable $e) {
$this->logger->warning('Snapshot payload cannot be deserialized to Travel', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'snapshotId' => $snapshot->getId(),
'error' => $e->getMessage(),
]);
return null;
}
if (false === $travel instanceof Travel) {
$this->logger->warning('Snapshot payload deserialization returned unexpected type', [
'dateId' => $dateId,
'hotelId' => $hotelId,
'snapshotId' => $snapshot->getId(),
'type' => get_debug_type($travel),
]);
return null;
}
return $travel;
}
/**
* Creates or updates a snapshot from a Travel aggregate.
*
* Uses payload hash comparison to skip unnecessary writes.
*/
public function upsertFromTravel(Travel $travel): void
{
if (null === $travel->id || null === $travel->hotelId) {
return;
}
$payload = $this->serializer->serialize($travel, 'json');
$payloadHash = hash('sha256', $payload);
$snapshot = $this->snapshotRepository->findByDateAndHotel($travel->id, $travel->hotelId);
if (null === $snapshot) {
$snapshot = new TravelSnapshot($travel->id, $travel->hotelId, $payload, $payloadHash);
$this->applyTravelMetadata($snapshot, $travel);
$this->entityManager->persist($snapshot);
$this->entityManager->flush();
return;
}
if ($payloadHash === $snapshot->getPayloadHash()) {
return;
}
$snapshot
->setPayload($payload)
->setPayloadHash($payloadHash)
->touchUpdatedAt();
$this->applyTravelMetadata($snapshot, $travel);
$this->entityManager->flush();
}
/**
* Builds travel mapping entries from snapshots.
*
* @return array<int, array<string, mixed>>
*/
public function generateMapping(): array
{
$mapping = [];
foreach ($this->snapshotRepository->findAllForMapping() as $snapshot) {
$dateId = $snapshot->getDateId();
$hotelId = $snapshot->getHotelId();
if (false === isset($mapping[$dateId])) {
$mapping[$dateId] = [
'id' => $dateId,
'code' => $snapshot->getDateCode(),
'label' => $snapshot->getLabel(),
'dateFrom' => $snapshot->getDateFrom(),
'dateTo' => $snapshot->getDateTo(),
'hotels' => [],
'file' => null,
];
}
$mapping[$dateId]['hotels'][$hotelId] = [
'id' => $hotelId,
'code' => $snapshot->getHotelCode(),
'label' => $snapshot->getHotelLabel(),
];
}
return $mapping;
}
/**
* Finds product ID for a travel date based on snapshot metadata.
*/
public function findProductIdByDateId(int $dateId): ?int
{
return $this->snapshotRepository->findProductIdByDateId($dateId);
}
/**
* Refreshes snapshot payloads with extended availability data.
*
* @return array{processed:int,updated:int,failed:int}
*/
public function refreshExtendedSnapshots(int $limit = 500, bool $force = false, int $refreshAfterMinutes = 360): array
{
$dateToThreshold = new \DateTimeImmutable(sprintf('-%d days', $this->retentionBufferDays));
$refreshBefore = new \DateTimeImmutable(sprintf('-%d minutes', $refreshAfterMinutes));
$candidates = true === $force
? $this->snapshotRepository->findAllForMapping($limit)
: $this->snapshotRepository->findRefreshCandidates($dateToThreshold, $refreshBefore, $limit);
$updated = 0;
$failed = 0;
/** @var array<int, ExtendedServiceAvailabilityResponse|null> $extendedResponseByDateId */
$extendedResponseByDateId = [];
foreach ($candidates as $snapshot) {
$dateId = $snapshot->getDateId();
$travel = $this->deserializeTravelFromSnapshot($snapshot);
if (null === $travel) {
++$failed;
continue;
}
if (false === array_key_exists($dateId, $extendedResponseByDateId)) {
$response = null;
try {
$response = $this->apiClient->getAvailabilitiesExtended($dateId);
} catch (ApiClientException $e) {
$extendedResponseByDateId[$dateId] = null;
$this->logger->warning('Failed to fetch extended availability', [
'dateId' => $dateId,
'error' => $e->getMessage(),
]);
}
if (null !== $response && false === $response instanceof Notification) {
$extendedResponseByDateId[$dateId] = $response;
} elseif (false === isset($extendedResponseByDateId[$dateId])) {
$extendedResponseByDateId[$dateId] = null;
}
}
$response = $extendedResponseByDateId[$dateId];
if (null === $response) {
++$failed;
continue;
}
$this->applyExtendedAvailability($travel, $response);
$payload = $this->serializer->serialize($travel, 'json');
$payloadHash = hash('sha256', $payload);
if ($payloadHash !== $snapshot->getPayloadHash()) {
$snapshot->setPayload($payload)->setPayloadHash($payloadHash);
$this->applyTravelMetadata($snapshot, $travel);
}
$snapshot->setExtendedRefreshedAt(new \DateTimeImmutable())->touchUpdatedAt();
$this->entityManager->flush();
++$updated;
}
return [
'processed' => count($candidates),
'updated' => $updated,
'failed' => $failed,
];
}
/**
* Purges snapshots past retention threshold.
*/
public function purgeExpiredSnapshots(): int
{
$beforeDate = new \DateTimeImmutable(sprintf('-%d days', $this->retentionBufferDays));
return $this->snapshotRepository->deleteExpiredSnapshots($beforeDate);
}
/**
* Resolves snapshot by exact date/hotel or by date fallback.
*/
private function findSnapshot(int $dateId, ?int $hotelId = null): ?TravelSnapshot
{
if (null !== $hotelId) {
return $this->snapshotRepository->findByDateAndHotel($dateId, $hotelId);
}
return $this->snapshotRepository->findFirstByDateId($dateId);
}
/**
* Deserializes Travel from an already-loaded snapshot without an extra DB query.
*/
private function deserializeTravelFromSnapshot(TravelSnapshot $snapshot): ?Travel
{
try {
$travel = $this->serializer->deserialize($snapshot->getPayload(), Travel::class, 'json');
} catch (\Throwable $e) {
$this->logger->warning('Snapshot payload cannot be deserialized to Travel', [
'snapshotId' => $snapshot->getId(),
'error' => $e->getMessage(),
]);
return null;
}
if (false === $travel instanceof Travel) {
return null;
}
return $travel;
}
/**
* Synchronizes lightweight lookup metadata from Travel into snapshot row.
*/
private function applyTravelMetadata(TravelSnapshot $snapshot, Travel $travel): void
{
$snapshot
->setDateCode($travel->code)
->setLabel($travel->label)
->setProductCode($travel->productCode)
->setProductId($travel->productId)
->setDateFrom($travel->dateFrom)
->setDateTo($travel->dateTo)
->setHotelCode($travel->hotel?->code)
->setHotelLabel($travel->hotel?->name);
}
/**
* Applies extended availability deltas to matching Travel services.
*/
private function applyExtendedAvailability(Travel $travel, ExtendedServiceAvailabilityResponse $response): void
{
foreach ($response->getServices() as $serviceId => $availability) {
$service = $travel->additionalServices[$serviceId] ?? $travel->transportationServices[$serviceId] ?? null;
if (false === $service instanceof Service) {
continue;
}
$this->applyExtendedToService($service, $availability);
}
if ([] !== $response->allowedBookingStatus) {
$travel->allowedBookingStatus = $response->allowedBookingStatus;
}
if (null !== $response->travelStatus && '' !== trim($response->travelStatus)) {
$travel->status = $response->travelStatus;
}
}
/**
* Applies one extended availability record to one Service model.
*/
private function applyExtendedToService(Service $service, ExtendedAvailability $availability): void
{
if (null !== $availability->available) {
$service->available = $availability->available;
}
if (null !== $availability->status && '' !== trim($availability->status)) {
$service->status = $availability->status;
}
if (null !== $availability->price) {
$service->price = $availability->price;
}
if (null !== $availability->dateFrom) {
$service->dateFrom = $availability->dateFrom;
}
if (null !== $availability->dateTo) {
$service->dateTo = $availability->dateTo;
}
if (null !== $availability->description && '' !== trim($availability->description)) {
$service->description = $availability->description;
}
if (null !== $availability->ageFrom) {
$service->ageFrom = $availability->ageFrom;
}
if (null !== $availability->ageTo) {
$service->ageTo = $availability->ageTo;
}
if (null !== $availability->mandatory) {
$service->mandatory = $availability->mandatory;
}
if (null !== $availability->timeFrom && '' !== trim($availability->timeFrom)) {
$service->timeFrom = $availability->timeFrom;
$service->dayTime = (new DayTimeUtility())->mapTime($availability->timeFrom);
}
}
}