ldc-cocotte/app/Console/Commands/UpsertEmployeesFormFile.php
Jérémy Munsch 3a1b34602c
fix(ImportEmployees): include matricule in error messages
fix(UpsertEmployeesFormFile): return failure on import errors

test(UpsertEmployeesFormFileTest): add tests for CSV import error handling

test(EmployeeCsvReaderServiceTest): validate error messages for invalid headers
2026-11-17 10:32:25 +01:00

56 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Contracts\Executor;
use App\Cqs\Actions\Employee\ImportEmployees;
use App\Cqs\Actions\Employee\ImportEmployeesInput;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Description('Synchronize all employees from a CSV file with Arcadia API')]
#[Signature('uwil:employees:upsert {file : The CSV file to import}')]
class UpsertEmployeesFormFile extends Command
{
public function handle(
Executor $executor,
ImportEmployees $action
): int {
$file = $this->argument('file');
if (! file_exists($file)) {
$this->error("File not found: {$file}");
return self::FAILURE;
}
$this->info('Starting synchronous employee import from CSV...');
$stream = new \SplFileObject($file, 'r');
if (! $stream->isReadable()) {
$this->error("Failed to open file: {$file}");
return self::FAILURE;
}
$importResult = $executor->execute($action, new ImportEmployeesInput($stream));
$this->info("Successfully inserted {$importResult->insertedCount} employees.");
$this->info("Successfully updated {$importResult->updatedCount} employees.");
if (! empty($importResult->errors)) {
$this->error('Errors occurred during import:');
foreach ($importResult->errors as $error) {
$this->error($error);
}
return self::FAILURE;
}
return self::SUCCESS;
}
}