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,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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user