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

66 lines
1.9 KiB
PHP

<?php
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;
class HotelDataProvider
{
public function __construct(
private readonly ApiClient $apiClient,
private readonly CacheInterface $cache,
private readonly LoggerInterface $logger,
) {
}
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;
}
public function get(int $busProId): ?Hotel
{
return $this->getAll()[$busProId] ?? null;
}
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)) {
$hotels[] = $hotel;
}
}
return $hotels;
}
}