wip: refactoring
This commit is contained in:
@@ -4,7 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Form\ChoiceLoader;
|
||||
|
||||
use App\Form\DataAdapters\ParticipantDataAdapter;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use App\Form\Model\RoomSelectionDto;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
@@ -21,9 +22,14 @@ class ParticipantRoomChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
private ?ChoiceListInterface $choiceList = null;
|
||||
|
||||
/**
|
||||
* @param ParticipantDto[] $allParticipants all participant DTOs from the root form
|
||||
* @param RoomSelectionDto[] $selectedRooms the rooms selected in the previous step
|
||||
* @param int $participantIndex the index of the current participant
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly ChoiceListFactoryInterface $factory,
|
||||
private readonly ParticipantDataAdapter $adapter,
|
||||
private readonly array $allParticipants,
|
||||
private readonly array $selectedRooms,
|
||||
private readonly int $participantIndex,
|
||||
) {
|
||||
@@ -66,7 +72,7 @@ class ParticipantRoomChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
$roomOccupancy = $this->calculateRoomOccupancy();
|
||||
$participantRoomChoices = [];
|
||||
$assignedRoomId = $this->getParticipantAssignedRoomId();
|
||||
$assignedRoomId = $this->allParticipants[$this->participantIndex]->assignedRoomId ?? null;
|
||||
|
||||
foreach ($this->selectedRooms as $roomSelection) {
|
||||
$currentOccupancy = $roomOccupancy[$roomSelection->roomId] ?? 0;
|
||||
@@ -77,7 +83,8 @@ class ParticipantRoomChoiceLoader implements ChoiceLoaderInterface
|
||||
--$adjustedOccupancy;
|
||||
}
|
||||
|
||||
$remainingCapacity = $roomSelection->minPax - $adjustedOccupancy;
|
||||
$totalAvailableRooms = $roomSelection->quantity;
|
||||
$remainingCapacity = $totalAvailableRooms - $adjustedOccupancy;
|
||||
|
||||
// Include room if it has capacity OR if it's the participant's current assignment
|
||||
if ($remainingCapacity > 0 || $assignedRoomId === $roomSelection->roomId) {
|
||||
@@ -89,7 +96,7 @@ class ParticipantRoomChoiceLoader implements ChoiceLoaderInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates room occupancy based on participant assignments.
|
||||
* Calculates room occupancy based on all participant assignments.
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
@@ -97,24 +104,12 @@ class ParticipantRoomChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
$roomOccupancy = [];
|
||||
|
||||
foreach ($this->adapter->getParticipants() as $participant) {
|
||||
$assignedRoomId = $this->adapter->getAssignedRoomId($participant);
|
||||
if (null !== $assignedRoomId) {
|
||||
$roomOccupancy[$assignedRoomId] = ($roomOccupancy[$assignedRoomId] ?? 0) + 1;
|
||||
foreach ($this->allParticipants as $participant) {
|
||||
if (null !== $participant->assignedRoomId) {
|
||||
$roomOccupancy[$participant->assignedRoomId] = ($roomOccupancy[$participant->assignedRoomId] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $roomOccupancy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the assigned room ID for the current participant.
|
||||
*/
|
||||
private function getParticipantAssignedRoomId(): ?int
|
||||
{
|
||||
$participants = $this->adapter->getParticipants();
|
||||
$participant = $participants[$this->participantIndex] ?? null;
|
||||
|
||||
return null !== $participant ? $this->adapter->getAssignedRoomId($participant) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user