Files
myep-team/src/BusProNet/DataProvider/HotelDataProvider.php
T

68 lines
1.6 KiB
PHP

<?php
namespace App\BusProNet\DataProvider;
use App\BusProNet\Model\Hotel;
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 ResponseMapper $responseMapper,
private readonly StaleWhileErrorCache $cache,
) {
}
/**
* @return array<int, Hotel> keyed by busProId
*/
public function getAll(): array
{
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
{
return $this->getAll()[$busProId] ?? null;
}
/**
* @return list<Hotel>
*/
public function findByCode(string $code): array
{
$hotels = [];
foreach ($this->getAll() as $hotel) {
$hotelCode = $hotel->getCode();
if (null === $hotelCode) {
continue;
}
if (str_starts_with($hotelCode, $code) || str_ends_with($hotelCode, $code)) {
$hotels[] = $hotel;
}
}
return $hotels;
}
}