Ajout du service pour récupérer les jours fériés (#1)
Reviewed-on: #1 Co-authored-by: kevin <kevin@yuno.malio.fr> Co-committed-by: kevin <kevin@yuno.malio.fr>
This commit was merged in pull request #1.
This commit is contained in:
29
src/ApiResource/PublicHoliday.php
Normal file
29
src/ApiResource/PublicHoliday.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\State\PublicHolidayProvider;
|
||||
use App\State\PublicHolidayYearProvider;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/public-holidays/{zone}',
|
||||
provider: PublicHolidayProvider::class
|
||||
),
|
||||
new Get(
|
||||
uriTemplate: '/public-holidays/{zone}/{years}',
|
||||
provider: PublicHolidayYearProvider::class
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class PublicHoliday
|
||||
{
|
||||
public function __construct(
|
||||
public array $days = []
|
||||
) {}
|
||||
}
|
||||
80
src/Service/PublicHolidayService.php
Normal file
80
src/Service/PublicHolidayService.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Throwable;
|
||||
|
||||
final class PublicHolidayService implements PublicHolidayServiceInterface
|
||||
{
|
||||
private const string BASE_URL = 'https://calendrier.api.gouv.fr/jours-feries/';
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getHolidaysDay(string $zone): array
|
||||
{
|
||||
$zone = strtolower(trim($zone));
|
||||
$url = self::BASE_URL."{$zone}.json";
|
||||
|
||||
try {
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
$url
|
||||
);
|
||||
} catch (TransportExceptionInterface) {
|
||||
throw new RuntimeException('Unable to reach public holidays API.');
|
||||
} catch (ClientExceptionInterface) {
|
||||
throw new RuntimeException('Invalid zone provided for public holidays.');
|
||||
} catch (ServerExceptionInterface) {
|
||||
throw new RuntimeException('Public holidays API is temporarily unavailable.');
|
||||
} catch (Throwable) {
|
||||
throw new RuntimeException('Unexpected error while fetching public holidays.');
|
||||
}
|
||||
|
||||
return json_decode($response->getContent(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
*/
|
||||
public function getHolidaysDayByYears(string $zone, string $years): array
|
||||
{
|
||||
$zone = strtolower(trim($zone));
|
||||
$years = trim($years);
|
||||
$url = self::BASE_URL."{$zone}/{$years}.json";
|
||||
|
||||
try {
|
||||
$response = $this->client->request('GET', $url);
|
||||
} catch (TransportExceptionInterface) {
|
||||
throw new RuntimeException('Unable to reach public holidays API.');
|
||||
} catch (ClientExceptionInterface) {
|
||||
throw new RuntimeException('Invalid zone or year provided for public holidays.');
|
||||
} catch (ServerExceptionInterface) {
|
||||
throw new RuntimeException('Public holidays API is temporarily unavailable.');
|
||||
} catch (Throwable) {
|
||||
throw new RuntimeException('Unexpected error while fetching public holidays.');
|
||||
}
|
||||
|
||||
return json_decode($response->getContent(), true);
|
||||
}
|
||||
}
|
||||
12
src/Service/PublicHolidayServiceInterface.php
Normal file
12
src/Service/PublicHolidayServiceInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
interface PublicHolidayServiceInterface
|
||||
{
|
||||
public function getHolidaysDay(string $zone): array;
|
||||
|
||||
public function getHolidaysDayByYears(string $zone, string $years): array;
|
||||
}
|
||||
24
src/State/PublicHolidayProvider.php
Normal file
24
src/State/PublicHolidayProvider.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\PublicHoliday as PublicHolidayResource;
|
||||
use App\Service\PublicHolidayServiceInterface;
|
||||
|
||||
final readonly class PublicHolidayProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private PublicHolidayServiceInterface $publicHolidayService
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
|
||||
{
|
||||
$holidaysDay = $this->publicHolidayService->getHolidaysDay($uriVariables['zone']);
|
||||
|
||||
return $holidaysDay ? new PublicHolidayResource($holidaysDay) : null;
|
||||
}
|
||||
}
|
||||
24
src/State/PublicHolidayYearProvider.php
Normal file
24
src/State/PublicHolidayYearProvider.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\PublicHoliday;
|
||||
use App\Service\PublicHolidayServiceInterface;
|
||||
|
||||
final readonly class PublicHolidayYearProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private PublicHolidayServiceInterface $PublicHolidayYearService
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
|
||||
{
|
||||
$holidaysDay = $this->PublicHolidayYearService->getHolidaysDayByYears($uriVariables['zone'], $uriVariables['years']);
|
||||
|
||||
return $holidaysDay ? new PublicHoliday($holidaysDay) : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user