feat: read BusProNet base data from the MyE&P API
This commit is contained in:
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
|
||||
|
||||
@@ -2,47 +2,40 @@
|
||||
|
||||
namespace App\BusProNet\DataProvider;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\ApiClientException;
|
||||
use App\BusProNet\Model\Hotel;
|
||||
use App\BusProNet\Model\NotificationResponse;
|
||||
use App\BusProNet\ResponseParserException;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Hotel base data, read from MyE&P rather than from BusProNet directly.
|
||||
*
|
||||
* The models and the public interface are unchanged from when this spoke the BusProNet
|
||||
* socket protocol: every caller keys on the BusProNet idbuspro, which is exactly what
|
||||
* MyE&P reports as the hotel id.
|
||||
*/
|
||||
class HotelDataProvider
|
||||
{
|
||||
private const CACHE_KEY = 'myep_hotels';
|
||||
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<int, Hotel> keyed by busProId
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
try {
|
||||
$hotels = $this->cache->get('bpn_hotels', function (ItemInterface $item) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
$response = $this->apiClient->getBaseData(ApiClient::TYPE_BASE_DATA_HOTELS);
|
||||
|
||||
if ($response instanceof NotificationResponse) {
|
||||
$this->logger->error('Unable to fetch hotel base data from BusProNet. Code: '.$response->getCode().', Message: '.$response->getMessage());
|
||||
|
||||
throw new ApiClientException('Unable to fetch hotel base data from BusProNet');
|
||||
}
|
||||
|
||||
return $response->getItems();
|
||||
});
|
||||
} catch (ApiClientException|InvalidArgumentException|ResponseParserException $e) {
|
||||
$this->logger->error('Unable to fetch hotel base data from BusProNet: '.$e->getMessage());
|
||||
$hotels = [];
|
||||
}
|
||||
|
||||
return $hotels;
|
||||
return $this->cache->get(
|
||||
self::CACHE_KEY,
|
||||
self::TTL,
|
||||
fn (): array => $this->responseMapper->mapHotels($this->apiClient->get('api/hotels')),
|
||||
);
|
||||
}
|
||||
|
||||
public function get(int $busProId): ?Hotel
|
||||
@@ -50,12 +43,21 @@ class HotelDataProvider
|
||||
return $this->getAll()[$busProId] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<Hotel>
|
||||
*/
|
||||
public function findByCode(string $code): array
|
||||
{
|
||||
$hotels = [];
|
||||
|
||||
foreach ($this->getAll() as $hotel) {
|
||||
if (str_starts_with($hotel->getCode(), $code) || str_ends_with($hotel->getCode(), $code)) {
|
||||
$hotelCode = $hotel->getCode();
|
||||
|
||||
if (null === $hotelCode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str_starts_with($hotelCode, $code) || str_ends_with($hotelCode, $code)) {
|
||||
$hotels[] = $hotel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,46 +2,39 @@
|
||||
|
||||
namespace App\BusProNet\DataProvider;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\ApiClientException;
|
||||
use App\BusProNet\Model\NotificationResponse;
|
||||
use App\BusProNet\Model\Pickup;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Boarding point base data, read from MyE&P rather than from BusProNet directly.
|
||||
*
|
||||
* These are the stops themselves, without a departure time: a time belongs to a stop on
|
||||
* a given travel date and is carried by the destination's pickups instead.
|
||||
*/
|
||||
class PickupDataProvider
|
||||
{
|
||||
private const CACHE_KEY = 'myep_pickups';
|
||||
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<int, Pickup> keyed by busProId
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
try {
|
||||
$pickups = $this->cache->get('bpn_pickups', function (ItemInterface $item) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
$response = $this->apiClient->getBaseData(ApiClient::TYPE_BASE_DATA_PICKUPS);
|
||||
|
||||
if ($response instanceof NotificationResponse) {
|
||||
$this->logger->error('Unable to fetch pickup base data from BusProNet. Code: '.$response->getCode().', Message: '.$response->getMessage());
|
||||
|
||||
throw new ApiClientException('Unable to fetch pickup base data from BusProNet');
|
||||
}
|
||||
|
||||
return $response->getItems();
|
||||
});
|
||||
} catch (ApiClientException|ResponseParserException $e) {
|
||||
$this->logger->error('Unable to fetch pickup base data from BusProNet: '.$e->getMessage());
|
||||
$pickups = [];
|
||||
}
|
||||
|
||||
return $pickups;
|
||||
return $this->cache->get(
|
||||
self::CACHE_KEY,
|
||||
self::TTL,
|
||||
fn (): array => $this->responseMapper->mapPickups($this->apiClient->get('api/pickups')),
|
||||
);
|
||||
}
|
||||
|
||||
public function get(int $busProId): ?Pickup
|
||||
|
||||
Reference in new issue
Block a user