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\ApiClient;
|
||||||
use App\BusProNet\ApiClientException;
|
use App\BusProNet\ApiClientException;
|
||||||
use App\BusProNet\Model\Country;
|
use App\BusProNet\Model\Country;
|
||||||
|
use App\BusProNet\Model\NotificationResponse;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
use Symfony\Contracts\Cache\CacheInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
|
|
||||||
class CountryDataProvider
|
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
|
public function getAll(): array
|
||||||
@@ -20,11 +25,15 @@ class CountryDataProvider
|
|||||||
$countries = $this->cache->get('bpn_countries', function (ItemInterface $item) {
|
$countries = $this->cache->get('bpn_countries', function (ItemInterface $item) {
|
||||||
$item->expiresAfter(3600);
|
$item->expiresAfter(3600);
|
||||||
|
|
||||||
return $this
|
$response = $this->apiClient->getBaseData(ApiClient::TYPE_BASE_DATA_COUNTRIES);
|
||||||
->apiClient
|
|
||||||
->getBaseData(ApiClient::TYPE_BASE_DATA_COUNTRIES)
|
if ($response instanceof NotificationResponse) {
|
||||||
->getItems()
|
$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) {
|
} catch (ApiClientException $e) {
|
||||||
$countries = [];
|
$countries = [];
|
||||||
|
|||||||
@@ -5,14 +5,19 @@ namespace App\BusProNet\DataProvider;
|
|||||||
use App\BusProNet\ApiClient;
|
use App\BusProNet\ApiClient;
|
||||||
use App\BusProNet\ApiClientException;
|
use App\BusProNet\ApiClientException;
|
||||||
use App\BusProNet\Model\Hotel;
|
use App\BusProNet\Model\Hotel;
|
||||||
|
use App\BusProNet\Model\NotificationResponse;
|
||||||
use Psr\Cache\InvalidArgumentException;
|
use Psr\Cache\InvalidArgumentException;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
use Symfony\Contracts\Cache\CacheInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
|
|
||||||
class HotelDataProvider
|
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
|
public function getAll(): array
|
||||||
@@ -21,11 +26,15 @@ class HotelDataProvider
|
|||||||
$hotels = $this->cache->get('bpn_hotels', function (ItemInterface $item) {
|
$hotels = $this->cache->get('bpn_hotels', function (ItemInterface $item) {
|
||||||
$item->expiresAfter(3600);
|
$item->expiresAfter(3600);
|
||||||
|
|
||||||
return $this
|
$response = $this->apiClient->getBaseData(ApiClient::TYPE_BASE_DATA_HOTELS);
|
||||||
->apiClient
|
|
||||||
->getBaseData(ApiClient::TYPE_BASE_DATA_HOTELS)
|
if ($response instanceof NotificationResponse) {
|
||||||
->getItems()
|
$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) {
|
} catch (ApiClientException|InvalidArgumentException $e) {
|
||||||
$hotels = [];
|
$hotels = [];
|
||||||
|
|||||||
@@ -4,14 +4,19 @@ namespace App\BusProNet\DataProvider;
|
|||||||
|
|
||||||
use App\BusProNet\ApiClient;
|
use App\BusProNet\ApiClient;
|
||||||
use App\BusProNet\ApiClientException;
|
use App\BusProNet\ApiClientException;
|
||||||
|
use App\BusProNet\Model\NotificationResponse;
|
||||||
use App\BusProNet\Model\Pickup;
|
use App\BusProNet\Model\Pickup;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
use Symfony\Contracts\Cache\CacheInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
|
|
||||||
class PickupDataProvider
|
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
|
public function getAll(): array
|
||||||
@@ -20,11 +25,15 @@ class PickupDataProvider
|
|||||||
$pickups = $this->cache->get('bpn_pickups', function (ItemInterface $item) {
|
$pickups = $this->cache->get('bpn_pickups', function (ItemInterface $item) {
|
||||||
$item->expiresAfter(3600);
|
$item->expiresAfter(3600);
|
||||||
|
|
||||||
return $this
|
$response = $this->apiClient->getBaseData(ApiClient::TYPE_BASE_DATA_PICKUPS);
|
||||||
->apiClient
|
|
||||||
->getBaseData(ApiClient::TYPE_BASE_DATA_PICKUPS)
|
if ($response instanceof NotificationResponse) {
|
||||||
->getItems()
|
$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) {
|
} catch (ApiClientException $e) {
|
||||||
$pickups = [];
|
$pickups = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user