- Added EmployeesImportResults class to encapsulate import results. - Created ImportEmployees action to handle CSV import and upsert employees. - Introduced ImportEmployeesInput class for input validation. - Developed EmployeeCsvReaderService for reading and parsing CSV files. - Implemented EmployeeCsvImportLine to map CSV rows to employee data. - Added EnhancedCsvReader for improved CSV handling. - Updated composer.json to include fusonic/csv-reader dependency. - Created tests for the new employee import functionality.
54 lines
1.5 KiB
PHP
54 lines
1.5 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 = fopen($file, 'r');
|
|
|
|
if (! $stream) {
|
|
$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::SUCCESS;
|
|
}
|
|
}
|