45 lines
1.1 KiB
PHP
45 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\BusProNet\DataProvider;
|
|
|
|
use App\BusProNet\Model\Pickup;
|
|
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 ResponseMapper $responseMapper,
|
|
private readonly StaleWhileErrorCache $cache,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return array<int, Pickup> keyed by busProId
|
|
*/
|
|
public function getAll(): array
|
|
{
|
|
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
|
|
{
|
|
return $this->getAll()[$busProId] ?? null;
|
|
}
|
|
}
|