wip: form refactoring

This commit is contained in:
Björn Fromme
2025-07-25 09:28:13 +02:00
parent 2b122a7cc4
commit ae2ed306b9
13 changed files with 158 additions and 100 deletions
+57 -16
View File
@@ -6,7 +6,6 @@ use App\BusProNet\Model\Room;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingCreateDto;
use App\Form\Model\RoomSelectionDto;
use App\Service\TravelDataService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -14,8 +13,7 @@ class BookingService
{
public function __construct(
private readonly TravelDataService $travelDataService,
) {
}
) {}
public function getOrCreateBookingCreateDto(Request $request): BookingCreateDto
{
@@ -43,7 +41,7 @@ class BookingService
$availableRooms = $travelData->getAvailableRooms();
$roomSelections = array_map(
fn (Room $room) => $this->createRoomSelection($room, $roomsIdsAndQuantities),
fn(Room $room) => $this->createRoomSelection($room, $roomsIdsAndQuantities),
$availableRooms
);
@@ -103,7 +101,6 @@ class BookingService
return $participantsCount;
}
/**
* Calculates the number of participants assigned to each room ID.
*
@@ -124,7 +121,6 @@ class BookingService
/**
* Returns a summary of selected rooms and the resulting participant count for a booking.
*
* @param BookingCreateDto $bookingCreateDto
* @return array{selectedRooms: array, participantCount: int}
*/
public function getRoomSummaryAndParticipantCount(BookingCreateDto $bookingCreateDto): array
@@ -142,19 +138,20 @@ class BookingService
* Groups available rooms by selection type ('by_pax' or 'by_room').
*
* @param array<int, Room> $rooms Rooms indexed by room ID
*
* @return array{by_pax: array<int, Room>, by_room: array<int, Room>}
*/
public function groupRoomsBySelectionType(array $rooms): array
{
$groups = [
'by_pax' => [],
'by_room' => [],
Room::SELECTION_TYPE_BY_PAX => [],
Room::SELECTION_TYPE_BY_ROOM => [],
];
foreach ($rooms as $room) {
if (stripos($room->label, 'bett') !== false) {
$groups['by_pax'][$room->id] = $room;
if (false !== stripos($room->label, 'bett')) {
$groups[Room::SELECTION_TYPE_BY_PAX][$room->id] = $room;
} else {
$groups['by_room'][$room->id] = $room;
$groups[Room::SELECTION_TYPE_BY_ROOM][$room->id] = $room;
}
}
@@ -164,15 +161,16 @@ class BookingService
/**
* Groups roomSelections by selection type ('by_pax' or 'by_room'), using Room::getSelectionType().
*
* @param array $roomSelections Array of selected RoomSelectionDto
* @param array<int, Room> $roomsById Rooms indexed by room ID
* @param array $roomSelections Array of selected RoomSelectionDto
* @param array<int, Room> $roomsById Rooms indexed by room ID
*
* @return array{by_pax: array, by_room: array}
*/
public function groupRoomSelectionsByType(array $roomSelections, array $roomsById): array
{
$groups = [
'by_pax' => [],
'by_room' => [],
Room::SELECTION_TYPE_BY_PAX => [],
Room::SELECTION_TYPE_BY_ROOM => [],
];
foreach ($roomSelections as $roomSelection) {
$room = $roomsById[$roomSelection->roomId] ?? null;
@@ -181,7 +179,50 @@ class BookingService
$groups[$type][] = $roomSelection;
}
}
return $groups;
}
/**
* Determines if the room selection has changed between two DTOs.
*/
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);
return $old !== $new;
}
/**
* Resets all participant room assignments in the DTO.
*/
public function resetParticipantAssignments(BookingCreateDto $dto): void
{
foreach ($dto->participants as $participant) {
$participant->assignedRoomId = null;
}
}
/**
* Creates a snapshot of the current room selection state.
*
* @return array<int, array{int, int}> Array of [roomId, quantity] pairs
*/
public function createRoomSelectionSnapshot(BookingCreateDto $dto): array
{
return array_map(
fn($roomSelection) => [$roomSelection->roomId, $roomSelection->quantity],
$dto->roomSelections
);
}
/**
* Checks if room selection has changed compared to a previous snapshot.
*/
public function hasRoomSelectionChanged(array $oldSnapshot, BookingCreateDto $newDto): bool
{
$newSnapshot = $this->createRoomSelectionSnapshot($newDto);
return $oldSnapshot !== $newSnapshot;
}
}