From 2cfb05e5de6d01caf2149f1049899b998d9c33cc Mon Sep 17 00:00:00 2001 From: tristan Date: Tue, 17 Mar 2026 15:05:43 +0100 Subject: [PATCH] =?UTF-8?q?feat=20:=20ajout=20de=20cache=20sur=20la=20r?= =?UTF-8?q?=C3=A9cup=C3=A9ration=20des=20jours=20f=C3=A9ri=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Service/PublicHolidayService.php | 73 ++++++++++++++++------------ 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/src/Service/PublicHolidayService.php b/src/Service/PublicHolidayService.php index 005a02a..176fb18 100644 --- a/src/Service/PublicHolidayService.php +++ b/src/Service/PublicHolidayService.php @@ -6,6 +6,8 @@ namespace App\Service; use Exception; use RuntimeException; +use Symfony\Contracts\Cache\CacheInterface; +use Symfony\Contracts\Cache\ItemInterface; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -17,7 +19,8 @@ final readonly class PublicHolidayService implements PublicHolidayServiceInterfa { public function __construct( private HttpClientInterface $client, - private string $holidayUrl + private string $holidayUrl, + private CacheInterface $cache, ) {} /** @@ -30,24 +33,29 @@ final readonly class PublicHolidayService implements PublicHolidayServiceInterfa public function getHolidaysDay(string $zone): array { $zone = strtolower(trim($zone)); - $url = $this->holidayUrl."{$zone}.json"; + $key = "public_holidays_{$zone}_all"; - 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 $this->cache->get($key, function (ItemInterface $item) use ($zone): array { + $item->expiresAfter(30 * 86400); + $url = $this->holidayUrl."{$zone}.json"; - return json_decode($response->getContent(), true); + 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); + }); } /** @@ -60,20 +68,25 @@ final readonly class PublicHolidayService implements PublicHolidayServiceInterfa { $zone = strtolower(trim($zone)); $years = trim($years); - $url = $this->holidayUrl."{$zone}/{$years}.json"; + $key = "public_holidays_{$zone}_{$years}"; - 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 $this->cache->get($key, function (ItemInterface $item) use ($zone, $years): array { + $item->expiresAfter(30 * 86400); + $url = $this->holidayUrl."{$zone}/{$years}.json"; - return json_decode($response->getContent(), true); + 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); + }); } }