feat: replace overly complex and unnecessary form choice loader for rooms

This commit is contained in:
Björn Fromme
2026-03-16 12:00:55 +01:00
parent af2620c8b3
commit 6b5a51f652
5 changed files with 41 additions and 207 deletions
@@ -7,12 +7,12 @@ namespace App\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Insurance;
use App\BusProNet\Model\Pickup;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Service;
use App\BusProNet\Utility\DirectionMapper;
use App\Form\Model\BookingDto;
use App\Form\Model\RoomSelectionDto;
use App\Form\Service\Abstract\AbstractFieldOptionsProvider;
use App\Form\Service\Factory\ParticipantRoomChoiceLoaderFactory;
use App\Service\BookingPriceCalculatorService;
use App\Service\InsuranceService;
use App\Service\ServiceAvailabilityCalculator;
@@ -36,21 +36,7 @@ use App\Service\ServiceAvailabilityCalculator;
*/
class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
{
/**
* Initializes the provider with required dependencies.
*
* The provider automatically registers all field option providers during
* construction to ensure they're available for form building. This approach
* keeps all field configuration logic centralized and makes it easy to add
* new dynamic fields.
*
* @param ParticipantRoomChoiceLoaderFactory $roomChoiceLoaderFactory Factory for creating room choice loaders
* @param ServiceAvailabilityCalculator $serviceAvailabilityCalculator Service for calculating dynamic availability
* @param InsuranceService $insuranceService Service for insurance operations
* @param BookingPriceCalculatorService $priceCalculatorService Service for calculating participant prices
*/
public function __construct(
private readonly ParticipantRoomChoiceLoaderFactory $roomChoiceLoaderFactory,
private readonly ServiceAvailabilityCalculator $serviceAvailabilityCalculator,
private readonly InsuranceService $insuranceService,
private readonly BookingPriceCalculatorService $priceCalculatorService,
@@ -83,8 +69,7 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
{
// Room assignment field provider (available for both create and edit workflows)
$this->fieldOptionProviders['assignedRoomId'] = function (BookingDto $bookingDto, int $participantIndex, array $options = []) {
$choiceLoader = null;
$singleChoice = false;
$choices = [];
if (BookingDto::MODE_CREATE === $bookingDto->getMode()) {
// Create context: use selected rooms from step 1, filtered by participant age
@@ -93,26 +78,19 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
$bookingDto,
$participantIndex
);
$choiceLoader = $this->roomChoiceLoaderFactory->createForCreate($filteredRooms);
$singleChoice = 1 === count($filteredRooms);
$choices = $this->buildRoomChoicesFromSelections($filteredRooms);
} elseif (BookingDto::MODE_EDIT === $bookingDto->getMode()) {
// Edit context: use already-booked rooms from booking
$choiceLoader = $this->roomChoiceLoaderFactory->createForEdit(
$bookingDto->booking->rooms
);
$singleChoice = 1 === count($bookingDto->booking->rooms);
$choices = $this->buildRoomChoicesFromBookedRooms($bookingDto->booking->rooms);
}
$singleChoice = 1 === count($choices);
return [
'label' => 'Zimmer',
// No placeholder when only one room available - the only option is pre-selected
'placeholder' => $singleChoice ? false : 'Nicht zugeordnet',
// Use factory to create context-aware choice loader that:
// - Shows only available rooms for this participant
// - Excludes rooms already assigned to other participants
// - Respects room capacity and booking constraints
// - Filters Baby rooms for participants over 2 years old
'choice_loader' => $choiceLoader,
'choices' => $choices,
];
};
@@ -548,6 +526,40 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
});
}
/**
* Builds room choices array from RoomSelectionDto objects.
*
* @param RoomSelectionDto[] $roomSelections Room selections from step 1
*
* @return array<string, int> Choices array with label => id
*/
private function buildRoomChoicesFromSelections(array $roomSelections): array
{
$choices = [];
foreach ($roomSelections as $roomSelection) {
$choices[$roomSelection->roomLabel] = $roomSelection->roomId;
}
return $choices;
}
/**
* Builds room choices array from booked Room objects.
*
* @param Room[] $bookedRooms Rooms from existing booking
*
* @return array<string, int> Choices array with label => id
*/
private function buildRoomChoicesFromBookedRooms(array $bookedRooms): array
{
$choices = [];
foreach ($bookedRooms as $room) {
$choices[$room->label] = $room->id;
}
return $choices;
}
/**
* Filters rental services by selected skipass duration.
*