56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\BusProNet\DataProvider;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\ApiClientException;
|
|
use App\BusProNet\Model\Country;
|
|
use App\BusProNet\Model\NotificationResponse;
|
|
use App\BusProNet\ResponseParserException;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Contracts\Cache\CacheInterface;
|
|
use Symfony\Contracts\Cache\ItemInterface;
|
|
|
|
class CountryDataProvider
|
|
{
|
|
public function __construct(
|
|
private readonly ApiClient $apiClient,
|
|
private readonly CacheInterface $cache,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
public function getAll(): array
|
|
{
|
|
try {
|
|
$countries = $this->cache->get('bpn_countries', function (ItemInterface $item) {
|
|
$item->expiresAfter(3600);
|
|
|
|
$response = $this->apiClient->getBaseData(ApiClient::TYPE_BASE_DATA_COUNTRIES);
|
|
|
|
if ($response instanceof NotificationResponse) {
|
|
$this->logger->error('Unable to fetch country base data from BusProNet. Code: '.$response->getCode().', Message: '.$response->getMessage());
|
|
|
|
throw new ApiClientException('Unable to fetch country base data from BusProNet');
|
|
}
|
|
|
|
return $response->getItems();
|
|
});
|
|
} catch (ApiClientException|ResponseParserException $e) {
|
|
$this->logger->error('Unable to fetch country base data from BusProNet: '.$e->getMessage());
|
|
$countries = [];
|
|
}
|
|
|
|
return $countries;
|
|
}
|
|
|
|
public function get(?string $token): ?Country
|
|
{
|
|
if (null === $token) {
|
|
return null;
|
|
}
|
|
|
|
return $this->getAll()[$token] ?? null;
|
|
}
|
|
}
|