WIP: Implement closer BPN integration
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\BusProNet\DataProvider\HotelDataProvider;
|
||||
use App\BusProNet\DataProvider\PickupDataProvider;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:bpn-import',
|
||||
description: 'Imports dates, products and hotels from BusProNet',
|
||||
)]
|
||||
class BpnImportCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Connection $connection,
|
||||
private readonly HotelDataProvider $hotelDataProvider,
|
||||
private readonly PickupDataProvider $pickupDataProvider,
|
||||
private readonly string $xmlFilesPath
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
// Get all xml files
|
||||
$xmlFiles = (new Finder())
|
||||
->name('Ziel_*.xml')
|
||||
->in($this->xmlFilesPath)
|
||||
;
|
||||
|
||||
$totalCount = count($xmlFiles);
|
||||
|
||||
if (0 === $totalCount) {
|
||||
$io->error('No XML files available or found');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Flush dates
|
||||
$this
|
||||
->connection
|
||||
->executeQuery('TRUNCATE TABLE bpn_date')
|
||||
;
|
||||
|
||||
$io->info('Found '.$totalCount.' XML files');
|
||||
$datesCount = 0;
|
||||
|
||||
// Iterate over xml files
|
||||
foreach ($xmlFiles as $xmlFile) {
|
||||
|
||||
// Load xml file into simplexml object
|
||||
$xml = simplexml_load_file($xmlFile);
|
||||
|
||||
// Iterate over all dates
|
||||
foreach ($xml->xpath('reise/termin') as $dateXml) {
|
||||
$product = (string) $dateXml->xpath('text')[0];
|
||||
$busProId = (int) $dateXml->attributes()['idbuspro'];
|
||||
$code = (string) $dateXml->attributes()['code'];
|
||||
$dateFrom = \DateTimeImmutable::createFromFormat('d.m.Y', (string) $dateXml->attributes()['termin']);
|
||||
$dateTo = \DateTimeImmutable::createFromFormat('d.m.Y', (string) $dateXml->attributes()['bis']);
|
||||
$bus = false;
|
||||
|
||||
// Iterate over all service entries and look for 'bus'
|
||||
foreach ($dateXml->xpath('lei_befoerderung/leistung') as $serviceXml) {
|
||||
if ('BUS' === (string) $serviceXml->attributes()['unterart']) {
|
||||
$bus = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Find pickups
|
||||
$datePickups = [];
|
||||
foreach ($dateXml->xpath('zustiege/zustieg') as $pickupXml) {
|
||||
$pickupBusProId = (int) $pickupXml->attributes()['idbuspro'];
|
||||
if ($pickupBusProId) {
|
||||
$pickup = $this->pickupDataProvider->get($pickupBusProId);
|
||||
$datePickups[] = [
|
||||
'busProId' => $pickupBusProId,
|
||||
'city' => $pickup->getCity(),
|
||||
'street' => $pickup->getStreet(),
|
||||
'time' => (string) $pickupXml->attributes()['zeit'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate over all hotel entries
|
||||
foreach ($dateXml->xpath('hotel') as $hotelXml) {
|
||||
$hotelBusProId = (int) $hotelXml->attributes()['idbuspro'];
|
||||
$hotel = $this->hotelDataProvider->get($hotelBusProId);
|
||||
|
||||
// Finally store collected data
|
||||
$this
|
||||
->connection
|
||||
->insert('bpn_date', [
|
||||
'code' => $code,
|
||||
'bus_pro_id' => $busProId,
|
||||
'date_from' => $dateFrom->format('Y-m-d'),
|
||||
'date_to' => $dateTo->format('Y-m-d'),
|
||||
'product' => $product,
|
||||
'hotel' => $hotel->getName(),
|
||||
'hotel_code' => $hotel->getCode(),
|
||||
'hotel_bus_pro_id' => $hotelBusProId,
|
||||
'bus' => (int) $bus,
|
||||
'pickups' => json_encode($datePickups),
|
||||
])
|
||||
;
|
||||
++$datesCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$io->info('Imported '.$datesCount.' dates');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user