Files
myep/src/Form/Service/ParticipantEmailFieldHandler.php
T

111 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
* Handles the email field, pre-filling the applicant's address for under-16 participants.
*
* BusPro requires a Mail-Adresse for every participant once a room is present and rejects the
* step-3 Anfrage with 650 otherwise, children included. It does accept an address identical to
* the Anmelder's, so children inherit the applicant's rather than being asked for one they
* typically do not have. The value stays editable and stays mandatory. This happens in the
* create flow only; in edit mode the handler just stores what was submitted.
*
* The copy only ever fills a blank, so an address somebody typed is never overwritten. A later
* correction to the applicant's own address therefore does not propagate to children who already
* inherited the previous one.
*
* Depends on dateOfBirth: whether a participant counts as a child is only known once their date
* of birth has been processed.
*/
class ParticipantEmailFieldHandler extends AbstractParticipantFieldHandler
{
public function getFieldName(): string
{
return 'email';
}
/**
* @return string[]
*/
public function getDependencies(): array
{
return ['dateOfBirth'];
}
/**
* @param array<string, mixed> $submittedData The submitted participant form data
* @param BookingDto $bookingDto The booking DTO to update (create or edit)
* @param int $participantIndex The index of the participant being processed
*/
public function processField(array $submittedData, BookingDto $bookingDto, int $participantIndex): void
{
$participant = $this->getParticipant($bookingDto, $participantIndex);
if (false === $participant instanceof ParticipantDto) {
return;
}
$submitted = $this->getFieldValue($submittedData, $this->getFieldName());
if (true === is_string($submitted)) {
$participant->email = '' !== trim($submitted) ? trim($submitted) : null;
}
$this->applyApplicantEmail($bookingDto, $participant);
}
/**
* Copies the applicant's address onto a child who has none of their own.
*/
public function applyApplicantEmail(BookingDto $bookingDto, ParticipantDto $participant): void
{
// Create mode only. An existing booking already carries whatever BusPro accepted, a
// blank may be intended where personal data is optional, and participant[0] is not
// reliably the applicant there.
if (BookingDto::MODE_CREATE !== $bookingDto->getMode()) {
return;
}
if (false === $participant->isChild()) {
return;
}
$applicantEmail = $this->resolveApplicantEmail($bookingDto);
if (null === $applicantEmail) {
return;
}
// The applicant themselves never inherits, and on an ordinary booking that is participant[0].
if (true === $participant->isApplicant() && true === $bookingDto->applicantTravels) {
return;
}
if (null === $participant->email || '' === trim($participant->email)) {
$participant->email = $applicantEmail;
}
}
private function resolveApplicantEmail(BookingDto $bookingDto): ?string
{
$applicant = $bookingDto->applicantTravels
? ($bookingDto->participants[0] ?? null)
: $bookingDto->applicant;
$email = $applicant?->email;
if (null === $email || '' === trim($email)) {
return null;
}
return trim($email);
}
}