63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Exceptions\ExportFailureException;
|
|
use App\Models\Employee;
|
|
use App\Models\User;
|
|
use App\Notifications\EmployeesExportIsDone;
|
|
use App\Services\EmployeeExportService;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class SalariesExportJob implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
/** @param \App\Models\Employee[] $employeesToExport */
|
|
public function __construct(
|
|
private readonly array $employeesToExport,
|
|
private readonly User $mailToUser
|
|
) {
|
|
foreach ($employeesToExport as $employee) {
|
|
throw_unless(
|
|
$employee instanceof Employee,
|
|
\UnexpectedValueException::class,
|
|
'employeeToExport as to be an array of ' . Employee::class
|
|
);
|
|
}
|
|
}
|
|
|
|
public function handle(EmployeeExportService $employeesService): void
|
|
{
|
|
$diskName = 'employee';
|
|
$disk = Storage::disk($diskName);
|
|
$exportDateTime = Date::now()->format('d_m_Y-H_i_s');
|
|
$csvFileName = "{$exportDateTime}-{$this->mailToUser->id}-export_salaries.csv";
|
|
|
|
$exportStatus = $employeesService->export(
|
|
$diskName,
|
|
$csvFileName,
|
|
$this->employeesToExport,
|
|
);
|
|
|
|
throw_if(
|
|
$exportStatus === false,
|
|
ExportFailureException::class
|
|
);
|
|
|
|
$this->mailToUser->notify(new EmployeesExportIsDone(
|
|
fullname: "{$this->mailToUser->firstname} {$this->mailToUser->lastname}",
|
|
filePath: $csvFileName,
|
|
disk: $diskName
|
|
));
|
|
|
|
if ($disk->exists($csvFileName)) {
|
|
$disk->delete($csvFileName);
|
|
}
|
|
}
|
|
}
|