wip: rentals insurance field
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Service;
|
||||
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\Form\Model\BookingDtoInterface;
|
||||
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
|
||||
* rental services, creating a dependency chain where rental insurance depends
|
||||
* on rental selections.
|
||||
*
|
||||
* Dependencies: dateOfBirth (for age evaluation) 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 both dateOfBirth (for age evaluation) and rentals
|
||||
* (because rental insurance is only relevant when rentals are selected).
|
||||
*
|
||||
* @return string[] Array containing 'dateOfBirth' and 'rentals' dependencies
|
||||
*/
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return ['dateOfBirth', '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 BookingDtoInterface $bookingDto The booking DTO to update (create or edit)
|
||||
* @param int $participantIndex The index of the participant being processed
|
||||
*/
|
||||
public function processField(array $submittedData, BookingDtoInterface $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 rental selections
|
||||
$hasRentals = false === empty($participant->rentals);
|
||||
|
||||
if (false === $hasRentals) {
|
||||
// If 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 BookingDtoInterface $bookingDto The booking DTO containing travel data
|
||||
*
|
||||
* @return Service|null The rental insurance service object, or null if not found
|
||||
*/
|
||||
private function findRentalInsuranceService(BookingDtoInterface $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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user