feat: read BusProNet base data from the MyE&P API

This commit is contained in:
Björn Fromme authored and Björn Fromme committed 2026-09-29 11:08:04 +02:00
1 parent 1505f9b50b
commit 2e9328fc51
37 files changed
+2302 -148

No files matched your search

@@ -2,46 +2,39 @@
namespace App\BusProNet\DataProvider;
use App\BusProNet\ApiClient;
use App\BusProNet\ApiClientException;
use App\BusProNet\Model\NotificationResponse;
use App\BusProNet\Model\Pickup;
use App\BusProNet\ResponseParserException;
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;
/**
* Boarding point base data, read from MyE&P rather than from BusProNet directly.
*
* These are the stops themselves, without a departure time: a time belongs to a stop on
* a given travel date and is carried by the destination's pickups instead.
*/
class PickupDataProvider
{
private const CACHE_KEY = 'myep_pickups';
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, Pickup> keyed by busProId
*/
public function getAll(): array
{
try {
$pickups = $this->cache->get('bpn_pickups', function (ItemInterface $item) {
$item->expiresAfter(3600);
$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|ResponseParserException $e) {
$this->logger->error('Unable to fetch pickup base data from BusProNet: '.$e->getMessage());
$pickups = [];
}
return $pickups;
return $this->cache->get(
self::CACHE_KEY,
self::TTL,
fn (): array => $this->responseMapper->mapPickups($this->apiClient->get('api/pickups')),
);
}
public function get(int $busProId): ?Pickup