feat: refactoring and cleanup

This commit is contained in:
Björn Fromme committed 2025-12-06 15:02:13 +01:00
1 parent f4691ec67e
commit 696800aafd
41 files changed
+2591 -2233

No files matched your search

@@ -7,6 +7,7 @@ namespace App\Controller\Booking\Create;
use App\Controller\Booking\Traits\BookingCreateTrait;
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Form\BookingCreateStep1Type;
use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Service\BookingService;
use App\Service\BookingSummaryDataService;
@@ -66,15 +67,15 @@ class Step1Controller extends AbstractController
$form->handleRequest($request);
if (true === $form->isSubmitted() && true === $form->isValid()) {
if ($this->bookingService->hasRoomSelectionChanged($oldRoomSelectionSnapshot, $bookingCreateDto)) {
$this->bookingService->resetParticipantAssignments($bookingCreateDto);
if ($bookingCreateDto->hasRoomSelectionChanged($oldRoomSelectionSnapshot)) {
$bookingCreateDto->resetParticipantAssignments();
}
// Check if room selection forces inquiry mode
$this->bookingService->updateBookingStatusFromRoomSelection($bookingCreateDto);
$bookingCreateDto->currentStep = 2;
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
$this->bookingService->saveBookingDto($request, $bookingCreateDto, BookingDto::MODE_CREATE);
// Clear baseline snapshot when moving to step 2
$this->bookingService->clearBaselineSnapshot($request);
@@ -83,21 +84,16 @@ class Step1Controller extends AbstractController
}
// Get complete summary data (pricing, rooms, CMS data)
$summary = $this->summaryDataService->getSummaryData($bookingCreateDto);
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
$groupedRooms = $this->bookingService->groupRoomsBySelectionType($availableRooms);
$groupedSelectedRooms = $this->bookingService->groupRoomSelectionsByType($summary['selectedRooms'], $availableRooms);
return $this->render('booking/create/step_1.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'roomSummary' => $summary['selectedRooms'],
'participantCount' => $summary['participantCount'],
'pricingData' => $summary['pricingData'],
'cmsData' => $summary['cmsData'],
'summaryData' => $summaryData,
'form' => $form->createView(),
'groupedRooms' => $groupedRooms,
'groupedSelectedRooms' => $groupedSelectedRooms,
]);
}
@@ -120,13 +116,12 @@ class Step1Controller extends AbstractController
]);
$form->handleRequest($request);
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
$this->bookingService->saveBookingDto($request, $bookingCreateDto, BookingDto::MODE_CREATE);
// Get complete summary data (pricing, rooms, CMS data)
$summary = $this->summaryDataService->getSummaryData($bookingCreateDto);
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
$groupedRooms = $this->bookingService->groupRoomsBySelectionType($availableRooms);
$groupedSelectedRooms = $this->bookingService->groupRoomSelectionsByType($summary['selectedRooms'], $availableRooms);
// The DTO is now updated with the latest selection.
// We can now render the blocks with the fresh data.
@@ -136,11 +131,8 @@ class Step1Controller extends AbstractController
[
'form' => $form->createView(),
'bookingCreateDto' => $bookingCreateDto,
'participantCount' => $summary['participantCount'],
'pricingData' => $summary['pricingData'],
'cmsData' => $summary['cmsData'],
'summaryData' => $summaryData,
'groupedRooms' => $groupedRooms,
'groupedSelectedRooms' => $groupedSelectedRooms,
]
);
}
@@ -9,7 +9,6 @@ use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Controller\Booking\Traits\ParticipantCardFlowTrait;
use App\Form\BookingCreateStep2Type;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ParticipantFieldOptionsProvider;
use App\Htmx\HxTrait;
use App\Service\BookingService;
@@ -66,13 +65,17 @@ class Step2Controller extends AbstractController
}
// Enrich with fresh availability data
$this->enrichWithFreshAvailabilities($bookingCreateDto);
$this->travelDataService->enrichWithFreshAvailabilities($bookingCreateDto->travel);
// Ensure correct number of participants
$this->ensureCorrectNumberOfParticipants($bookingCreateDto);
// Ensure correct number of participants with prepopulation callback
$this->bookingService->ensureCorrectNumberOfParticipants(
$bookingCreateDto,
$this->getUser(),
fn ($user, $participant) => $this->prepopulationService->prepopulateApplicantFromUser($user, $participant)
);
// Auto-assign rooms if needed
$this->autoAssignRoomsIfNeeded($bookingCreateDto);
$this->roomAssignmentService->assignRoomsIfNeeded($bookingCreateDto);
// Preselect mandatory services
$this->bookingService->preselectMandatoryServices($bookingCreateDto);
@@ -107,8 +110,6 @@ class Step2Controller extends AbstractController
'bookingDto' => $bookingCreateDto,
'cardsData' => $cardsData,
'summaryData' => $summaryData,
'pricingData' => $summaryData['pricingData'],
'cmsData' => $summaryData['cmsData'],
];
// HTMX request: render blocks only
@@ -143,7 +144,7 @@ class Step2Controller extends AbstractController
}
// Enrich with fresh availability data
$this->enrichWithFreshAvailabilities($bookingDto);
$this->travelDataService->enrichWithFreshAvailabilities($bookingDto->travel);
// Create form with booking_context option
$form = $this->createParticipantForm($bookingDto, $index);
@@ -158,11 +159,7 @@ class Step2Controller extends AbstractController
$this->bookingService->saveBookingDto($request, $bookingDto, BookingDto::MODE_CREATE);
// Add notifications as flash messages (redirects destroy HTMX-triggered toasts)
if (false === empty($notifications)) {
foreach ($notifications as $notification) {
$this->addFlash($notification['type'], $notification['message']);
}
}
$this->addNotificationsAsFlashMessages($notifications);
// HTMX redirect to cards view
return $this->hxRedirect($request, $this->generateUrl('app_booking_create_step_2'));
@@ -176,8 +173,6 @@ class Step2Controller extends AbstractController
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'summaryData' => $summaryData,
'pricingData' => $summaryData['pricingData'],
'cmsData' => $summaryData['cmsData'],
'refreshRouteName' => 'app_booking_create_step_2_participant_refresh',
'submitRouteName' => 'app_booking_create_step_2_participant',
];
@@ -224,7 +219,7 @@ class Step2Controller extends AbstractController
}
// Enrich with fresh availability data
$this->enrichWithFreshAvailabilities($bookingDto);
$this->travelDataService->enrichWithFreshAvailabilities($bookingDto->travel);
// Use trait method for refresh handling
return $this->handleParticipantRefresh(
@@ -235,76 +230,4 @@ class Step2Controller extends AbstractController
'app_booking_create_step_2_participant'
);
}
/**
* Ensures the booking DTO has the correct number of participant objects.
*/
private function ensureCorrectNumberOfParticipants(BookingDto $bookingCreateDto): void
{
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
$participants = $bookingCreateDto->participants;
$bookingCreateDto->participants = [];
for ($i = 0; $i < $participantsCount; ++$i) {
$participant = $participants[$i] ?? new ParticipantDto();
$participant->index = $i;
// Prepopulate applicant from authenticated user (index 0 only)
if (0 === $i && $this->getUser() && $this->shouldPrepopulate($participant)) {
$participant = $this->prepopulationService->prepopulateApplicantFromUser(
$this->getUser(),
$participant
);
}
$bookingCreateDto->participants[$i] = $participant;
}
}
/**
* Determines if a participant should be prepopulated.
*
* Only prepopulates if the participant is "fresh" (no name set yet).
*/
private function shouldPrepopulate(ParticipantDto $participant): bool
{
return null === $participant->firstName || '' === $participant->firstName;
}
/**
* Enriches travel data with cached availability information from BusProNet API.
*/
private function enrichWithFreshAvailabilities(BookingDto $bookingCreateDto): void
{
$dateId = $bookingCreateDto->travel->id;
$availabilities = $this->travelDataService->getAvailabilityData($dateId, true);
if (null !== $availabilities) {
$this->travelDataService->patchAvailabilities($bookingCreateDto->travel, $availabilities);
}
}
/**
* Automatically assigns participants to rooms if they don't have room assignments yet.
*
* Note: Auto-assignment only occurs when exactly one room type is selected.
* With multiple room types, users must manually select rooms to avoid UX issues
* with having to unselect preassigned rooms in individual participant forms.
*/
private function autoAssignRoomsIfNeeded(BookingDto $bookingCreateDto): void
{
// Check if any participants need room assignment
$needsAssignment = false;
foreach ($bookingCreateDto->participants as $participant) {
if (null === $participant->assignedRoomId) {
$needsAssignment = true;
break;
}
}
if ($needsAssignment) {
$this->roomAssignmentService->assignParticipantsToRooms($bookingCreateDto);
}
}
}
@@ -94,7 +94,7 @@ class Step3Controller extends AbstractController
// Auto-switch to inquiry mode
$bookingCreateDto->bookingStatus = 'A';
$bookingCreateDto->currentStep = 4;
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
$this->bookingService->saveBookingDto($request, $bookingCreateDto, BookingDto::MODE_CREATE);
$message = 'Buchung konnte nicht validiert werden.';
if (null !== $inquiryResponse->message && '' !== trim($inquiryResponse->message)) {
@@ -147,7 +147,7 @@ class Step3Controller extends AbstractController
// Validation successful - proceed to confirmation step
$bookingCreateDto->currentStep = 4;
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
$this->bookingService->saveBookingDto($request, $bookingCreateDto, BookingDto::MODE_CREATE);
if ($inquiryResponse->message) {
$this->addFlash('info', $inquiryResponse->message);
@@ -199,7 +199,7 @@ class Step3Controller extends AbstractController
]);
$form->handleRequest($request);
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
$this->bookingService->saveBookingDto($request, $bookingCreateDto, BookingDto::MODE_CREATE);
return $this->renderStepForm($bookingCreateDto, $form);
}
@@ -211,17 +211,11 @@ class Step3Controller extends AbstractController
{
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
$groupedSelectedRooms = $this->bookingService->groupRoomSelectionsByType($summaryData['selectedRooms'], $availableRooms);
return $this->render('booking/create/step_3.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'form' => $form->createView(),
'participantCount' => $summaryData['participantCount'],
'pricingData' => $summaryData['pricingData'],
'cmsData' => $summaryData['cmsData'],
'assignmentCounts' => $summaryData['assignmentCounts'],
'groupedSelectedRooms' => $groupedSelectedRooms,
'summaryData' => $summaryData,
]);
}
@@ -102,7 +102,7 @@ class Step4Controller extends AbstractController
// Success: Store booking number in flash and clear session
$this->addFlash('booking_number', $bookingResponse->transactionNumber);
$this->clearTravelDataCache($bookingCreateDto);
$this->bookingService->clearBookingCreateDto($request);
$this->bookingService->clearBookingDto($request, BookingDto::MODE_CREATE);
return $this->hxRedirect($request, $this->generateUrl('app_booking_create_success'));
} catch (TimeoutException $e) {
@@ -140,17 +140,11 @@ class Step4Controller extends AbstractController
{
// Get complete summary data (pricing, rooms, CMS data)
$summaryData = $this->summaryDataService->getSummaryData($bookingCreateDto);
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
$groupedSelectedRooms = $this->bookingService->groupRoomSelectionsByType($summaryData['selectedRooms'], $availableRooms);
return $this->render('booking/create/step_4.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'form' => $form->createView(),
'participantCount' => $summaryData['participantCount'],
'pricingData' => $summaryData['pricingData'],
'cmsData' => $summaryData['cmsData'],
'assignmentCounts' => $summaryData['assignmentCounts'],
'groupedSelectedRooms' => $groupedSelectedRooms,
'summaryData' => $summaryData,
'participantPrices' => $this->priceCalculator->calculateAllParticipantIndividualPrices($bookingCreateDto),
]);
}