feat: integrate with flysystem for xml data
This commit is contained in:
1 parent
036734eadf
commit
e4e03a848b
12 files changed
+617
-24
No files matched your search
@@ -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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user