feat: refactoring and cleanup
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user