wip: major refactoring
This commit is contained in:
@@ -11,14 +11,51 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class BookingService
|
||||
{
|
||||
public const BOOKING_CREATE_KEY = 'booking_create';
|
||||
public const BOOKING_CREATE_BASELINE_KEY = 'booking_create_baseline_snapshot';
|
||||
|
||||
public function __construct(
|
||||
private readonly TravelDataService $travelDataService,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets or creates the baseline room selection snapshot for change detection.
|
||||
*
|
||||
* The baseline snapshot captures the initial room selection state when step 1
|
||||
* is first loaded, before any HTMX modifications. This ensures accurate change
|
||||
* detection for room assignment resets.
|
||||
*/
|
||||
public function getOrCreateBaselineSnapshot(Request $request, BookingCreateDto $bookingCreateDto): array
|
||||
{
|
||||
$baselineKey = self::BOOKING_CREATE_BASELINE_KEY;
|
||||
|
||||
if (!$request->getSession()->has($baselineKey) || $request->query->has('reset_baseline')) {
|
||||
$baseline = $this->createRoomSelectionSnapshot($bookingCreateDto);
|
||||
$request->getSession()->set($baselineKey, $baseline);
|
||||
|
||||
return $baseline;
|
||||
}
|
||||
|
||||
return $request->getSession()->get($baselineKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the baseline snapshot from the session.
|
||||
*
|
||||
* Should be called when moving to the next step or when the baseline
|
||||
* needs to be refreshed.
|
||||
*/
|
||||
public function clearBaselineSnapshot(Request $request): void
|
||||
{
|
||||
$request->getSession()->remove('booking_create_baseline_snapshot');
|
||||
}
|
||||
|
||||
public function getOrCreateBookingCreateDto(Request $request): BookingCreateDto
|
||||
{
|
||||
$bookingCreateKey = self::BOOKING_CREATE_KEY;
|
||||
$bookingUuid = $request->query->get('uid');
|
||||
$bookingCreateDto = $request->getSession()->get('booking_create');
|
||||
$bookingCreateDto = $request->getSession()->get($bookingCreateKey);
|
||||
|
||||
// No UID parameter - return existing DTO from session if available
|
||||
if (null === $bookingUuid && null !== $bookingCreateDto) {
|
||||
@@ -41,7 +78,7 @@ class BookingService
|
||||
$availableRooms = $travelData->getAvailableRooms();
|
||||
|
||||
$roomSelections = array_map(
|
||||
fn(Room $room) => $this->createRoomSelection($room, $roomsIdsAndQuantities),
|
||||
fn (Room $room) => $this->createRoomSelection($room, $roomsIdsAndQuantities),
|
||||
$availableRooms
|
||||
);
|
||||
|
||||
@@ -55,7 +92,7 @@ class BookingService
|
||||
|
||||
public function saveBookingCreateDto(Request $request, BookingCreateDto $bookingCreateDto): void
|
||||
{
|
||||
$request->getSession()->set('booking_create', $bookingCreateDto);
|
||||
$request->getSession()->set(self::BOOKING_CREATE_KEY, $bookingCreateDto);
|
||||
}
|
||||
|
||||
private function createRoomSelection(Room $room, array $roomsIdsAndQuantities): RoomSelectionDto
|
||||
@@ -188,8 +225,8 @@ class BookingService
|
||||
*/
|
||||
public function shouldResetAssignments(BookingCreateDto $oldDto, BookingCreateDto $newDto): bool
|
||||
{
|
||||
$old = array_map(fn($roomSelectionDto) => [$roomSelectionDto->roomId, $roomSelectionDto->quantity], $oldDto->roomSelections);
|
||||
$new = array_map(fn($roomSelectionDto) => [$roomSelectionDto->roomId, $roomSelectionDto->quantity], $newDto->roomSelections);
|
||||
$old = array_map(fn ($roomSelectionDto) => [$roomSelectionDto->roomId, $roomSelectionDto->quantity], $oldDto->roomSelections);
|
||||
$new = array_map(fn ($roomSelectionDto) => [$roomSelectionDto->roomId, $roomSelectionDto->quantity], $newDto->roomSelections);
|
||||
|
||||
return $old !== $new;
|
||||
}
|
||||
@@ -212,7 +249,7 @@ class BookingService
|
||||
public function createRoomSelectionSnapshot(BookingCreateDto $dto): array
|
||||
{
|
||||
return array_map(
|
||||
fn($roomSelection) => [$roomSelection->roomId, $roomSelection->quantity],
|
||||
fn ($roomSelection) => [(int) $roomSelection->roomId, (int) $roomSelection->quantity],
|
||||
$dto->roomSelections
|
||||
);
|
||||
}
|
||||
@@ -223,6 +260,7 @@ class BookingService
|
||||
public function hasRoomSelectionChanged(array $oldSnapshot, BookingCreateDto $newDto): bool
|
||||
{
|
||||
$newSnapshot = $this->createRoomSelectionSnapshot($newDto);
|
||||
|
||||
return $oldSnapshot !== $newSnapshot;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user