Files
ephub/src/Controller/Board/DayController.php
T
2026-09-17 16:10:34 +02:00

60 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller\Board;
use App\Controller\Traits\BoardResponseTrait;
use App\Controller\Traits\CurrentUserTrait;
use App\Enum\ResourceType;
use App\Htmx\HxTrait;
use App\Routing\DateParam;
use App\Service\BoardContextFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* Picking a day -- or narrowing the span -- swaps the calendar and the floor plan in one
* response.
*
* This is the whole reason htmxOobResponse exists: two regions, one round trip, no client-side
* event choreography. Requests without the htmx header get a plain redirect rather than a bare
* pair of fragments, so the URL stays shareable.
*/
final class DayController extends AbstractController
{
use BoardResponseTrait;
use CurrentUserTrait;
use HxTrait;
public function __construct(private readonly BoardContextFactory $boardContext)
{
}
#[Route('/book/{type}/day/{date}', name: 'app_board_day', requirements: ['type' => '[a-z]+', 'date' => DateParam::PATTERN], methods: ['GET'])]
public function index(Request $request, string $type, string $date): Response
{
$resourceType = $this->resourceType($type);
$selected = DateParam::parseOrToday($date);
$time = $this->boardTime($request);
if (!$request->headers->has('HX-Request')) {
return $this->redirectToRoute('app_board', $this->boardUrlParameters($resourceType, $selected, $time));
}
return $this->boardResponse($resourceType, $selected, $time, ['calendar', 'floorplan']);
}
/**
* Route requirements cannot enumerate the enum, so {type} is matched loosely and resolved
* here -- an unknown kind is a 404 rather than a 500.
*/
private function resourceType(string $value): ResourceType
{
return ResourceType::tryFrom($value)
?? throw $this->createNotFoundException(\sprintf('Unknown bookable kind "%s".', $value));
}
}