80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
<?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 readonly class PublicHolidayService implements PublicHolidayServiceInterface
|
|
{
|
|
public function __construct(
|
|
private HttpClientInterface $client,
|
|
private string $holidayUrl
|
|
) {}
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
* @throws Exception
|
|
*/
|
|
public function getHolidaysDay(string $zone): array
|
|
{
|
|
$zone = strtolower(trim($zone));
|
|
$url = $this->holidayUrl."{$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 = $this->holidayUrl."{$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);
|
|
}
|
|
}
|