45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use const PHP_EOL;
|
|
|
|
use App\Models\School;
|
|
use App\Services\CsvImportService;
|
|
use Illuminate\Console\Command;
|
|
|
|
class importCheckSchoolsCommand extends Command
|
|
{
|
|
protected $signature = 'import:check-schools';
|
|
|
|
protected $description = 'Check the import file for schools that do not exist in the database';
|
|
|
|
protected $csvImporter;
|
|
|
|
public function __construct(CsvImportService $csvImporter)
|
|
{
|
|
parent::__construct();
|
|
$this->csvImporter = $csvImporter;
|
|
}
|
|
|
|
public function handle(): void
|
|
{
|
|
$rows = $this->csvImporter->readCsv(storage_path('app/import/import.csv'));
|
|
$checkedSchools = collect();
|
|
foreach ($rows as $row) {
|
|
if ($checkedSchools->contains($row['School'])) {
|
|
continue;
|
|
}
|
|
$checkedSchools->push($row['School']);
|
|
if (School::where('name', $row['School'])->count() > 0) {
|
|
$this->info('School '.$row['School'].' already exists');
|
|
} else {
|
|
$this->newLine();
|
|
$this->alert('School '.$row['School'].' does not exist'.PHP_EOL.'Creating school...');
|
|
School::create(['name' => $row['School']]);
|
|
$this->info('School '.$row['School'].' created');
|
|
}
|
|
}
|
|
}
|
|
}
|