2021-09-24 03:13:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Controller\Api\Stations;
|
|
|
|
|
|
|
|
use App\Controller\Api\Traits\HasScheduleDisplay;
|
|
|
|
use App\Entity;
|
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2021-12-29 22:19:34 +01:00
|
|
|
use App\OpenApi;
|
2021-09-24 03:13:11 +02:00
|
|
|
use App\Radio\AutoDJ\Scheduler;
|
|
|
|
use Carbon\CarbonImmutable;
|
2021-12-29 06:36:30 +01:00
|
|
|
use OpenApi\Attributes as OA;
|
2023-03-02 15:33:57 +01:00
|
|
|
use Psr\Cache\CacheItemPoolInterface;
|
2021-09-24 03:13:11 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
2021-12-29 06:36:30 +01:00
|
|
|
#[OA\Get(
|
|
|
|
path: '/station/{station_id}/schedule',
|
|
|
|
operationId: 'getSchedule',
|
|
|
|
description: 'Return upcoming and currently ongoing schedule entries.',
|
|
|
|
tags: ['Stations: Schedules'],
|
|
|
|
parameters: [
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Parameter(ref: OpenApi::REF_STATION_ID_REQUIRED),
|
2021-12-29 06:36:30 +01:00
|
|
|
new OA\Parameter(
|
|
|
|
name: 'now',
|
|
|
|
description: 'The date/time to compare schedule items to. Defaults to the current date and time.',
|
|
|
|
in: 'query',
|
|
|
|
required: false,
|
|
|
|
schema: new OA\Schema(type: 'string')
|
|
|
|
),
|
|
|
|
new OA\Parameter(
|
|
|
|
name: 'rows',
|
|
|
|
description: 'The number of upcoming/ongoing schedule entries to return. Defaults to 5.',
|
|
|
|
in: 'query',
|
|
|
|
required: false,
|
|
|
|
schema: new OA\Schema(type: 'integer')
|
|
|
|
),
|
|
|
|
],
|
|
|
|
responses: [
|
|
|
|
new OA\Response(
|
|
|
|
response: 200,
|
|
|
|
description: 'Success',
|
|
|
|
content: new OA\JsonContent(
|
|
|
|
type: 'array',
|
|
|
|
items: new OA\Items(ref: '#/components/schemas/Api_StationSchedule')
|
|
|
|
)
|
|
|
|
),
|
2022-01-11 05:31:24 +01:00
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_NOT_FOUND, response: 404),
|
|
|
|
new OA\Response(ref: OpenApi::REF_RESPONSE_GENERIC_ERROR, response: 500),
|
2021-12-29 06:36:30 +01:00
|
|
|
]
|
|
|
|
)]
|
2022-05-22 02:27:28 +02:00
|
|
|
final class ScheduleAction
|
2021-09-24 03:13:11 +02:00
|
|
|
{
|
|
|
|
use HasScheduleDisplay;
|
|
|
|
|
2022-05-22 02:27:28 +02:00
|
|
|
public function __construct(
|
|
|
|
private readonly Scheduler $scheduler,
|
|
|
|
private readonly Entity\ApiGenerator\ScheduleApiGenerator $scheduleApiGenerator,
|
2023-03-02 15:33:57 +01:00
|
|
|
private readonly Entity\Repository\StationScheduleRepository $scheduleRepo,
|
|
|
|
private readonly CacheItemPoolInterface $psr6Cache
|
2022-05-22 02:27:28 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2021-09-24 03:13:11 +02:00
|
|
|
public function __invoke(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2022-05-24 07:50:43 +02:00
|
|
|
string $station_id
|
2021-09-24 03:13:11 +02:00
|
|
|
): ResponseInterface {
|
|
|
|
$station = $request->getStation();
|
|
|
|
$tz = $station->getTimezoneObject();
|
|
|
|
|
|
|
|
$queryParams = $request->getQueryParams();
|
|
|
|
|
|
|
|
if (isset($queryParams['start'])) {
|
2022-06-12 12:51:35 +02:00
|
|
|
$dateRange = $this->getScheduleDateRange($request);
|
2021-09-24 03:13:11 +02:00
|
|
|
|
|
|
|
$cacheKey = 'api_station_' . $station->getId() . '_schedule_'
|
2022-06-12 12:51:35 +02:00
|
|
|
. $dateRange->getStart()->format('Ymd') . '-'
|
|
|
|
. $dateRange->getEnd()->format('Ymd');
|
2021-09-24 03:13:11 +02:00
|
|
|
|
2023-03-02 15:33:57 +01:00
|
|
|
$cacheItem = $this->psr6Cache->getItem($cacheKey);
|
2021-09-24 03:13:11 +02:00
|
|
|
|
2023-03-02 15:33:57 +01:00
|
|
|
if (!$cacheItem->isHit()) {
|
|
|
|
$nowTz = CarbonImmutable::now($station->getTimezoneObject());
|
|
|
|
$events = $this->scheduleRepo->getAllScheduledItemsForStation($station);
|
2021-09-24 03:13:11 +02:00
|
|
|
|
2023-03-02 15:33:57 +01:00
|
|
|
$cacheItem->set(
|
|
|
|
$this->getEvents(
|
2022-06-12 12:51:35 +02:00
|
|
|
$dateRange,
|
2021-09-24 03:13:11 +02:00
|
|
|
$nowTz,
|
2022-05-22 02:27:28 +02:00
|
|
|
$this->scheduler,
|
2021-09-24 03:13:11 +02:00
|
|
|
$events,
|
2022-05-22 02:27:28 +02:00
|
|
|
[$this->scheduleApiGenerator, '__invoke']
|
2023-03-02 15:33:57 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$cacheItem->expiresAfter(600);
|
|
|
|
|
|
|
|
$this->psr6Cache->saveDeferred($cacheItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
$events = $cacheItem->get();
|
2021-09-24 03:13:11 +02:00
|
|
|
} else {
|
|
|
|
if (!empty($queryParams['now'])) {
|
|
|
|
$now = CarbonImmutable::parse($queryParams['now'], $tz);
|
|
|
|
$cacheKey = 'api_station_' . $station->getId() . '_schedule_' . $now->format('Ymd_gia');
|
|
|
|
} else {
|
|
|
|
$now = CarbonImmutable::now($tz);
|
|
|
|
$cacheKey = 'api_station_' . $station->getId() . '_schedule_upcoming';
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:33:57 +01:00
|
|
|
$cacheItem = $this->psr6Cache->getItem($cacheKey);
|
|
|
|
|
|
|
|
if (!$cacheItem->isHit()) {
|
|
|
|
$cacheItem->set($this->scheduleRepo->getUpcomingSchedule($station, $now));
|
|
|
|
$cacheItem->expiresAfter(60);
|
|
|
|
|
|
|
|
$this->psr6Cache->saveDeferred($cacheItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
$events = $cacheItem->get();
|
2021-09-24 03:13:11 +02:00
|
|
|
|
|
|
|
$rows = (int)$request->getQueryParam('rows', 5);
|
|
|
|
$events = array_slice($events, 0, $rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->withJson($events);
|
|
|
|
}
|
|
|
|
}
|