2018-03-21 08:17:11 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Console\Command;
|
2018-03-22 08:12:36 +08:00
|
|
|
use App\Services\ImportService;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ImportCsv
|
|
|
|
* @package App\Console\Commands
|
|
|
|
*/
|
|
|
|
class ImportCsv extends Command
|
|
|
|
{
|
|
|
|
protected $signature = 'phpvms:csv-import {type} {file}';
|
|
|
|
protected $description = 'Import from a CSV file';
|
|
|
|
|
|
|
|
private $importer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import constructor.
|
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();
|
|
|
|
$this->importer = $importer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed|void
|
|
|
|
* @throws \League\Csv\Exception
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$type = $this->argument('type');
|
|
|
|
$file = $this->argument('file');
|
|
|
|
|
|
|
|
if (\in_array($type, ['flight', 'flights'])) {
|
|
|
|
$status = $this->importer->importFlights($file);
|
|
|
|
} elseif ($type === 'aircraft') {
|
|
|
|
$status = $this->importer->importAircraft($file);
|
|
|
|
} elseif (\in_array($type, ['airport', 'airports'])) {
|
|
|
|
$status = $this->importer->importAirports($file);
|
|
|
|
} elseif ($type === 'subfleet') {
|
|
|
|
$status = $this->importer->importSubfleets($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($status['success'] as $line) {
|
|
|
|
$this->info($line);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($status['failed'] as $line) {
|
|
|
|
$this->error($line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|