feat: read BusProNet base data from the MyE&P API

This commit is contained in:
Björn Fromme authored and Björn Fromme committed 2026-09-29 11:08:04 +02:00
1 parent 1505f9b50b
commit 2e9328fc51
37 files changed
+2302 -148

No files matched your search

@@ -2,46 +2,39 @@
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;
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 CacheInterface $cache,
private readonly LoggerInterface $logger,
private readonly ResponseMapper $responseMapper,
private readonly StaleWhileErrorCache $cache,
) {
}
/**
* @return array<string, Country> keyed by token
*/
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;
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