feat: api endpoints and xml sync for contingents data

This commit is contained in:
Björn Fromme committed 2026-04-22 11:23:52 +02:00
1 parent a1f68ec11d
commit 9b84893d88
50 files changed
+2567 -221

No files matched your search

@@ -0,0 +1,127 @@
<?php
namespace App\BusProNet\XmlLoader;
use App\BusProNet\XmlParser\ContingentParser;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
use Psr\Cache\InvalidArgumentException;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
class ContingentLoader extends AbstractLoader
{
public function __construct(
private readonly ContingentParser $parser,
TagAwareCacheInterface $cache,
FilesystemOperator $xmlExportContingents,
) {
parent::__construct($cache, $xmlExportContingents);
}
/**
* @return array<int, string>
*/
public function generateFilesMap(): array
{
try {
return $this->cache->get('bpn_contingent_files', function (ItemInterface $item) {
$item->expiresAfter(3 * 60 * 60);
$item->tag(['xml-sync']);
$xmlFiles = $this->xmlExport
->listContents('.')
->filter(fn (StorageAttributes $attributes) => $attributes->isFile() && str_starts_with($attributes->path(), 'HotelZimmer_'));
$mapping = [];
foreach ($xmlFiles as $file) {
if (1 !== preg_match('/HotelZimmer_(\d+)\.xml$/', $file->path(), $matches)) {
continue;
}
$mapping[(int) $matches[1]] = $file->path();
}
return $mapping;
});
} catch (InvalidArgumentException) {
return [];
}
}
/**
* @return array{roomTypes: array<int, array<string, mixed>>, rows: array<int, array<string, mixed>>}
*/
public function loadByHotelId(int $hotelId): array
{
$cacheKey = sprintf('contingent_hotel_%d', $hotelId);
try {
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($hotelId) {
$item->expiresAfter(3600);
$item->tag(['xml-sync']);
return $this->loadByHotelIdUncached($hotelId);
});
} catch (InvalidArgumentException) {
return $this->loadByHotelIdUncached($hotelId);
}
}
/**
* @return array{roomTypes: array<int, array<string, mixed>>, rows: array<int, array<string, mixed>>}
*/
private function loadByHotelIdUncached(int $hotelId): array
{
$mapping = $this->generateFilesMap();
if (false === isset($mapping[$hotelId])) {
return ['roomTypes' => [], 'rows' => []];
}
$filename = $mapping[$hotelId];
try {
$xml = $this->xmlExport->read($filename);
} catch (FilesystemException) {
return ['roomTypes' => [], 'rows' => []];
}
$linkedHotelId = $this->getRootLinkedHotelId($xml);
if (null !== $linkedHotelId) {
if (false === isset($mapping[$linkedHotelId])) {
return ['roomTypes' => [], 'rows' => []];
}
try {
$xml = $this->xmlExport->read($mapping[$linkedHotelId]);
} catch (FilesystemException) {
return ['roomTypes' => [], 'rows' => []];
}
}
return $this->parser->parse($xml);
}
private function getRootLinkedHotelId(string $xml): ?int
{
$document = new \DOMDocument();
if (false === @$document->loadXML($xml)) {
return null;
}
$root = $document->documentElement;
if (null === $root) {
return null;
}
$link = $root->getAttribute('idbuspro_kontingent_aus');
if ('' === $link) {
return null;
}
return (int) $link;
}
}
+1
View File
@@ -17,6 +17,7 @@ class HotelLoader extends AbstractLoader
try {
return $this->cache->get('bpn_hotels', function (ItemInterface $item) use ($filename) {
$item->expiresAfter(3 * 60 * 60);
$item->tag(['xml-sync']);
$hotels = [];
+2 -1
View File
@@ -27,7 +27,8 @@ class InsuranceLoader extends AbstractLoader
{
try {
return $this->cache->get('bpn_insurances', function (ItemInterface $item) use ($filename) {
$item->expiresAfter(24 * 60 * 60);
$item->expiresAfter(3 * 60 * 60);
$item->tag(['xml-sync']);
$crawler = $this->loadXml($filename);
+1
View File
@@ -16,6 +16,7 @@ class PickupLoader extends AbstractLoader
try {
return $this->cache->get('bpn_pickups', function (ItemInterface $item) use ($filename) {
$item->expiresAfter(3 * 60 * 60);
$item->tag(['xml-sync']);
$crawler = $this->loadXml($filename);
$pickupNodes = $crawler->filterXPath('//zustiege/zustieg');
+1
View File
@@ -58,6 +58,7 @@ class TravelLoader extends AbstractLoader
try {
return $this->cache->get('bpn_travels_mapping', function (ItemInterface $item) {
$item->expiresAfter(3 * 60 * 60);
$item->tag(['xml-sync']);
$xmlFiles = $this
->xmlExport