feat: integrate with flysystem for xml data

This commit is contained in:
Björn Fromme
2025-03-26 16:07:37 +01:00
parent 036734eadf
commit e4e03a848b
12 changed files with 617 additions and 24 deletions
+6 -5
View File
@@ -14,6 +14,8 @@ use App\BusProNet\Model\Notification;
use App\BusProNet\Model\PersonalData;
use App\Form\Model\BookingData;
use App\Form\Model\RegistrationData;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
@@ -35,6 +37,7 @@ class ApiClient
public function __construct(
private readonly SerializerInterface $serializer,
private readonly ApiResponseParser $responseParser,
private readonly FilesystemOperator $xmlDump,
private readonly LoggerInterface $logger,
array $options
) {
@@ -329,11 +332,10 @@ class ApiClient
private function dumpXmlToFile(string $type, string $requestId, string $body): void
{
if (false === file_exists($this->config['target_folder_dumps'])) {
mkdir($this->config['target_folder_dumps']);
try {
$this->xmlDump->write($requestId . '_' . $type . '.xml', $body);
} catch (FilesystemException $e) {
}
file_put_contents($this->config['target_folder_dumps'].'/'.$requestId.'_'.$type.'.xml', $body);
}
private function createKey(string $username, string $password, string $type): string
@@ -355,7 +357,6 @@ class ApiClient
$optionsResolver->setDefaults([
'max_retries' => 25,
'debug' => false,
'target_folder_dumps' => '/tmp/bpn',
]);
return $optionsResolver->resolve($options);
+6 -3
View File
@@ -4,6 +4,7 @@ namespace App\BusProNet\XmlLoader;
use App\BusProNet\TypeConversionTrait;
use App\BusProNet\XmlParserTrait;
use League\Flysystem\FilesystemOperator;
use Symfony\Contracts\Cache\CacheInterface;
abstract class AbstractLoader
@@ -11,7 +12,9 @@ abstract class AbstractLoader
use TypeConversionTrait;
use XmlParserTrait;
public function __construct(protected readonly CacheInterface $cache, protected readonly ?string $xmlPath = null)
{
public function __construct(
protected readonly CacheInterface $cache,
protected readonly FilesystemOperator $xmlExport,
) {
}
}
}
+2 -3
View File
@@ -56,8 +56,7 @@ class HotelLoader extends AbstractLoader
private function loadXml(?string $filename = 'hotel.xml'): Crawler
{
$filepath = $this->xmlPath.'/'.$filename;
$xml = file_get_contents($filepath);
$xml = $this->xmlExport->read($filename);
return new Crawler($xml);
}
@@ -82,4 +81,4 @@ class HotelLoader extends AbstractLoader
$hotel = $this->loadById($hotelId);
$travel->hotel = $hotel;
}
}
}
+8 -1
View File
@@ -16,7 +16,7 @@ class PickupLoader extends AbstractLoader
return $this->cache->get('bpn_pickups', function (ItemInterface $item) use ($filename) {
$item->expiresAfter(3 * 60 * 60);
$crawler = new Crawler(file_get_contents($this->xmlPath . '/' . $filename));
$crawler = $this->loadXml($filename);
$pickupNodes = $crawler->filterXPath('//zustiege/zustieg');
$pickups = [];
@@ -40,6 +40,13 @@ class PickupLoader extends AbstractLoader
return $pickups[$id] ?? null;
}
private function loadXml(?string $filename = 'zustiege.xml'): Crawler
{
$xml = $this->xmlExport->read($filename);
return new Crawler($xml);
}
public function parseXml(Crawler $node): Pickup
{
$pickup = new Pickup();
+17 -9
View File
@@ -11,9 +11,10 @@ use App\BusProNet\Model\Pickup;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Finder\Finder;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
@@ -23,9 +24,9 @@ class TravelLoader extends AbstractLoader
private readonly HotelLoader $hotelDataLoader,
CacheInterface $cache,
private readonly string $travelInfoBaseUrl,
?string $xmlPath = null
FileSystemOperator $xmlExport,
) {
parent::__construct($cache, $xmlPath);
parent::__construct($cache, $xmlExport);
}
public function generateFilesMap(): array
@@ -34,14 +35,21 @@ class TravelLoader extends AbstractLoader
return $this->cache->get('bpn_travels_mapping', function (ItemInterface $item) {
$item->expiresAfter(3 * 60 * 60);
$finder = new Finder();
$finder->files()->in($this->xmlPath)->name('Ziel_*.xml');
$xmlFiles = $this
->xmlExport
->listContents('.')->filter(fn (StorageAttributes $attributes) => $attributes->isFile())
;
$mapping = [];
$hotels = $this->hotelDataLoader->loadAll();
foreach ($finder as $file) {
$xml = $file->getContents();
foreach ($xmlFiles as $file) {
if (false === str_starts_with($file->path(), 'Ziel_')) {
continue;
}
$xml = $this->xmlExport->read($file->path());
$crawler = new Crawler($xml);
$travelDataNodes = $crawler->filterXPath('//reisen/reise/termin');
@@ -55,7 +63,7 @@ class TravelLoader extends AbstractLoader
'dateFrom' => $this->stringToDate($node->attr('termin')),
'dateTo' => $this->stringToDate($node->attr('bis')),
'hotels' => [],
'file' => $file->getRealPath(),
'file' => $file->path(),
];
$node->filterXPath('//hotel')
@@ -110,7 +118,7 @@ class TravelLoader extends AbstractLoader
private function loadXml(int $id, ?int $hotelId, string $filename): ?Travel
{
$xml = file_get_contents($filename);
$xml = $this->xmlExport->read($filename);
$crawler = new Crawler($xml);
$travelNode = $crawler->filterXPath(sprintf('//reise/termin[@idbuspro="%d"]', $id));