wip: booking process, refactoring

This commit is contained in:
Björn Fromme
2025-07-16 19:37:17 +02:00
parent 47faa6b08e
commit eecea0abd1
43 changed files with 2269 additions and 366 deletions
+73 -36
View File
@@ -5,8 +5,9 @@ namespace App\BusProNet\XmlLoader;
use App\BusProNet\Model\BaseData;
use App\BusProNet\Model\MutableData;
use App\BusProNet\Model\Travel;
use App\BusProNet\Utility\TravelCodeUtility;
use App\BusProNet\Utility\DateCodeUtility;
use App\BusProNet\XmlParser\TravelParser;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
use Psr\Cache\InvalidArgumentException;
@@ -24,11 +25,11 @@ use Symfony\Contracts\Cache\ItemInterface;
class TravelLoader extends AbstractLoader
{
/**
* @param HotelLoader $hotelDataLoader Hotel data loader for hotel information
* @param TravelParser $travelParser Parser for XML travel data
* @param string $travelInfoBaseUrl Base URL for travel information pages
* @param CacheInterface $cache Cache interface for performance optimization
* @param FilesystemOperator $xmlExport Filesystem operator for XML file access
* @param HotelLoader $hotelDataLoader Hotel data loader for hotel information
* @param TravelParser $travelParser Parser for XML travel data
* @param string $travelInfoBaseUrl Base URL for travel information pages
* @param CacheInterface $cache Cache interface for performance optimization
* @param FilesystemOperator $xmlExport Filesystem operator for XML file access
*/
public function __construct(
private readonly HotelLoader $hotelDataLoader,
@@ -103,25 +104,55 @@ class TravelLoader extends AbstractLoader
}
/**
* Map a travel code to its corresponding travel ID.
* Map a date code to its corresponding date ID.
*
* Uses the cached files mapping to find the travel ID associated with
* the given travel code. Returns null if no matching travel is found.
* Uses the cached files mapping to find the date ID associated with
* the given date code. Returns null if no matching date is found.
*
* @param string $travelCode The travel code to look up
* @return int|null The travel ID or null if not found
* @param string $dateCode The date code to look up
*
* @return int|null The date ID or null if not found
*/
public function mapCodeToId(string $travelCode): ?int
public function mapCodeToId(string $dateCode): ?int
{
$mapping = $this->generateFilesMap();
$travelCodes = array_column($mapping, 'code', 'id');
$dateCodes = array_column($mapping, 'code', 'id');
if (false === $travelId = array_search($travelCode, $travelCodes)) {
if (false === $dateId = array_search($dateCode, $dateCodes)) {
return null;
}
return $travelId;
return $dateId;
}
/**
* Map a date ID to its corresponding product ID.
*
* Finds the product ID by looking up which XML file contains the given date ID.
* Since XML files are named with the product ID pattern (Ziel_x.xml), this method
* extracts the product ID from the file path.
*
* @param int $dateId The date/term ID to look up
*
* @return int|null The product ID or null if not found
*/
public function mapDateIdToProductId(int $dateId): ?int
{
$mapping = $this->generateFilesMap();
if (!isset($mapping[$dateId])) {
return null;
}
$filename = $mapping[$dateId]['file'];
// Extract product ID from filename (e.g., "Ziel_12345.xml" -> 12345)
if (preg_match('/Ziel_(\d+)\.xml$/', $filename, $matches)) {
return (int) $matches[1];
}
return null;
}
/**
@@ -130,26 +161,27 @@ class TravelLoader extends AbstractLoader
* Retrieves travel data from XML exports. If filename is provided, loads directly
* from that file. Otherwise, uses the cached mapping to find the appropriate file.
*
* @param int $travelId The travel ID to load
* @param int|null $hotelId Optional hotel ID for specific hotel data
* @param int $dateId The travel date ID to load
* @param int|null $hotelId Optional hotel ID for specific hotel data
* @param string|null $filename Optional filename to load from directly
*
* @return Travel|null The loaded travel object or null if not found
*/
public function loadById(int $travelId, ?int $hotelId = null, ?string $filename = null): ?Travel
public function loadById(int $dateId, ?int $hotelId = null, ?string $filename = null): ?Travel
{
if (null !== $filename) {
return $this->loadXml($travelId, $hotelId, $filename);
return $this->loadXml($dateId, $hotelId, $filename);
}
$mapping = $this->generateFilesMap();
if (false === isset($mapping[$travelId])) {
if (false === isset($mapping[$dateId])) {
return null;
}
$filename = $mapping[$travelId]['file'];
$filename = $mapping[$dateId]['file'];
return $this->loadXml($travelId, $hotelId, $filename);
return $this->loadXml($dateId, $hotelId, $filename);
}
/**
@@ -158,23 +190,28 @@ class TravelLoader extends AbstractLoader
* Reads and parses XML file content to extract travel information for
* the specified travel ID and optional hotel ID.
*
* @param int $travelId The travel ID to load
* @param int|null $hotelId Optional hotel ID for specific hotel data
* @param string $filename The XML filename to load from
* @param int $dateId The travel date ID to load
* @param int|null $hotelId Optional hotel ID for specific hotel data
* @param string $filename The XML filename to load from
*
* @return Travel|null The loaded travel object or null if not found
*/
private function loadXml(int $travelId, ?int $hotelId, string $filename): ?Travel
private function loadXml(int $dateId, ?int $hotelId, string $filename): ?Travel
{
$xml = $this->xmlExport->read($filename);
$crawler = new Crawler($xml);
try {
$xml = $this->xmlExport->read($filename);
$crawler = new Crawler($xml);
$travelNode = $crawler->filterXPath(sprintf('//reise/termin[@idbuspro="%d"]', $travelId));
$travelNode = $crawler->filterXPath(sprintf('//reise/termin[@idbuspro="%d"]', $dateId));
if (0 === $travelNode->count()) {
if (0 === $travelNode->count()) {
return null;
}
return $this->travelParser->parse($travelNode->first(), $hotelId);
} catch (FilesystemException $e) {
return null;
}
return $this->travelParser->parse($travelNode->first(), $hotelId);
}
/**
@@ -184,7 +221,7 @@ class TravelLoader extends AbstractLoader
* mutable data configuration. This controls which travel components
* can be modified during booking.
*
* @param Travel $travel The travel object to update
* @param Travel $travel The travel object to update
* @param BaseData $mutableData The mutability configuration data
*/
public function patchMutability(Travel $travel, BaseData $mutableData): void
@@ -220,7 +257,7 @@ class TravelLoader extends AbstractLoader
* Updates the availability status of additional and transportation
* services based on the provided availability data.
*
* @param Travel $travel The travel object to update
* @param Travel $travel The travel object to update
* @param BaseData $availabilities The availability data for services
*/
public function patchAvailabilities(Travel $travel, BaseData $availabilities): void
@@ -244,10 +281,10 @@ class TravelLoader extends AbstractLoader
*/
public function patchBookings(BaseData $bookings): void
{
$travelCodeUtility = new TravelCodeUtility();
$travelCodeUtility = new DateCodeUtility();
foreach ($bookings->getItems() as $booking) {
if (null === $travelId = $booking->travelId) {
if (null === $travelId = $booking->dateId) {
continue;
}