feat: centralize create/edit flow contexts, extract edit submit service

This commit is contained in:
Björn Fromme
2026-04-12 10:17:04 +02:00
parent f903f17ebd
commit 69705052b3
28 changed files with 1342 additions and 423 deletions
@@ -9,10 +9,9 @@ use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Form\BookingCreateStep1Type;
use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Service\BookingCreateContextFactory;
use App\Service\BookingService;
use App\Service\BookingRoomSelectionService;
use App\Service\BookingSessionService;
use App\Service\BookingSummaryDataService;
use App\Service\RoomPricingCalculator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@@ -33,9 +32,8 @@ class Step1Controller extends AbstractController
public function __construct(
private readonly BookingService $bookingService,
private readonly BookingRoomSelectionService $roomSelectionService,
private readonly BookingSessionService $bookingSessionService,
private readonly BookingSummaryDataService $summaryDataService,
private readonly BookingCreateContextFactory $createContextFactory,
) {
}
@@ -82,17 +80,14 @@ class Step1Controller extends AbstractController
return $this->redirectToRoute('app_booking_create_step_2');
}
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto, RoomPricingCalculator::PRICING_MODE_SELECTION);
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
$groupedRooms = $this->roomSelectionService->groupRoomsBySelectionType($availableRooms);
$context = $this->createContextFactory->create(
$bookingCreateDto,
RoomPricingCalculator::PRICING_MODE_SELECTION
);
return $this->render('booking/create/step_1.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'summaryData' => $summaryData,
'bookingCreateContext' => $context,
'form' => $form->createView(),
'groupedRooms' => $groupedRooms,
]);
}
@@ -103,7 +98,7 @@ class Step1Controller extends AbstractController
#[Route('/bookings/create/refresh', name: 'app_booking_create_step_1_refresh', methods: ['POST'])]
public function refresh(Request $request): Response
{
$result = $this->getOrCreateBookingCreateDtoForHtmx($this->bookingService, $request);
$result = $this->getOrCreateBookingCreateDtoForHtmx($this->bookingSessionService, $request);
if ($result instanceof Response) {
return $result;
}
@@ -115,10 +110,10 @@ class Step1Controller extends AbstractController
]);
$form->handleRequest($request);
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto, RoomPricingCalculator::PRICING_MODE_SELECTION);
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
$groupedRooms = $this->roomSelectionService->groupRoomsBySelectionType($availableRooms);
$context = $this->createContextFactory->create(
$bookingCreateDto,
RoomPricingCalculator::PRICING_MODE_SELECTION
);
// The DTO is now updated with the latest selection.
// We can now render the blocks with the fresh data.
@@ -127,9 +122,7 @@ class Step1Controller extends AbstractController
['room_selection_form', 'booking_summary'],
[
'form' => $form->createView(),
'bookingCreateDto' => $bookingCreateDto,
'summaryData' => $summaryData,
'groupedRooms' => $groupedRooms,
'bookingCreateContext' => $context,
]
);
}
@@ -9,11 +9,10 @@ use App\Controller\Booking\Traits\BookingCreateTrait;
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Form\BookingCreateStep2Type;
use App\Form\Model\BookingDto;
use App\Service\BookingCreateContextFactory;
use App\Service\BookingService;
use App\Service\BookingParticipantCountService;
use App\Service\BookingSessionService;
use App\Service\BookingSummaryDataService;
use App\Service\ParticipantCardDataService;
use App\Service\ParticipantPrepopulationService;
use App\Service\RoomAssignmentService;
use App\Service\TravelDataService;
@@ -37,10 +36,9 @@ class Step2Controller extends AbstractController
private readonly BookingService $bookingService,
private readonly BookingParticipantCountService $participantCountService,
private readonly BookingSessionService $bookingSessionService,
private readonly BookingSummaryDataService $summaryDataService,
private readonly BookingCreateContextFactory $createContextFactory,
private readonly TravelDataService $travelDataService,
private readonly RoomAssignmentService $roomAssignmentService,
private readonly ParticipantCardDataService $participantCardService,
private readonly ParticipantPrepopulationService $prepopulationService,
) {
}
@@ -106,18 +104,11 @@ class Step2Controller extends AbstractController
return $this->redirectToRoute('app_booking_create_step_3');
}
// Always generate card data with validation state to show completeness
$cardsData = $this->participantCardService->getAllCardsDataWithValidation($bookingCreateDto);
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$context = $this->createContextFactory->createWithParticipantCards($bookingCreateDto, $form->isSubmitted());
$templateData = [
'form' => $form->createView(),
'bookingDto' => $bookingCreateDto,
'cardsData' => $cardsData,
'summaryData' => $summaryData,
'isSubmitted' => $form->isSubmitted(),
'bookingCreateContext' => $context,
];
return $this->render('booking/create/step_2.html.twig', $templateData);
@@ -7,12 +7,13 @@ namespace App\Controller\Booking\Create;
use App\Form\BookingParticipantType;
use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Service\BookingCreateContextFactory;
use App\Service\BookingService;
use App\Service\BookingSessionService;
use App\Service\BookingSummaryDataService;
use App\Service\ParticipantFormSupportService;
use App\Service\ParticipantPrepopulationService;
use App\Service\TravelDataService;
use App\Service\RoomPricingCalculator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
@@ -29,7 +30,7 @@ class Step2ParticipantController extends AbstractController
public function __construct(
private readonly BookingService $bookingService,
private readonly BookingSessionService $bookingSessionService,
private readonly BookingSummaryDataService $summaryDataService,
private readonly BookingCreateContextFactory $createContextFactory,
private readonly TravelDataService $travelDataService,
private readonly ParticipantPrepopulationService $prepopulationService,
private readonly ParticipantFormSupportService $participantFormSupportService,
@@ -128,11 +129,15 @@ class Step2ParticipantController extends AbstractController
int $index,
BookingDto $bookingDto,
): Response {
$bookingCreateContext = $this->createContextFactory->create(
$bookingDto,
RoomPricingCalculator::PRICING_MODE_SELECTION
);
return $this->render('booking/create/step_2_participant.html.twig', [
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'summaryData' => $this->summaryDataService->getSummaryData($bookingDto),
'bookingCreateContext' => $bookingCreateContext,
'refreshRouteName' => 'app_booking_create_step_2_participant_refresh',
]);
}
@@ -190,7 +195,10 @@ class Step2ParticipantController extends AbstractController
$this->bookingSessionService->saveBookingDto($request, $bookingDto, $bookingDto->getMode());
$summaryData = $this->summaryDataService->getSummaryData($bookingDto);
$bookingCreateContext = $this->createContextFactory->create(
$bookingDto,
RoomPricingCalculator::PRICING_MODE_SELECTION
);
$response = $this->htmxOobResponse(
'booking/_participant_form.html.twig',
@@ -198,8 +206,7 @@ class Step2ParticipantController extends AbstractController
[
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'summaryData' => $summaryData,
'bookingCreateContext' => $bookingCreateContext,
'refreshRouteName' => $refreshRouteName,
]
);
@@ -13,11 +13,12 @@ use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Form\BookingCreateStep3Type;
use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Service\BookingCreateContextFactory;
use App\Service\BookingPriceCalculatorService;
use App\Service\BookingPriceMismatchDiagnosticsService;
use App\Service\BookingService;
use App\Service\BookingSessionService;
use App\Service\BookingSummaryDataService;
use App\Service\RoomPricingCalculator;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
@@ -37,7 +38,7 @@ class Step3Controller extends AbstractController
public function __construct(
private readonly BookingService $bookingService,
private readonly BookingSessionService $bookingSessionService,
private readonly BookingSummaryDataService $summaryDataService,
private readonly BookingCreateContextFactory $createContextFactory,
private readonly BookingPriceCalculatorService $priceCalculator,
private readonly BookingPriceMismatchDiagnosticsService $priceMismatchDiagnostics,
private readonly ApiClient $apiClient,
@@ -216,13 +217,14 @@ class Step3Controller extends AbstractController
*/
private function renderStepForm(BookingDto $bookingCreateDto, FormInterface $form): Response
{
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$context = $this->createContextFactory->create(
$bookingCreateDto,
RoomPricingCalculator::PRICING_MODE_ASSIGNMENT
);
return $this->render('booking/create/step_3.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'bookingCreateContext' => $context,
'form' => $form->createView(),
'summaryData' => $summaryData,
]);
}
@@ -14,10 +14,10 @@ use App\Form\BookingCreateStep4Type;
use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Entity\User;
use App\Service\BookingCreateContextFactory;
use App\Service\BookingPriceCalculatorService;
use App\Service\BookingService;
use App\Service\BookingSessionService;
use App\Service\BookingSummaryDataService;
use App\Service\Newsletter\MailjetNewsletterService;
use App\Service\Newsletter\NewsletterDoubleOptInService;
use Psr\Log\LoggerInterface;
@@ -40,7 +40,7 @@ class Step4Controller extends AbstractController
public function __construct(
private readonly BookingService $bookingService,
private readonly BookingSessionService $bookingSessionService,
private readonly BookingSummaryDataService $summaryDataService,
private readonly BookingCreateContextFactory $createContextFactory,
private readonly BookingPriceCalculatorService $priceCalculator,
private readonly ApiClient $apiClient,
private readonly CacheInterface $cache,
@@ -134,9 +134,9 @@ class Step4Controller extends AbstractController
}
// Success: Store booking data in flash for conversion tracking
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$bookingCreateContext = $this->createContextFactory->createWithParticipantPrices($bookingCreateDto);
$this->addFlash('booking_number', $bookingResponse->bookingNumber);
$this->addFlash('booking_total', $summaryData->payableAmount);
$this->addFlash('booking_total', $bookingCreateContext->summaryData->payableAmount);
$this->addFlash('booking_travel_name', $bookingCreateDto->travel->label);
$this->clearTravelDataCache($bookingCreateDto);
$this->bookingSessionService->clearBookingDto($request, BookingDto::MODE_CREATE);
@@ -182,14 +182,11 @@ class Step4Controller extends AbstractController
?string $newsletterTargetEmail,
): Response
{
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$bookingCreateContext = $this->createContextFactory->createWithParticipantPrices($bookingCreateDto);
return $this->render('booking/create/step_4.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'bookingCreateContext' => $bookingCreateContext,
'form' => $form->createView(),
'summaryData' => $summaryData,
'participantPrices' => $this->priceCalculator->calculateAllParticipantIndividualPrices($bookingCreateDto),
'newsletterOptInVisible' => $newsletterOptInVisible,
'newsletterTargetEmail' => $newsletterTargetEmail,
]);
+14 -125
View File
@@ -4,9 +4,6 @@ declare(strict_types=1);
namespace App\Controller\Booking\Edit;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\TimeoutException;
use App\BusProNet\Model\Notification;
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Entity\User;
@@ -16,13 +13,10 @@ use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Service\BookingEditDataLoaderService;
use App\Service\BookingEditDraftService;
use App\Service\BookingEditContextFactory;
use App\Service\BookingFingerprintService;
use App\Service\BookingEditSubmitGuardService;
use App\Service\BookingEditSubmitService;
use App\Service\BookingSessionService;
use App\Service\BookingSummaryDataService;
use App\Service\ParticipantCardDataService;
use App\Service\TravelDataService;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -36,7 +30,7 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
* - Card overview with lazy-loaded individual participant forms
* - Handles canceled participants (status 'S')
* - Applies mutability constraints via EditFieldStateProvider
* - Final submission calls ApiClient::updateBooking()
* - Final submission is delegated to BookingEditSubmitService
*/
class IndexController extends AbstractController
{
@@ -44,16 +38,12 @@ class IndexController extends AbstractController
use HxTrait;
public function __construct(
private readonly ApiClient $apiClient,
private readonly BookingEditDataLoaderService $dataLoader,
private readonly BookingEditDraftService $draftService,
private readonly TravelDataService $travelDataService,
private readonly BookingSessionService $bookingSessionService,
private readonly BookingEditSubmitGuardService $submitGuard,
private readonly BookingFingerprintService $fingerprintService,
private readonly BookingSummaryDataService $summaryDataService,
private readonly ParticipantCardDataService $participantCardService,
private readonly LoggerInterface $logger,
private readonly BookingEditContextFactory $editContextFactory,
private readonly BookingEditSubmitService $submitService,
) {
}
@@ -114,34 +104,26 @@ class IndexController extends AbstractController
return $this->redirectToRoute('app_bookings');
}
// Fetch mutable data for form constraints
$mutableData = $this->travelDataService->getMutabilityData($bookingData->dateId);
// Create validation form (same pattern as CreateStep2Controller)
$form = $this->createForm(BookingEditType::class, $bookingDto);
$form->handleRequest($request);
// Handle form submission (clicking "Buchung aktualisieren")
if ($form->isSubmitted() && $form->isValid()) {
return $this->handleFormSubmission($request, $bookingDto, $id, $user);
return $this->submitService->handleSubmission($request, $bookingDto, $id, $user);
}
// Always generate card data with validation state to show completeness
$cardsData = $this->participantCardService->getAllCardsDataWithValidation($bookingDto);
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingDto);
$bookingEditContext = $this->editContextFactory->createOverviewContext(
$bookingDto,
$bookingData,
$this->fingerprintService->isDirty($bookingDto),
$form->isSubmitted(),
$form->isSubmitted() && false === $form->isValid(),
);
$templateData = [
'form' => $form->createView(),
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $mutableData,
'cardsData' => $cardsData,
'summaryData' => $summaryData,
'isDirty' => $this->fingerprintService->isDirty($bookingDto),
'isSubmitted' => $form->isSubmitted(),
'hasValidationErrors' => $form->isSubmitted() && false === $form->isValid(),
'bookingEditContext' => $bookingEditContext,
];
return $this->render('booking/edit/index.html.twig', $templateData);
@@ -208,97 +190,4 @@ class IndexController extends AbstractController
return $this->redirectToRoute('app_bookings');
}
/**
* Handles form submission for booking update.
*/
private function handleFormSubmission(Request $request, BookingDto $bookingDto, int $id, User $user): Response
{
$email = $user->getEmail();
$this->logger->info('Initiated booking update', [
'email' => $email,
'booking_id' => $id,
]);
$this->dataLoader->invalidateBookingCache($id, $user);
$freshBookingData = $this->dataLoader->fetchBookingData($id, $user);
if (null === $freshBookingData || $freshBookingData instanceof Notification) {
$this->addFlash('error', 'Buchungsdaten konnten vor dem Speichern nicht neu geladen werden');
$this->logger->warning('Failed to refresh booking before update submission', [
'email' => $email,
'booking_id' => $id,
'has_notification' => $freshBookingData instanceof Notification,
]);
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
$bookingDto->booking = $freshBookingData;
$mutableData = $this->travelDataService->getMutabilityData(
$freshBookingData->dateId,
forceRefresh: true
);
if (null !== $mutableData) {
$this->travelDataService->patchMutability($bookingDto->travel, $mutableData);
}
$immutableChangesReverted = $this->submitGuard->reconcileImmutableCategories($bookingDto, $freshBookingData);
if (true === $immutableChangesReverted) {
$this->bookingSessionService->saveBookingDto($request, $bookingDto, BookingDto::MODE_EDIT);
$this->addFlash('info', 'Einige Änderungen wurden verworfen, da sie aktuell nicht mehr änderbar sind.');
}
try {
$response = $this->apiClient->updateBooking($bookingDto, true);
if ($response instanceof Notification) {
if (true === $response->isError()) {
$this->addFlash('error', $response->message);
} else {
$this->addFlash('info', $response->message);
}
$this->logger->error('Booking update not successful', [
'email' => $email,
'booking_id' => $id,
'message' => $response->message,
]);
} elseif (true === $response->success) {
// Invalidate cache and clear session on success
$this->dataLoader->invalidateBookingCache($id, $user);
$this->bookingSessionService->clearBookingDto($request, BookingDto::MODE_EDIT);
// Delete draft on successful submission
$this->draftService->deleteDraft($user, $id);
$this->addFlash('success', 'Buchung erfolgreich aktualisiert');
$this->logger->info('Booking update successful', [
'email' => $email,
'booking_id' => $id,
]);
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
} else {
// BookingUpdate received but success is false
$this->addFlash('error', $response->status ?? 'Buchung konnte nicht aktualisiert werden');
$this->logger->warning('Booking update returned unsuccessful status', [
'email' => $email,
'booking_id' => $id,
'status' => $response->status,
'valid' => $response->valid,
]);
}
} catch (TimeoutException $e) {
$this->addFlash('error', 'Die Anfrage hat zu lange gedauert. Bitte versuchen Sie es erneut.');
$this->logger->error('Booking update timeout', [
'email' => $email,
'booking_id' => $id,
'exception' => $e->getMessage(),
]);
} catch (ApiClientException) {
$this->addFlash('error', 'Es ist ein Fehler in der Kommunikation mit dem Buchungssystem aufgetreten');
}
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
}
@@ -11,8 +11,8 @@ use App\Form\BookingParticipantType;
use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Service\BookingEditDataLoaderService;
use App\Service\BookingEditContextFactory;
use App\Service\BookingEditDraftService;
use App\Service\BookingEditParticipantContextFactory;
use App\Service\BookingSessionService;
use App\Service\ParticipantFormSupportService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -31,7 +31,7 @@ class ParticipantController extends AbstractController
public function __construct(
private readonly BookingEditDataLoaderService $dataLoader,
private readonly BookingEditDraftService $draftService,
private readonly BookingEditParticipantContextFactory $participantContextFactory,
private readonly BookingEditContextFactory $editContextFactory,
private readonly BookingSessionService $bookingSessionService,
private readonly ParticipantFormSupportService $participantFormSupportService,
) {
@@ -74,7 +74,7 @@ class ParticipantController extends AbstractController
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
$this->participantContextFactory->prepareBookingDto($bookingDto);
$this->editContextFactory->prepareBookingDto($bookingDto);
$wrapper = $this->participantFormSupportService->createParticipantEditDto($bookingDto, $index);
@@ -96,15 +96,12 @@ class ParticipantController extends AbstractController
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
$context = $this->participantContextFactory->create($bookingDto, $bookingData);
$context = $this->editContextFactory->createParticipantContext($bookingDto, $bookingData);
return $this->render('booking/edit/participant.html', [
return $this->render('booking/edit/participant.html.twig', [
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $context->bookingDto,
'bookingData' => $context->bookingData,
'mutableData' => $context->mutableData,
'summaryData' => $context->summaryData,
'bookingEditContext' => $context,
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
@@ -134,7 +131,7 @@ class ParticipantController extends AbstractController
}
$this->participantFormSupportService->ensureParticipantExists($bookingDto, $index);
$this->participantContextFactory->prepareBookingDto($bookingDto);
$this->editContextFactory->prepareBookingDto($bookingDto);
$bookingData = $this->dataLoader->fetchBookingData($id, $user);
@@ -149,20 +146,18 @@ class ParticipantController extends AbstractController
$form->handleRequest($request);
$notifications = $this->participantFormSupportService->collectAndClearNotifications($bookingDto);
$context = null !== $bookingData && !($bookingData instanceof Notification)
? $this->participantContextFactory->create($bookingDto, $bookingData)
: null;
$context = $this->editContextFactory->createParticipantContext(
$bookingDto,
$bookingData instanceof Booking ? $bookingData : null
);
$response = $this->htmxOobResponse(
'booking/_participant_form.html.twig',
['participant_form', 'booking_summary'],
[
'form' => $form,
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $context?->mutableData,
'summaryData' => $context?->summaryData,
'bookingEditContext' => $context,
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
@@ -48,23 +48,6 @@ trait BookingCreateTrait
return $this->redirectToRoute($route);
}
/**
* Prepares template variables for the booking summary sidebar.
*
* @return array<string, mixed> Array containing all variables needed for the summary partial
*/
private function getSummaryVariables(BookingDto $bookingCreateDto): array
{
$summary = $this->summaryDataService->getSummaryData($bookingCreateDto);
return [
'participantsCount' => $summary->participantCount,
'pricingData' => $summary->pricingData,
'assignmentCounts' => $summary->assignmentCounts,
'groupedSelectedRooms' => $summary->groupedSelectedRooms,
];
}
/**
* Handles API errors by logging and adding a flash message.
*/