Files
myep/src/Form/Service/Condition/BulkInsuranceBookingCondition.php
T

98 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\Form\Model\BookingDto;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that evaluates whether bulk insurance booking is active for dependent participants.
*
* This condition determines if the applicant (first participant) has enabled bulk insurance
* booking for all participants. When active, dependent participants (index > 0) will have
* their insurance field replaced with a "wie Anmelder" (same as applicant) message, and the
* applicant's insurance selection (or lack thereof) will be automatically applied to all
* participants based on their individual travel prices and age constraints.
*
* The condition is satisfied when:
* 1. Evaluating a dependent participant (not the applicant)
* 2. The applicant has bulkInsuranceBooking flag set to true
*/
class BulkInsuranceBookingCondition implements FieldConditionInterface
{
/**
* Evaluates if bulk insurance booking is active for the given participant.
*
* For the applicant (index 0), always returns false since they control the bulk booking.
* For dependent participants, returns true if the applicant has bulk insurance booking enabled,
* regardless of whether an insurance is selected (applies to "no insurance" as well).
*
* @param BookingDto $bookingDto The current booking data
* @param int $participantIndex The index of the participant being evaluated
* @param array<string, mixed> $formData Current form data for condition evaluation
*
* @return bool True if bulk insurance booking is active for this dependent participant
*/
public function evaluate(BookingDto $bookingDto, int $participantIndex, array $formData): bool
{
// Applicant is never affected by bulk insurance booking (they control it)
if (0 === $participantIndex) {
return false;
}
// Get applicant data
$applicant = $bookingDto->getParticipant(0);
if (null === $applicant) {
return false;
}
// Check if applicant has bulk insurance booking enabled
// No need to check for insurance selection - bulk applies even when no insurance is selected
return $this->getBulkInsuranceBookingValue($formData, $applicant);
}
/**
* Returns field names that this condition depends on.
*
* This condition depends on the applicant's bulkInsuranceBooking flag.
* Any changes to this field should trigger re-evaluation of dependent participant field states.
*
* @return string[] Array containing field names this condition depends on
*/
public function getDependentFields(): array
{
return ['bulkInsuranceBooking'];
}
/**
* Returns a human-readable description of this condition.
*
* @return string Description of the bulk insurance booking condition
*/
public function getDescription(): string
{
return 'Applicant has bulk insurance booking enabled';
}
/**
* Gets the bulk insurance booking flag value from form data or participant DTO.
*/
/** @param array<string, mixed> $formData */
private function getBulkInsuranceBookingValue(array $formData, object $applicant): bool
{
// First check form data (for fresh submissions)
if (isset($formData['participants'][0]['bulkInsuranceBooking'])) {
return (bool) $formData['participants'][0]['bulkInsuranceBooking'];
}
// Fall back to participant DTO
if (property_exists($applicant, 'bulkInsuranceBooking')) {
return (bool) $applicant->bulkInsuranceBooking;
}
return false;
}
}