phpvms/app/Console/Commands/ImportCsv.php

56 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use App\Contracts\Command;
use App\Services\ImportService;
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
*
* @param ImportService $importer
*/
public function __construct(ImportService $importer)
{
parent::__construct();
$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
*/
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)) {
$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)) {
$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) {
$this->info($line);
}
2018-03-23 23:37:34 +08:00
foreach ($status['errors'] as $line) {
$this->error($line);
}
}
}