wip: initial commit

This commit is contained in:
Björn Fromme
2024-11-20 18:32:09 +01:00
parent ae964198e9
commit d4c4e69d09
91 changed files with 32685 additions and 144 deletions
@@ -0,0 +1,15 @@
<?php
namespace App\BusProNet\DataLoader;
use App\BusProNet\ApiResponseParser\ResponseParserTrait;
use Symfony\Contracts\Cache\CacheInterface;
abstract class AbstractDataLoader
{
use ResponseParserTrait;
public function __construct(protected readonly CacheInterface $cache, protected readonly ?string $xmlPath = null)
{
}
}
@@ -0,0 +1,75 @@
<?php
namespace App\BusProNet\DataLoader;
use App\BusProNet\Model\Hotel;
use Psr\Cache\InvalidArgumentException;
use Symfony\Contracts\Cache\ItemInterface;
class HotelDataLoader extends AbstractDataLoader
{
public function loadAll(?string $filename = 'hotel.xml'): array
{
try {
return $this->cache->get('bpn_hotels', function (ItemInterface $item) use ($filename) {
$item->expiresAfter(3600);
$xml = $this->loadXml($filename);
$hotels = [];
foreach ($xml->hotel as $hotel) {
$id = (int)$hotel->attributes()['idbuspro'];
$hotels[$id] = $this->parseXml($hotel);
}
return $hotels;
});
} catch (InvalidArgumentException $e) {
return [];
}
}
public function loadById(int $id, ?string $filename = 'hotel.xml'): ?Hotel
{
$hotels = $this->loadAll($filename);
return $hotels[$id] ?? null;
}
public function loadByCode(string $code, ?string $filename = 'hotel.xml'): ?Hotel
{
$hotels = $this->loadAll($filename);
foreach ($hotels as $hotel) {
if ($code === $hotel->code) {
return $hotel;
}
}
return null;
}
private function loadXml(?string $filename = 'hotel.xml'): \SimpleXMLElement
{
$filepath = $this->xmlPath.'/'.$filename;
return simplexml_load_file($filepath);
}
public function parseXml(\SimpleXMLElement $xml): Hotel
{
$attributes = $xml->attributes();
$hotel = new Hotel();
$hotel->id = (int) $attributes['idbuspro'];
$hotel->code = (string) $attributes['code'];
$hotel->name = (string) $xml->name;
$hotel->city = $xml->ort ? (string) $xml->ort : null;
$hotel->country = (string) $xml->land;
$hotel->street = (string) $xml->strasse;
$hotel->phone = $xml->telefon ? (string) $xml->telefon : null;
return $hotel;
}
}
@@ -0,0 +1,66 @@
<?php
namespace App\BusProNet\DataLoader;
use App\BusProNet\Model\Pickup;
use App\BusProNet\Model\Travel;
use Psr\Cache\InvalidArgumentException;
use Symfony\Contracts\Cache\ItemInterface;
class PickupDataLoader extends AbstractDataLoader
{
public function loadAll(?string $filename = 'zustiege.xml'): array
{
try {
return $this->cache->get('bpn_pickups', function (ItemInterface $item) use ($filename) {
$item->expiresAfter(3600);
$xml = simplexml_load_file($this->xmlPath . '/' . $filename);
$pickups = [];
foreach ($xml->zustieg as $pickup) {
$id = (int)$pickup->attributes()['idbuspro'];
$pickups[$id] = $this->parseXml($pickup);
}
return $pickups;
});
} catch (InvalidArgumentException $e) {
return [];
}
}
public function loadById(int $id, ?string $filename = 'zustiege.xml'): ?Pickup
{
$pickups = $this->loadAll($filename);
return $pickups[$id] ?? null;
}
public function parseXml(\SimpleXMLElement $xml): Pickup
{
$attributes = $xml->attributes();
$pickup = new Pickup();
$pickup->id = (int) $attributes['idbuspro'];
$pickup->code = (string) $attributes['code'];
$pickup->city = (string) $xml->ort;
$pickup->postalCode = (string) $xml->plz;
$pickup->street = (string) $xml->strasse;
return $pickup;
}
public function enrichPickupsData(Travel $travel): void
{
foreach ($travel->pickups as $pickupId => $pickup) {
$pickupData = $this->loadById($pickupId);
$pickup->code = $pickupData->code;
$pickup->postalCode = $pickupData->postalCode;
$pickup->city = $pickupData->city;
$pickup->street = $pickupData->street;
}
}
}
@@ -0,0 +1,194 @@
<?php
namespace App\BusProNet\DataLoader;
use App\BusProNet\Model\BaseData;
use App\BusProNet\Model\CrmAttribute;
use App\BusProNet\Model\CrmAttributeGroup;
use App\BusProNet\Model\Pickup;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use Symfony\Component\Finder\Finder;
class TravelDataLoader extends AbstractDataLoader
{
public function loadById(int $id, ?string $filename = null): ?Travel
{
if (null !== $filename) {
return $this->loadXml($id, $filename);
}
$finder = new Finder();
$finder->files()->in($this->xmlPath)->name('Ziel_*.xml');
foreach ($finder as $file) {
if (null !== $xml = $this->loadXml($id, $file->getRealPath())) {
return $xml;
}
}
return null;
}
private function loadXml(int $id, string $filename): ?Travel
{
$xml = simplexml_load_file($filename);
foreach ($xml->reise->termin as $travel) {
if ($id === (int) $travel->attributes()['idbuspro']) {
return $this->parseXml($travel);
}
}
return null;
}
public function parseXml(\SimpleXMLElement $xml): Travel
{
$attributes = $xml->attributes();
$travel = new Travel();
$travel->id = (int) $attributes['idbuspro'];
$travel->label = (string) $xml->text;
$travel->dateFrom = $this->stringToDate((string) $attributes['termin']);
$travel->dateTo = $this->stringToDate((string) $attributes['bis']);
$travel->code = (string) $attributes['code'];
$travel->type = (string) $attributes['reiseart'];
$travel->priceFrom = $this->stringToFloat((string) $xml->abpreis);
$travel->selectionGroups = $this->getSelectionGroups($xml);
$travel->additionalServices = $this->getAdditionalServices($xml);
$travel->transportationServices = $this->getTransportationServices($xml);
$travel->rooms = $this->getRooms($xml);
$travel->pickups = $this->getPickups($xml);
return $travel;
}
public function getSelectionGroups(\SimpleXMLElement $xml): array
{
$selectionGroups = [];
foreach ($xml->selektiongruppe as $item) {
$groupId = (int) $item->attributes()['idbuspro'];
$selectionGroup = new CrmAttributeGroup();
$selectionGroup->id = $groupId;
$selectionGroup->label = (string) $item->attributes()['bezeichnung'];
foreach ($item->selektion as $subItem) {
$selectionId = (int) $subItem->attributes()['idbuspro'];
$selection = new CrmAttribute();
$selection->id = $selectionId;
$selection->label = (string) $subItem->attributes()['bezeichnung'];
$selectionGroups[$groupId]['selections'][$selectionId] = (string) $subItem->attributes()['bezeichnung'];
$selectionGroup->attributes[] = $selection;
}
$selectionGroups[$groupId] = $selectionGroup;
}
return $selectionGroups;
}
public function getAdditionalServices(\SimpleXMLElement $xml): array
{
$additionalServices = [];
foreach ($xml->lei_sonstiges->leistung as $item) {
$attributes = $item->attributes();
$serviceId = (int) $attributes['idbuspro'];
$service = new Service();
$service->id = $serviceId;
$service->subType = (string) $attributes['unterart'];
$service->mandatory = $this->stringToBool((string) $attributes['pflicht']);
$service->dateFrom = $this->stringToDate((string) $attributes['termin']);
$service->dateTo = $this->stringToDate((string) $attributes['bis']);
$service->label = (string) $item->text;
$service->price = $this->stringToFloat((string) $item->preis);
$service->status = (string) $item->status;
$additionalServices[$serviceId] = $service;
}
return $additionalServices;
}
public function getTransportationServices(\SimpleXMLElement $xml): array
{
$transportationServices = [];
foreach ($xml->lei_befoerderung->leistung as $item) {
$attributes = $item->attributes();
$serviceId = (int) $attributes['idbuspro'];
$service = new Service();
$service->id = $serviceId;
$service->subType = (string) $attributes['unterart'];
$service->dateFrom = $this->stringToDate((string) $attributes['termin']);
$service->dateTo = $this->stringToDate((string) $attributes['bis']);
$service->label = (string) $item->text;
$service->price = $this->stringToFloat((string) $item->preis);
$service->direction = (string) $item->richtung;
$transportationServices[$serviceId] = $service;
}
return $transportationServices;
}
public function getPickups(\SimpleXMLElement $xml): array
{
$pickups = [];
foreach ($xml->zustiege->zustieg as $item) {
$attributes = $item->attributes();
$pickupId = (int) $attributes['idbuspro'];
$pickup = new Pickup();
$pickup->id = $pickupId;
$pickup->time = $this->stringToDateTime((string) $attributes['zeit']);
$pickup->price = $attributes['preis'] ? $this->stringToFloat((string) $attributes['preis']) : null;
$pickups[$pickupId] = $pickup;
}
return $pickups;
}
public function getRooms(\SimpleXMLElement $xml): array
{
$rooms = [];
foreach ($xml->hotel->zimmer->preis as $item) {
$attributes = $item->attributes();
$roomId = (int) $attributes['idbuspro_zimmer'];
$room = new Room();
$room->id = $roomId;
$room->code = (string) $item->attributes()['zimmercode'];
$room->label = (string) $item->attributes()['zimmertext'];
$room->minPax = (int) $item->attributes()['MinPax'];
$room->maxPax = (int) $item->attributes()['MaxPax'];
$room->nights = (int) $item->attributes()['naechte'];
$room->price = $attributes['preis'] ? $this->stringToFloat((string) $attributes['preis']) : null;
$room->status = (string) $item->status;
$room->available = (int) $item->attributes()['verfuegbar'];
$rooms[$roomId] = $room;
}
return $rooms;
}
public function enrichServicesData(Travel $travel, BaseData $availabilities): void
{
$serviceAvailabilities = $availabilities->getItems();
foreach ([...$travel->additionalServices, ...$travel->transportationServices] as $service) {
if (array_key_exists($service->id, $serviceAvailabilities)) {
$service->available = $serviceAvailabilities[$service->id]->available;
}
}
}
}