Account ICAO for subfleet, airline/location if columns exist #628 (#632)

This commit is contained in:
Nabeel S
2020-03-09 13:40:10 -04:00
committed by GitHub
parent 80b20a8b25
commit 412919fe21
@@ -11,32 +11,56 @@ class AircraftImporter extends BaseImporter
{ {
protected $table = 'aircraft'; protected $table = 'aircraft';
/**
* CONSTANTS
*/
public const SUBFLEET_NAME = 'Imported Aircraft';
public function run($start = 0) public function run($start = 0)
{ {
$this->comment('--- AIRCRAFT IMPORT ---'); $this->comment('--- AIRCRAFT IMPORT ---');
$subfleet = $this->getSubfleet(); $fields = [
'id',
'icao',
'name',
'fullname',
'registration',
'enabled',
];
$this->info('Subfleet ID is '.$subfleet->id); // See if there is an airline column
$columns = $this->db->getColumns($this->table);
if (in_array('airline', $columns)) {
$fields[] = 'airline';
}
if (in_array('location', $columns)) {
$fields[] = 'location';
}
$count = 0; $count = 0;
$rows = $this->db->readRows($this->table, $this->idField, $start); $rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
foreach ($rows as $row) { foreach ($rows as $row) {
$airline_id = null;
if (!empty($row->airline)) {
$airline_id = $this->idMapper->getMapping('airlines', $row->airline_id);
}
$subfleet = $this->getSubfleet($row->icao, $airline_id);
$this->info('Subfleet ID is '.$subfleet->id);
$where = [ $where = [
'name' => $row->fullname,
'registration' => $row->registration, 'registration' => $row->registration,
]; ];
$aircraft = Aircraft::firstOrCreate($where, [ $cols = [
'icao' => $row->icao, 'icao' => $row->icao,
'name' => $row->fullname,
'subfleet_id' => $subfleet->id, 'subfleet_id' => $subfleet->id,
'active' => $row->enabled, 'active' => $row->enabled,
]); ];
if (!empty($row->location)) {
$cols['airport_id'] = $row->location;
}
$aircraft = Aircraft::firstOrCreate($where, $cols);
$this->idMapper->addMapping('aircraft', $row->id, $aircraft->id); $this->idMapper->addMapping('aircraft', $row->id, $aircraft->id);
@@ -51,16 +75,21 @@ class AircraftImporter extends BaseImporter
/** /**
* Return the subfleet * Return the subfleet
* *
* @param string $icao ICAO of the subfleet
* @param int $airline_id
*
* @return mixed * @return mixed
*/ */
protected function getSubfleet() protected function getSubfleet($icao, $airline_id = null)
{ {
$airline = Airline::first(); if ($airline_id === null) {
$subfleet = Subfleet::firstOrCreate([ $airline = Airline::first();
'airline_id' => $airline->id, $airline_id = $airline->id;
'name' => self::SUBFLEET_NAME, }
], ['type' => 'PHPVMS']);
return $subfleet; return Subfleet::firstOrCreate([
'airline_id' => $airline_id,
'name' => $icao,
], ['type' => $icao]);
} }
} }