49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\BusProNet\DataProvider;
|
|
|
|
use App\BusProNet\Model\Country;
|
|
use App\MyEp\Api\ApiClient;
|
|
use App\MyEp\Api\ResponseMapper;
|
|
use App\MyEp\Cache\StaleWhileErrorCache;
|
|
|
|
/**
|
|
* Country base data, read from MyE&P rather than from BusProNet directly.
|
|
*
|
|
* Keyed by the raw BusProNet token, not by an ISO code: the token is what a hotel's
|
|
* country and a teamer's nationality join against, and that vocabulary stays raw.
|
|
*/
|
|
class CountryDataProvider
|
|
{
|
|
private const CACHE_KEY = 'myep_countries';
|
|
private const TTL = 3600;
|
|
|
|
public function __construct(
|
|
private readonly ApiClient $apiClient,
|
|
private readonly ResponseMapper $responseMapper,
|
|
private readonly StaleWhileErrorCache $cache,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return array<string, Country> keyed by token
|
|
*/
|
|
public function getAll(): array
|
|
{
|
|
return $this->cache->get(
|
|
self::CACHE_KEY,
|
|
self::TTL,
|
|
fn (): array => $this->responseMapper->mapCountries($this->apiClient->get('api/countries')),
|
|
);
|
|
}
|
|
|
|
public function get(?string $token): ?Country
|
|
{
|
|
if (null === $token) {
|
|
return null;
|
|
}
|
|
|
|
return $this->getAll()[$token] ?? null;
|
|
}
|
|
}
|