feat: move prepopulation guards into participant service

This commit is contained in:
Björn Fromme committed 2026-04-11 18:28:32 +02:00
1 parent dbc0b4acee
commit 63e8a068b3
9 files changed
+114 -289

No files matched your search

+2 -20
View File
@@ -7,7 +7,6 @@ namespace App\Service;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Keeps participant-count shaping separate from booking orchestration.
@@ -17,13 +16,9 @@ class BookingParticipantCountService
/**
* Ensures the booking DTO has the expected number of participant objects.
*
* @param callable|null $prepopulateCallback fn(UserInterface, ParticipantDto): ParticipantDto
*/
public function ensureCorrectNumberOfParticipants(
BookingDto $bookingDto,
?UserInterface $user = null,
?callable $prepopulateCallback = null,
): void {
public function ensureCorrectNumberOfParticipants(BookingDto $bookingDto): void
{
$participantsCount = $this->calculateParticipantsCount($bookingDto->roomSelections, $bookingDto->travel);
$existingParticipants = $bookingDto->participants;
@@ -33,11 +28,6 @@ class BookingParticipantCountService
$participant = $existingParticipants[$i] ?? new ParticipantDto();
$participant->index = $i;
// Prepopulate applicant from authenticated user (index 0 only)
if (0 === $i && null !== $user && null !== $prepopulateCallback && $this->shouldPrepopulate($participant)) {
$participant = $prepopulateCallback($user, $participant);
}
$bookingDto->participants[$i] = $participant;
}
}
@@ -63,12 +53,4 @@ class BookingParticipantCountService
return $participantsCount;
}
/**
* Only prepopulate if the participant is fresh.
*/
private function shouldPrepopulate(ParticipantDto $participant): bool
{
return null === $participant->firstName || '' === $participant->firstName;
}
}
@@ -5,10 +5,13 @@ declare(strict_types=1);
namespace App\Service;
use App\BusProNet\ApiClient;
use App\BusProNet\Model\Address;
use App\BusProNet\Model\Notification;
use App\Entity\User;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Security\Crypt;
use Carbon\CarbonImmutable;
use Psr\Log\LoggerInterface;
/**
@@ -20,6 +23,8 @@ use Psr\Log\LoggerInterface;
*/
class ParticipantPrepopulationService
{
public const TOKEN = '#KUN#';
public function __construct(
private readonly ApiClient $apiClient,
private readonly Crypt $crypt,
@@ -94,4 +99,50 @@ class ParticipantPrepopulationService
return $applicant;
}
}
/**
* Determines whether the applicant should be prepopulated.
*
* Only prepopulates if the participant is fresh.
*/
public function shouldPrepopulateApplicant(ParticipantDto $applicant): bool
{
return null === $applicant->firstName || '' === $applicant->firstName;
}
/**
* Checks if the participant's last name triggers dummy data fill.
*
* Only matches in create mode to avoid accidental triggering during edits.
*/
public function isDummyDataFillRequested(ParticipantDto $participant, string $mode): bool
{
if (BookingDto::MODE_CREATE !== $mode) {
return false;
}
return self::TOKEN === $participant->lastName;
}
/**
* Fills the participant DTO with generated dummy personal data.
*/
public function fillDummyParticipant(ParticipantDto $participant, int $participantIndex): void
{
$participantNumber = $participantIndex + 1;
$now = CarbonImmutable::now();
$participant->firstName = sprintf('Vorname %d', $participantNumber);
$participant->lastName = sprintf('Muster %d %s', $participantNumber, $now->format('H:i'));
$participant->mobile = '0171/111111';
$participant->email = sprintf('teilnehmer.in%[email protected]', $participantNumber);
$participant->dateOfBirth = $now->subYears(20)->toImmutable();
$address = new Address();
$address->street = 'Musterstr. 123';
$address->postCode = '99999';
$address->city = 'MusterOrt';
$address->country = 'Deutschland';
$participant->address = $address;
}
}