From fbfd71adcf1b793854342f0317a3be47cafe0100 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Thu, 22 Mar 2018 12:43:58 -0500 Subject: [PATCH] Cleanup Exporter; use firstOrCreate for any missing data we can infer/setup defaults for --- .../Controllers/Admin/FlightController.php | 4 +-- app/Interfaces/ImportExport.php | 11 +++++-- app/Models/Flight.php | 1 + app/Services/ExportService.php | 32 ++++++++++++------- .../AircraftImporter.php | 18 ++++------- .../AirportImporter.php | 6 ++-- .../FlightExporter.php | 8 ++--- .../FlightImporter.php | 13 ++++---- .../SubfleetImporter.php | 4 ++- app/Services/ImportService.php | 8 ++--- tests/ImporterTest.php | 2 +- 11 files changed, 58 insertions(+), 49 deletions(-) rename app/Services/{Import => ImportExport}/AircraftImporter.php (84%) rename app/Services/{Import => ImportExport}/AirportImporter.php (89%) rename app/Services/{Import => ImportExport}/FlightExporter.php (95%) rename app/Services/{Import => ImportExport}/FlightImporter.php (93%) rename app/Services/{Import => ImportExport}/SubfleetImporter.php (94%) diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index 095d9ed8..a3af877d 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -320,11 +320,9 @@ class FlightController extends Controller public function export(Request $request) { $exporter = app(ExportService::class); - $path = storage_path('app/import/export_flight.csv'); - $flights = $this->flightRepo->all(); - $exporter->exportFlights($flights, $path); + $path = $exporter->exportFlights($flights); return response() ->download($path, 'flights.csv', [ 'content-type' => 'text/csv', diff --git a/app/Interfaces/ImportExport.php b/app/Interfaces/ImportExport.php index c42a9ae3..7ed5e668 100644 --- a/app/Interfaces/ImportExport.php +++ b/app/Interfaces/ImportExport.php @@ -10,6 +10,7 @@ use App\Models\Airline; */ class ImportExport { + public $assetType; public $status; /** @@ -18,13 +19,17 @@ class ImportExport public static $columns = []; /** - * Get the airline from the ICAO + * Get the airline from the ICAO. Create it if it doesn't exist * @param $code - * @return \App\Models\Airline + * @return \Illuminate\Database\Eloquent\Model */ public function getAirline($code) { - return Airline::where('icao', $code)->first(); + $airline = Airline::firstOrCreate([ + 'icao' => $code, + ], ['name' => $code]); + + return $airline; } /** diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 70206fa3..2b439ef6 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -17,6 +17,7 @@ use PhpUnitsOfMeasure\Exception\NonStringUnitName; * @property mixed route_leg * @property Collection field_values * @property Collection fares + * @property Collection subfleets */ class Flight extends Model { diff --git a/app/Services/ExportService.php b/app/Services/ExportService.php index b3d76045..fd56983b 100644 --- a/app/Services/ExportService.php +++ b/app/Services/ExportService.php @@ -5,10 +5,12 @@ namespace App\Services; use App\Interfaces\ImportExport; use App\Interfaces\Service; use App\Repositories\FlightRepository; -use App\Services\Import\FlightExporter; +use App\Services\ImportExport\FlightExporter; use Illuminate\Support\Collection; use League\Csv\CharsetConverter; use League\Csv\Writer; +use Illuminate\Support\Facades\Storage; + /** * Class ExportService @@ -27,12 +29,12 @@ class ExportService extends Service } /** - * @param $csv_file + * @param string $path * @return Writer */ - public function openCsv($csv_file): Writer + public function openCsv($path): Writer { - $writer = Writer::createFromPath($csv_file, 'w+'); + $writer = Writer::createFromPath($path, 'w+'); CharsetConverter::addTo($writer, 'utf-8', 'iso-8859-15'); return $writer; } @@ -40,19 +42,27 @@ class ExportService extends Service /** * Run the actual importer * @param Collection $collection - * @param Writer $writer * @param ImportExport $exporter - * @return bool + * @return string * @throws \League\Csv\CannotInsertRecord */ - protected function runExport(Collection $collection, Writer $writer, ImportExport $exporter): bool + protected function runExport(Collection $collection, ImportExport $exporter): string { + $filename = 'export_' . $exporter->assetType . '.csv'; + Storage::makeDirectory(storage_path('app/import')); + $path = storage_path('app/import/'.$filename.'.csv'); + + $writer = $this->openCsv($path); + + // Write out the header first $writer->insertOne($exporter->getColumns()); + + // Write the rest of the rows foreach ($collection as $row) { $writer->insertOne($exporter->export($row)); } - return true; + return $path; } /** @@ -62,11 +72,9 @@ class ExportService extends Service * @return mixed * @throws \League\Csv\Exception */ - public function exportFlights($flights, $csv_file) + public function exportFlights($flights) { - $writer = $this->openCsv($csv_file); - $exporter = new FlightExporter(); - return $this->runExport($flights, $writer, $exporter); + return $this->runExport($flights, $exporter); } } diff --git a/app/Services/Import/AircraftImporter.php b/app/Services/ImportExport/AircraftImporter.php similarity index 84% rename from app/Services/Import/AircraftImporter.php rename to app/Services/ImportExport/AircraftImporter.php index ff40f4f5..f15c6afd 100644 --- a/app/Services/Import/AircraftImporter.php +++ b/app/Services/ImportExport/AircraftImporter.php @@ -1,6 +1,6 @@ $type])->first(); - if (!$subfleet) { - $subfleet = new Subfleet([ - 'type' => $type, - 'name' => $type, - ]); - - $subfleet->save(); - } + $subfleet = Subfleet::firstOrCreate([ + 'type' => $type, + ], ['name' => $type]); return $subfleet; } @@ -53,7 +49,7 @@ class AircraftImporter extends ImportExport * @param int $index * @return bool */ - public function import(array $row, $index) + public function import(array $row, $index): bool { $subfleet = $this->getSubfleet($row['subfleet']); diff --git a/app/Services/Import/AirportImporter.php b/app/Services/ImportExport/AirportImporter.php similarity index 89% rename from app/Services/Import/AirportImporter.php rename to app/Services/ImportExport/AirportImporter.php index 69f5cc41..2bd510f1 100644 --- a/app/Services/Import/AirportImporter.php +++ b/app/Services/ImportExport/AirportImporter.php @@ -1,6 +1,6 @@ airlineRepo = app(AirlineRepository::class); $this->fareSvc = app(FareService::class); $this->flightSvc = app(FlightService::class); } @@ -113,7 +112,7 @@ class FlightImporter extends ImportExport $count = 0; $subfleets = $this->parseMultiColumnValues($col); foreach($subfleets as $subfleet_type) { - $subfleet = Subfleet::firstOrNew( + $subfleet = Subfleet::firstOrCreate( ['type' => $subfleet_type], ['name' => $subfleet_type] ); @@ -142,7 +141,7 @@ class FlightImporter extends ImportExport $fare_attributes = []; } - $fare = Fare::firstOrNew(['code' => $fare_code], ['name' => $fare_code]); + $fare = Fare::firstOrCreate(['code' => $fare_code], ['name' => $fare_code]); $this->fareSvc->setForFlight($flight, $fare, $fare_attributes); } } diff --git a/app/Services/Import/SubfleetImporter.php b/app/Services/ImportExport/SubfleetImporter.php similarity index 94% rename from app/Services/Import/SubfleetImporter.php rename to app/Services/ImportExport/SubfleetImporter.php index 791ca584..937562a5 100644 --- a/app/Services/Import/SubfleetImporter.php +++ b/app/Services/ImportExport/SubfleetImporter.php @@ -1,6 +1,6 @@ export($flight); $this->assertEquals('VMS', $exported['airline']);