This commit is contained in:
Björn Fromme
2023-10-08 12:37:44 +02:00
parent 8740041e7a
commit 8e3499581a
35 changed files with 119 additions and 1109 deletions
+45 -43
View File
@@ -45,14 +45,9 @@ class BpnImportCommand extends Command
return Command::FAILURE;
}
// Flush dates
$this
->connection
->executeQuery('TRUNCATE TABLE bpn_date')
;
$io->info('Found '.$totalCount.' XML files');
$datesCount = 0;
$addedCount = 0;
$updatedCount = 0;
// Iterate over xml files
foreach ($xmlFiles as $xmlFile) {
@@ -60,26 +55,17 @@ class BpnImportCommand extends Command
// 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;
}
}
// Iterate over all destinations
foreach ($xml->xpath('reise/termin') as $destinationXml) {
$product = (string) $destinationXml->xpath('text')[0];
$busProId = (int) $destinationXml->attributes()['idbuspro'];
$code = (string) $destinationXml->attributes()['code'];
$dateFrom = \DateTimeImmutable::createFromFormat('d.m.Y', (string) $destinationXml->attributes()['termin']);
$dateTo = \DateTimeImmutable::createFromFormat('d.m.Y', (string) $destinationXml->attributes()['bis']);
// Find pickups
$datePickups = [];
foreach ($dateXml->xpath('zustiege/zustieg') as $pickupXml) {
foreach ($destinationXml->xpath('zustiege/zustieg') as $pickupXml) {
$pickupBusProId = (int) $pickupXml->attributes()['idbuspro'];
if ($pickupBusProId) {
$pickup = $this->pickupDataProvider->get($pickupBusProId);
@@ -93,32 +79,48 @@ class BpnImportCommand extends Command
}
// Iterate over all hotel entries
foreach ($dateXml->xpath('hotel') as $hotelXml) {
foreach ($destinationXml->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;
// Finally update or store collected data
if ($id = $this->connection->fetchOne('SELECT id FROM destination WHERE bus_pro_id=? AND hotel_bus_pro_id=?', [$busProId, $hotelBusProId])) {
$this
->connection
->update('destination', [
'code' => $code,
'date_from' => $dateFrom->format('Y-m-d'),
'date_to' => $dateTo->format('Y-m-d'),
'product' => $product,
'pickups' => json_encode($datePickups),
], [
'id' => $id,
'hotel_bus_pro_id' => $hotelBusProId,
])
;
++$updatedCount;
} else {
$this
->connection
->insert('destination', [
'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,
'pickups' => json_encode($datePickups),
])
;
++$addedCount;
}
}
}
}
$io->info('Imported '.$datesCount.' dates');
$io->info('Added '.$addedCount.' and updated '.$updatedCount.' destinations');
return Command::SUCCESS;
}