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.
65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Dto\PublicHolidayDto;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
class PublicHolidaysApiService
|
|
{
|
|
private const string CALENDAR_API_URL = 'https://calendrier.api.gouv.fr/jours-feries/metropole/:year.json';
|
|
|
|
private const int API_CALL_TIMEOUT_SECONDS = 15;
|
|
|
|
/**
|
|
* @return list<PublicHolidayDto>
|
|
*/
|
|
public function fetchForYear(int $year): array
|
|
{
|
|
Assert::greaterThanEq($year, 2026);
|
|
|
|
$url = str_replace(':year', strval($year), self::CALENDAR_API_URL);
|
|
$response = Http::timeout(self::API_CALL_TIMEOUT_SECONDS)->get($url);
|
|
|
|
throw_unless(
|
|
$response->successful(),
|
|
\UnexpectedValueException::class,
|
|
'Failed to fetch calendar from api'
|
|
);
|
|
|
|
$outputList = $response
|
|
->collect()
|
|
->map(fn ($label, $date): array => [
|
|
'date' => $date,
|
|
'label' => $label,
|
|
])->values()->all();
|
|
|
|
$validator = Validator::make($outputList, [
|
|
'*' => 'array',
|
|
'*.date' => ['required', 'date'],
|
|
'*.label' => ['required', 'string'],
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
throw new \UnexpectedValueException(
|
|
'Invalid response: ' . json_encode($validator->errors()->messages())
|
|
);
|
|
}
|
|
|
|
$timezone = new \DateTimeZone('Europe/Paris');
|
|
|
|
return array_values(array_map(
|
|
fn (array $entry): PublicHolidayDto => new PublicHolidayDto(
|
|
date: CarbonImmutable::parse(Arr::string($entry, 'date'), $timezone),
|
|
label: Arr::string($entry, 'label'),
|
|
),
|
|
$outputList
|
|
));
|
|
}
|
|
}
|