Files
myep/src/BusProNet/DataProcessor/PersonalDataSynchronizer.php
T

146 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\BusProNet\DataProcessor;
use App\BusProNet\Model\Address;
use App\BusProNet\Model\Booking;
use App\BusProNet\Model\PersonalData;
use App\Form\Model\ParticipantDto;
/**
* Synchronizes personal data between form DTOs and booking models.
*
* Handles the update of participant personal data, addresses, and communication
* information from form submissions to the booking model during update operations.
*/
class PersonalDataSynchronizer
{
/**
* Updates participant personal data from form input.
*
* Processes all participants regardless of status, including canceled participants (status 'S').
* Updates all personal data fields and communication information.
*
* Also fills missing mandatory fields on the applicant object for company bookings
* created by travel agencies. The applicant is separate from participant 0 and may
* contain only company data without personal fields required by the BPN API.
*
* IMPORTANT: The applicant's address must never be modified. This method updates
* participant addresses independently to ensure applicant data remains intact.
*
* @param array<ParticipantDto> $participants The participants array from the form
* @param Booking $bookingData The booking data object to update
* @param bool $isInternalAgencyBooking Whether to fill default names for participants with empty names
*/
public function updateParticipantPersonalData(
array $participants,
Booking $bookingData,
bool $isInternalAgencyBooking = false,
): void {
// Fill missing mandatory fields on applicant (for company bookings by travel agencies)
// This is separate from participants - applicant may be a company while participants are real people
$this->fillMissingApplicantFields($bookingData);
foreach ($participants as $participant) {
$bookingData->participants[$participant->index]->firstName = $participant->firstName;
$bookingData->participants[$participant->index]->name = $participant->lastName;
// For internal agency bookings, participants with empty names get default values.
// Agencies pre-book slots with placeholder data (e.g. applicant surname, empty first name),
// and the BPN API rejects updates with empty name fields.
if ($isInternalAgencyBooking) {
$defaultName = sprintf('Teilnehmer:in %d', $participant->index + 1);
$personalData = $bookingData->participants[$participant->index];
if (null === $personalData->firstName || '' === $personalData->firstName) {
$personalData->firstName = $defaultName;
}
if (null === $personalData->name || '' === $personalData->name) {
$personalData->name = $defaultName;
}
}
$bookingData->participants[$participant->index]->dateOfBirth = $participant->dateOfBirth;
$bookingData->participants[$participant->index]->gender = $participant->gender;
$bookingData->participants[$participant->index]->nationality = $participant->nationality;
$this->fillMissingParticipantFields($bookingData->participants[$participant->index]);
$bookingData->participants[$participant->index]->height = $participant->height;
$bookingData->participants[$participant->index]->weight = $participant->weight;
$bookingData->participants[$participant->index]->shoeSize = $participant->shoeSize;
// Update address if provided
// Create new Address instance to avoid modifying any shared object references
if (null !== $participant->address) {
$newAddress = new Address();
$newAddress->street = $participant->address->street;
$newAddress->postCode = $participant->address->postCode;
$newAddress->city = $participant->address->city;
$newAddress->district = $participant->address->district;
$newAddress->country = $participant->address->country;
$bookingData->participants[$participant->index]->address = $newAddress;
}
if ($participant->email || $participant->mobile) {
$bookingData->participants[$participant->index]->communication->email = $participant->email;
$bookingData->participants[$participant->index]->communication->mobile = $participant->mobile;
}
}
}
/**
* Fills missing mandatory fields on the applicant for company bookings.
*
* Travel agencies often create bookings with company data as the applicant,
* which lacks personal data fields. These fields are mandatory for the BPN
* API, so we fill them with sensible defaults. The applicant is separate from
* participants - participant 0 is a real person and must not be modified here.
*/
private function fillMissingApplicantFields(Booking $bookingData): void
{
if (null === $bookingData->applicant) {
return;
}
$applicant = $bookingData->applicant;
if (null === $applicant->firstName || '' === $applicant->firstName) {
$applicant->firstName = 'Anmelder';
}
if (null === $applicant->gender || '' === $applicant->gender) {
$applicant->gender = 'D';
}
if (null === $applicant->nationality || '' === $applicant->nationality) {
$applicant->nationality = 'D';
}
if (null === $applicant->dateOfBirth) {
$applicant->dateOfBirth = new \DateTimeImmutable('-20 years');
}
if (null === $applicant->communication->mobile || '' === $applicant->communication->mobile) {
$applicant->communication->mobile = '12345';
}
}
/**
* Fills missing mandatory fields on a participant.
*
* Ensures mandatory fields have sensible defaults before submission to the
* BPN API. Currently handles nationality which defaults to 'D' (German).
*
* @param PersonalData $participant The participant to fill defaults for
*/
private function fillMissingParticipantFields(PersonalData $participant): void
{
if (null === $participant->nationality || '' === $participant->nationality) {
$participant->nationality = 'D';
}
}
}