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

136 lines
5.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
* Handles processing of the rentalInsurance field for booking participants.
*
* This handler manages rental insurance selections for participants in the booking
* creation process. It processes the rentalInsurance field from form submissions,
* validates the selection, and updates the participant DTO with the valid selection.
*
* The rental insurance field is only shown when the participant has selected both
* a skipass and rental services, creating a dependency chain where rental insurance
* depends on skipass and rental selections.
*
* Dependencies: dateOfBirth (for age evaluation), skiPass (for visibility), and rentals (for field visibility)
*/
class ParticipantRentalInsuranceFieldHandler extends AbstractParticipantFieldHandler
{
/**
* Returns the form field name this handler processes.
*
* @return string The field name 'rentalInsurance'
*/
public function getFieldName(): string
{
return 'rentalInsurance';
}
/**
* Returns the field dependencies for proper processing order.
*
* This handler depends on dateOfBirth (for age evaluation), skiPass (for field visibility),
* and rentals (because rental insurance is only relevant when rentals are selected).
*
* @return string[] Array containing 'dateOfBirth', 'skiPass', and 'rentals' dependencies
*/
public function getDependencies(): array
{
return ['dateOfBirth', 'skiPass', 'rentals'];
}
/**
* Determines if this handler should process the field based on submitted data.
*
* For service selection fields like rental insurance, we always need to process
* to handle cases where the selection is cleared (field not present in data).
* This ensures the participant DTO is updated with null when no
* rental insurance is selected.
*
* @param array<string, mixed> $submittedData The submitted participant form data
* @param int $participantIndex The index of the participant being processed
*
* @return bool Always returns true for service selection fields
*/
public function shouldProcess(array $submittedData, int $participantIndex): bool
{
return true; // Always process to handle deselection cases
}
/**
* Processes the rentalInsurance field for a specific participant.
*
* This method extracts the rental insurance selection from submitted form data,
* validates the selection against the participant's age constraints, and
* updates the participant DTO with the valid selection. If the rental insurance
* is no longer appropriate for the participant's age, it is automatically cleared.
*
* @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
{
// Safely get the participant object, returning early if not found
$participant = $this->getParticipant($bookingDto, $participantIndex);
if (null === $participant) {
return;
}
// Check if rental insurance should be visible based on skipass and rental selections
$hasSkiPass = null !== $participant->skiPass;
$hasRentals = false === empty($participant->rentals);
if (false === $hasSkiPass || false === $hasRentals) {
// If no skipass or no rentals are selected, clear rental insurance data
$participant->rentalInsuranceSelected = false;
$participant->rentalInsurance = null;
return;
}
// Extract checkbox value from submitted data (this comes from the rentalInsuranceSelected property)
$rentalInsuranceSelected = $this->getFieldValue($submittedData, $this->getFieldName());
$isRentalInsuranceSelected = (bool) $rentalInsuranceSelected;
// Store boolean value
$participant->rentalInsuranceSelected = $isRentalInsuranceSelected;
// Store service object based on checkbox state for pricing calculations
if (true === $isRentalInsuranceSelected) {
$participant->rentalInsurance = $this->findRentalInsuranceService($bookingDto);
} else {
$participant->rentalInsurance = null;
}
}
/**
* Finds the rental insurance service from available services.
*
* Gets the first (and typically only) rental insurance service.
* Returns null if no rental insurance services are available.
*
* @param BookingDto $bookingDto The booking DTO containing travel data
*
* @return Service|null The rental insurance service object, or null if not found
*/
private function findRentalInsuranceService(BookingDto $bookingDto): ?Service
{
$rentalInsuranceServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTAL_INSURANCE, true, true);
if (empty($rentalInsuranceServices)) {
return null;
}
return reset($rentalInsuranceServices); // Get the first (and typically only) rental insurance service
}
}