fix: make import command resilient to api errors
This commit is contained in:
@@ -5,13 +5,18 @@ namespace App\BusProNet\DataProvider;
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\ApiClientException;
|
||||
use App\BusProNet\Model\Country;
|
||||
use App\BusProNet\Model\NotificationResponse;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class CountryDataProvider
|
||||
{
|
||||
public function __construct(private readonly ApiClient $apiClient, private readonly CacheInterface $cache)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
@@ -20,11 +25,15 @@ class CountryDataProvider
|
||||
$countries = $this->cache->get('bpn_countries', function (ItemInterface $item) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
return $this
|
||||
->apiClient
|
||||
->getBaseData(ApiClient::TYPE_BASE_DATA_COUNTRIES)
|
||||
->getItems()
|
||||
;
|
||||
$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 $e) {
|
||||
$countries = [];
|
||||
|
||||
@@ -5,14 +5,19 @@ namespace App\BusProNet\DataProvider;
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\ApiClientException;
|
||||
use App\BusProNet\Model\Hotel;
|
||||
use App\BusProNet\Model\NotificationResponse;
|
||||
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)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
@@ -21,11 +26,15 @@ class HotelDataProvider
|
||||
$hotels = $this->cache->get('bpn_hotels', function (ItemInterface $item) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
return $this
|
||||
->apiClient
|
||||
->getBaseData(ApiClient::TYPE_BASE_DATA_HOTELS)
|
||||
->getItems()
|
||||
;
|
||||
$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 $e) {
|
||||
$hotels = [];
|
||||
|
||||
@@ -4,14 +4,19 @@ namespace App\BusProNet\DataProvider;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\ApiClientException;
|
||||
use App\BusProNet\Model\NotificationResponse;
|
||||
use App\BusProNet\Model\Pickup;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class PickupDataProvider
|
||||
{
|
||||
public function __construct(private readonly ApiClient $apiClient, private readonly CacheInterface $cache)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
@@ -20,11 +25,15 @@ class PickupDataProvider
|
||||
$pickups = $this->cache->get('bpn_pickups', function (ItemInterface $item) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
return $this
|
||||
->apiClient
|
||||
->getBaseData(ApiClient::TYPE_BASE_DATA_PICKUPS)
|
||||
->getItems()
|
||||
;
|
||||
$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 $e) {
|
||||
$pickups = [];
|
||||
|
||||
Reference in New Issue
Block a user