ldc-cocotte/app/Jobs/EnsurePublicHolidaysSyncedJob.php
Jérémy Munsch faef51091e
feat(pickup-exceptions): implement upsert and query for public holidays
Added functionality to upsert pickup exceptions for public holidays
and query for auto-updated exceptions. This includes new classes
for handling input and service integration with the public holidays API.
2026-11-15 20:04:29 +01:00

80 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Contracts\Executor;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYear;
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput;
use App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQuery;
use App\Cqs\Queries\PickupExceptions\HasAutoUpdatedPickupExceptionsForYearQueryInput;
use App\Services\PublicHolidaysApiService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Date;
class EnsurePublicHolidaysSyncedJob implements ShouldQueue
{
use Queueable;
public int $tries = 3;
/**
* @var list<int>
*/
public array $backoff = [10, 30];
public function handle(
Executor $executor,
HasAutoUpdatedPickupExceptionsForYearQuery $hasHolidaysQuery,
PublicHolidaysApiService $apiService,
UpsertShopsPickupExceptionsForYear $action,
): void {
$now = Date::now();
if ($now->year >= 2026) {
$this->syncYearIfMissing(
$now->year,
$executor,
$hasHolidaysQuery,
$apiService,
$action,
);
}
if ($now->month >= 11) {
$this->syncYearIfMissing(
$now->year + 1,
$executor,
$hasHolidaysQuery,
$apiService,
$action,
);
}
}
private function syncYearIfMissing(
int $year,
Executor $executor,
HasAutoUpdatedPickupExceptionsForYearQuery $hasHolidaysQuery,
PublicHolidaysApiService $apiService,
UpsertShopsPickupExceptionsForYear $action,
): void {
$alreadySynced = $executor->execute(
$hasHolidaysQuery,
new HasAutoUpdatedPickupExceptionsForYearQueryInput($year),
);
if ($alreadySynced) {
return;
}
$holidays = $apiService->fetchForYear($year);
$executor->execute(
$action,
new UpsertShopsPickupExceptionsForYearInput($year, $holidays),
);
}
}