2018-02-22 11:38:35 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
use App\Interfaces\ImportExport;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Service;
|
2018-03-21 08:17:11 +08:00
|
|
|
use App\Models\Airport;
|
2018-02-22 11:38:35 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
2018-03-23 01:43:58 +08:00
|
|
|
use App\Services\ImportExport\AircraftImporter;
|
|
|
|
use App\Services\ImportExport\AirportImporter;
|
|
|
|
use App\Services\ImportExport\FlightImporter;
|
|
|
|
use App\Services\ImportExport\SubfleetImporter;
|
2018-03-21 08:17:11 +08:00
|
|
|
use League\Csv\Reader;
|
2018-02-22 11:38:35 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-22 08:12:36 +08:00
|
|
|
* Class ImportService
|
2018-02-22 11:38:35 +08:00
|
|
|
* @package App\Services
|
|
|
|
*/
|
2018-03-22 08:12:36 +08:00
|
|
|
class ImportService extends Service
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
|
|
|
protected $flightRepo;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* ImporterService constructor.
|
|
|
|
* @param FlightRepository $flightRepo
|
|
|
|
*/
|
2018-03-22 08:12:36 +08:00
|
|
|
public function __construct(FlightRepository $flightRepo) {
|
2018-02-22 11:38:35 +08:00
|
|
|
$this->flightRepo = $flightRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param $csv_file
|
|
|
|
* @return Reader
|
|
|
|
* @throws \League\Csv\Exception
|
2018-02-22 11:38:35 +08:00
|
|
|
*/
|
2018-03-21 08:17:11 +08:00
|
|
|
public function openCsv($csv_file)
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
2018-03-21 08:17:11 +08:00
|
|
|
$reader = Reader::createFromPath($csv_file);
|
|
|
|
$reader->setDelimiter(',');
|
|
|
|
$reader->setEnclosure('"');
|
|
|
|
|
|
|
|
return $reader;
|
2018-02-22 11:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 08:17:11 +08:00
|
|
|
* Run the actual importer
|
|
|
|
* @param Reader $reader
|
|
|
|
* @param ImportExport $importer
|
|
|
|
* @return array
|
2018-02-22 11:38:35 +08:00
|
|
|
*/
|
2018-03-21 08:17:11 +08:00
|
|
|
protected function runImport(Reader $reader, ImportExport $importer): array
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
2018-03-21 08:17:11 +08:00
|
|
|
$import_report = [
|
|
|
|
'success' => [],
|
|
|
|
'failed' => [],
|
|
|
|
];
|
2018-02-22 11:42:47 +08:00
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$cols = $importer->getColumns();
|
|
|
|
$first_header = $cols[0];
|
2018-02-22 11:42:47 +08:00
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$records = $reader->getRecords($cols);
|
|
|
|
foreach ($records as $offset => $row) {
|
|
|
|
// check if the first row being read is the header
|
|
|
|
if ($row[$first_header] === $first_header) {
|
2018-02-22 11:38:35 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$success = $importer->import($row, $offset);
|
|
|
|
if ($success) {
|
|
|
|
$import_report['success'][] = $importer->status;
|
|
|
|
} else {
|
|
|
|
$import_report['failed'][] = $importer->status;
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 11:38:35 +08:00
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
return $import_report;
|
|
|
|
}
|
2018-02-22 11:38:35 +08:00
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* Import aircraft
|
|
|
|
* @param string $csv_file
|
|
|
|
* @param bool $delete_previous
|
|
|
|
* @return mixed
|
|
|
|
* @throws \League\Csv\Exception
|
|
|
|
*/
|
|
|
|
public function importAircraft($csv_file, bool $delete_previous = true)
|
|
|
|
{
|
|
|
|
if ($delete_previous) {
|
|
|
|
# TODO: delete airports
|
|
|
|
}
|
2018-02-22 11:38:35 +08:00
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$reader = $this->openCsv($csv_file);
|
|
|
|
if (!$reader) {
|
|
|
|
return false;
|
2018-02-22 11:38:35 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$importer = new AircraftImporter();
|
|
|
|
return $this->runImport($reader, $importer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import airports
|
|
|
|
* @param string $csv_file
|
|
|
|
* @param bool $delete_previous
|
|
|
|
* @return mixed
|
|
|
|
* @throws \League\Csv\Exception
|
|
|
|
*/
|
|
|
|
public function importAirports($csv_file, bool $delete_previous = true)
|
|
|
|
{
|
|
|
|
if ($delete_previous) {
|
|
|
|
Airport::truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
$reader = $this->openCsv($csv_file);
|
|
|
|
if (!$reader) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$importer = new AirportImporter();
|
|
|
|
return $this->runImport($reader, $importer);
|
2018-02-22 11:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import flights
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param string $csv_file
|
|
|
|
* @param bool $delete_previous
|
|
|
|
* @return mixed
|
|
|
|
* @throws \League\Csv\Exception
|
|
|
|
*/
|
|
|
|
public function importFlights($csv_file, bool $delete_previous = true)
|
|
|
|
{
|
|
|
|
if ($delete_previous) {
|
|
|
|
# TODO: Delete all from: flights, flight_field_values
|
|
|
|
}
|
|
|
|
|
|
|
|
$reader = $this->openCsv($csv_file);
|
|
|
|
if (!$reader) {
|
|
|
|
# TODO: Throw an error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$importer = new FlightImporter();
|
|
|
|
return $this->runImport($reader, $importer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import subfleets
|
|
|
|
* @param string $csv_file
|
|
|
|
* @param bool $delete_previous
|
|
|
|
* @return mixed
|
|
|
|
* @throws \League\Csv\Exception
|
2018-02-22 11:38:35 +08:00
|
|
|
*/
|
2018-03-21 08:17:11 +08:00
|
|
|
public function importSubfleets($csv_file, bool $delete_previous = true)
|
2018-02-22 11:38:35 +08:00
|
|
|
{
|
2018-03-21 08:17:11 +08:00
|
|
|
if ($delete_previous) {
|
|
|
|
# TODO: Cleanup subfleet data
|
|
|
|
}
|
|
|
|
|
|
|
|
$reader = $this->openCsv($csv_file);
|
|
|
|
if (!$reader) {
|
|
|
|
# TODO: Throw an error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$importer = new SubfleetImporter();
|
|
|
|
return $this->runImport($reader, $importer);
|
2018-02-22 11:38:35 +08:00
|
|
|
}
|
|
|
|
}
|