feat: split booking participant controllers

This commit is contained in:
Björn Fromme committed 2026-04-11 18:28:32 +02:00
1 parent 346256f895
commit f22ceae41c
10 files changed
+767 -673

No files matched your search

@@ -8,14 +8,11 @@ use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\TimeoutException;
use App\BusProNet\Model\Notification;
use App\Controller\Booking\Traits;
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Entity\User;
use App\Exception\TravelNotFoundException;
use App\Form\BookingEditType;
use App\Form\BookingParticipantType;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantEditDto;
use App\Htmx\HxTrait;
use App\Service\BookingEditDataLoaderService;
use App\Service\BookingEditDraftService;
@@ -45,7 +42,6 @@ class IndexController extends AbstractController
{
use BookingExceptionHandlerTrait;
use HxTrait;
use Traits\ParticipantCardFlowTrait;
public function __construct(
private readonly ApiClient $apiClient,
@@ -151,194 +147,6 @@ class IndexController extends AbstractController
return $this->render('booking/edit/index.html.twig', $templateData);
}
/**
* Edit single participant form.
*/
#[Route(
path: '/bookings/{id}/edit/participants/{index}',
name: 'app_booking_edit_participant',
requirements: ['id' => '\d+', 'index' => '\d+']
)]
#[IsGranted('ROLE_USER')]
public function editParticipant(int $id, int $index, Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
// Load form data from session
$bookingDto = $this->bookingService->getBookingDto($request, BookingDto::MODE_EDIT);
if (null === $bookingDto) {
$this->addFlash('error', 'Sitzung abgelaufen. Bitte neu laden.');
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
$participant = $bookingDto->participants[$index] ?? null;
if (null === $participant) {
throw new \InvalidArgumentException('Invalid participant index');
}
// Fetch booking data to check for canceled status
$bookingData = $this->dataLoader->fetchBookingData($id, $user);
if (null === $bookingData || $bookingData instanceof Notification) {
$this->addFlash('error', 'Buchungsdaten nicht (mehr) verfügbar');
return $this->redirectToRoute('app_bookings');
}
// Check if participant is canceled
$isCanceled = 'S' === ($bookingData->participantsStatus[$index] ?? null);
if ($isCanceled) {
$this->addFlash('warning', 'Stornierte Teilnehmer können nicht bearbeitet werden');
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
// Use cached availability data (populated during booking init)
$this->travelDataService->enrichWithFreshAvailabilities($bookingDto->travel);
// Create wrapper DTO for email uniqueness validation
$wrapper = new ParticipantEditDto(
participant: $participant,
bookingContext: $bookingDto,
);
// Create form for participant with booking context
$form = $this->createForm(BookingParticipantType::class, $wrapper, [
'booking_context' => $bookingDto,
'height_choices' => $this->getParameter('body_dimensions.height_choices'),
'weight_choices' => $this->getParameter('body_dimensions.weight_choices'),
'shoe_size_min' => $this->getParameter('body_dimensions.shoe_size_min'),
'shoe_size_max' => $this->getParameter('body_dimensions.shoe_size_max'),
]);
$form->handleRequest($request);
// Collect notifications from field handlers (run during PRE_SUBMIT)
$notifications = $this->collectAndClearNotifications($bookingDto);
if ($form->isSubmitted() && $form->isValid()) {
// Save updated booking data to session
$this->bookingService->saveBookingDto($request, $bookingDto, BookingDto::MODE_EDIT);
// Auto-save draft to database for data persistence
$this->draftService->saveDraft($user, $id, $bookingDto);
// Add notifications as flash messages (redirects destroy HTMX-triggered toasts)
$this->addNotificationsAsFlashMessages($notifications);
// Redirect back to cards
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingDto);
// Fetch mutable data for form constraints
$mutableData = $this->travelDataService->getMutabilityData($bookingData->dateId);
$templateData = [
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $mutableData,
'summaryData' => $summaryData,
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
'cancelRouteParams' => ['id' => $id],
];
return $this->render('booking/edit/participant.html.twig', $templateData);
}
/**
* HTMX endpoint for refreshing participant form without validation.
*/
#[Route(
path: '/bookings/{id}/edit/participants/{index}/refresh',
name: 'app_booking_edit_participant_refresh',
requirements: ['id' => '\d+', 'index' => '\d+'],
methods: ['POST']
)]
#[IsGranted('ROLE_USER')]
public function refreshParticipantForm(int $id, int $index, Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
// Load form data from session
$bookingDto = $this->bookingService->getBookingDto($request, BookingDto::MODE_EDIT);
if (null === $bookingDto) {
return new Response('Session expired. Please reload page.', Response::HTTP_BAD_REQUEST);
}
// Refresh availability data
$this->travelDataService->enrichWithFreshAvailabilities($bookingDto->travel);
// Fetch booking data and mutable data
$bookingData = $this->dataLoader->fetchBookingData($id, $user);
$mutableData = null !== $bookingData && !($bookingData instanceof Notification)
? $this->travelDataService->getMutabilityData($bookingData->dateId)
: null;
// Create wrapper DTO for form
$wrapper = new ParticipantEditDto(
participant: $bookingDto->participants[$index],
bookingContext: $bookingDto,
);
// Create form with validation disabled
$form = $this->createForm(BookingParticipantType::class, $wrapper, [
'booking_context' => $bookingDto,
'validation_groups' => false,
'height_choices' => $this->getParameter('body_dimensions.height_choices'),
'weight_choices' => $this->getParameter('body_dimensions.weight_choices'),
'shoe_size_min' => $this->getParameter('body_dimensions.shoe_size_min'),
'shoe_size_max' => $this->getParameter('body_dimensions.shoe_size_max'),
]);
$form->handleRequest($request);
// Collect notifications from field handlers
$notifications = $this->collectAndClearNotifications($bookingDto);
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingDto);
// Render form + sidebar using htmxOobResponse
$response = $this->htmxOobResponse(
'booking/_participant_form.html.twig',
['participant_form', 'booking_summary'],
[
'form' => $form,
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $mutableData,
'summaryData' => $summaryData,
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
'cancelRouteParams' => ['id' => $id],
]
);
// Add notifications to HX-Trigger header if present
if (false === empty($notifications)) {
$response->headers->set('HX-Trigger', json_encode([
'showNotifications' => $notifications,
]));
}
return $response;
}
/**
* Reloads booking data from API, discarding all session changes.
*
@@ -0,0 +1,185 @@
<?php
declare(strict_types=1);
namespace App\Controller\Booking\Edit;
use App\BusProNet\Model\Notification;
use App\Entity\User;
use App\Form\BookingParticipantType;
use App\Htmx\HxTrait;
use App\Service\BookingEditParticipantFormService;
use App\Service\ParticipantFormSupportService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
/**
* Edit participant sub-flow for booking edit mode.
*/
class ParticipantController extends AbstractController
{
use HxTrait;
public function __construct(
private readonly ParticipantFormSupportService $participantFormSupportService,
private readonly BookingEditParticipantFormService $participantFormService,
) {
}
/**
* Edit single participant form.
*/
#[Route(
path: '/bookings/{id}/edit/participants/{index}',
name: 'app_booking_edit_participant',
requirements: ['id' => '\d+', 'index' => '\d+']
)]
#[IsGranted('ROLE_USER')]
public function editParticipant(int $id, int $index, Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
$bookingDto = $this->participantFormService->loadBookingDto($request);
if (null === $bookingDto) {
$this->addFlash('error', 'Sitzung abgelaufen. Bitte neu laden.');
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
$this->participantFormSupportService->ensureParticipantExists($bookingDto, $index);
$bookingData = $this->participantFormService->fetchBookingData($id, $user);
if (null === $bookingData || $bookingData instanceof Notification) {
$this->addFlash('error', 'Buchungsdaten nicht (mehr) verfügbar');
return $this->redirectToRoute('app_bookings');
}
if ($this->participantFormService->isParticipantCanceled($bookingData, $index)) {
$this->addFlash('warning', 'Stornierte Teilnehmer können nicht bearbeitet werden');
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
$this->participantFormService->enrichTravelData($bookingDto);
$wrapper = $this->participantFormSupportService->createParticipantEditDto($bookingDto, $index);
$form = $this->createForm(
BookingParticipantType::class,
$wrapper,
$this->participantFormSupportService->getParticipantFormOptions($bookingDto)
);
$form->handleRequest($request);
$notifications = $this->participantFormSupportService->collectAndClearNotifications($bookingDto);
if ($form->isSubmitted() && $form->isValid()) {
$this->participantFormService->saveBookingDto($request, $bookingDto);
$this->participantFormService->saveDraft($user, $id, $bookingDto);
$this->addNotificationsAsFlashMessages($notifications);
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
$summaryData = $this->participantFormService->getSummaryData($bookingDto);
$mutableData = $this->participantFormService->getMutableData($bookingData->dateId);
return $this->render('booking/edit/participant.html', [
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $mutableData,
'summaryData' => $summaryData,
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
'cancelRouteParams' => ['id' => $id],
]);
}
/**
* HTMX endpoint for refreshing participant form without validation.
*/
#[Route(
path: '/bookings/{id}/edit/participants/{index}/refresh',
name: 'app_booking_edit_participant_refresh',
requirements: ['id' => '\d+', 'index' => '\d+'],
methods: ['POST']
)]
#[IsGranted('ROLE_USER')]
public function refreshParticipantForm(int $id, int $index, Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
$bookingDto = $this->participantFormService->loadBookingDto($request);
if (null === $bookingDto) {
return new Response('Session expired. Please reload page.', Response::HTTP_BAD_REQUEST);
}
$this->participantFormSupportService->ensureParticipantExists($bookingDto, $index);
$this->participantFormService->enrichTravelData($bookingDto);
$bookingData = $this->participantFormService->fetchBookingData($id, $user);
$mutableData = null !== $bookingData && !($bookingData instanceof Notification)
? $this->participantFormService->getMutableData($bookingData->dateId)
: null;
$wrapper = $this->participantFormSupportService->createParticipantEditDto($bookingDto, $index);
$form = $this->createForm(
BookingParticipantType::class,
$wrapper,
$this->participantFormSupportService->getParticipantFormOptions($bookingDto, true)
);
$form->handleRequest($request);
$notifications = $this->participantFormSupportService->collectAndClearNotifications($bookingDto);
$summaryData = $this->participantFormService->getSummaryData($bookingDto);
$response = $this->htmxOobResponse(
'booking/_participant_form.html.twig',
['participant_form', 'booking_summary'],
[
'form' => $form,
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $mutableData,
'summaryData' => $summaryData,
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
'cancelRouteParams' => ['id' => $id],
]
);
if (false === empty($notifications)) {
$response->headers->set('HX-Trigger', json_encode([
'showNotifications' => $notifications,
]));
}
return $response;
}
/**
* @param array<array{type: string, message: string}> $notifications
*/
private function addNotificationsAsFlashMessages(array $notifications): void
{
foreach ($notifications as $notification) {
$this->addFlash($notification['type'], $notification['message']);
}
}
}