phpvms/modules/Installer/Services/Importer/Importers/AircraftImporter.php
Nabeel S 68eff40753
Replace importer with AJAX powered; better error handling #443 (#447)
* Replace importer with AJAX powered; better error handling #443

* Formatting

* Fix command line importer
2019-12-02 09:57:35 -05:00

66 lines
1.6 KiB
PHP

<?php
namespace Modules\Installer\Services\Importer\Importers;
use App\Models\Aircraft;
use App\Models\Airline;
use App\Models\Subfleet;
use Modules\Installer\Services\Importer\BaseImporter;
class AircraftImporter extends BaseImporter
{
public $table = 'aircraft';
/**
* CONSTANTS
*/
public const SUBFLEET_NAME = 'Imported Aircraft';
public function run($start = 0)
{
$this->comment('--- AIRCRAFT IMPORT ---');
$subfleet = $this->getSubfleet();
$this->info('Subfleet ID is '.$subfleet->id);
$count = 0;
foreach ($this->db->readRows($this->table, $start) as $row) {
$where = [
'name' => $row->fullname,
'registration' => $row->registration,
];
$aircraft = Aircraft::firstOrCreate($where, [
'icao' => $row->icao,
'subfleet_id' => $subfleet->id,
'active' => $row->enabled,
]);
$this->idMapper->addMapping('aircraft', $row->id, $aircraft->id);
if ($aircraft->wasRecentlyCreated) {
$count++;
}
}
$this->info('Imported '.$count.' aircraft');
}
/**
* Return the subfleet
*
* @return mixed
*/
protected function getSubfleet()
{
$airline = Airline::first();
$subfleet = Subfleet::firstOrCreate([
'airline_id' => $airline->id,
'name' => self::SUBFLEET_NAME,
], ['type' => 'PHPVMS']);
return $subfleet;
}
}