2018-03-21 08:17:11 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2019-09-17 01:08:26 +08:00
|
|
|
use App\Contracts\Command;
|
2018-03-22 08:12:36 +08:00
|
|
|
use App\Services\ImportService;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
class ImportCsv extends Command
|
|
|
|
{
|
|
|
|
protected $signature = 'phpvms:csv-import {type} {file}';
|
|
|
|
protected $description = 'Import from a CSV file';
|
|
|
|
|
|
|
|
private $importer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-22 08:12:36 +08:00
|
|
|
* @param ImportService $importer
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
2018-03-22 08:12:36 +08:00
|
|
|
public function __construct(ImportService $importer)
|
2018-03-21 08:17:11 +08:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2019-08-11 08:42:35 +08:00
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->importer = $importer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-23 23:37:34 +08:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return mixed|void
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$type = $this->argument('type');
|
|
|
|
$file = $this->argument('file');
|
|
|
|
|
2019-05-13 02:26:44 +08:00
|
|
|
if (\in_array($type, ['flight', 'flights'], true)) {
|
2018-03-21 08:17:11 +08:00
|
|
|
$status = $this->importer->importFlights($file);
|
|
|
|
} elseif ($type === 'aircraft') {
|
|
|
|
$status = $this->importer->importAircraft($file);
|
2019-05-13 02:26:44 +08:00
|
|
|
} elseif (\in_array($type, ['airport', 'airports'], true)) {
|
2018-03-21 08:17:11 +08:00
|
|
|
$status = $this->importer->importAirports($file);
|
|
|
|
} elseif ($type === 'subfleet') {
|
|
|
|
$status = $this->importer->importSubfleets($file);
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($status['success'] as $line) {
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->info($line);
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:37:34 +08:00
|
|
|
foreach ($status['errors'] as $line) {
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->error($line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|