|
|
|
|
@@ -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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|