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; } }