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

206 lines
8.0 KiB
PHP

<?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 skiPass field for booking participants.
*
* This handler manages skipass selection for participants in the booking
* creation process. It processes the skiPass field from form submissions,
* validates age-appropriate and date-valid skipasses, and updates the
* participant DTO with the valid selection.
*
* Key responsibilities:
* - Validates single skipass selection against age constraints
* - Validates skipass date ranges against travel dates
* - Clears skipass if no longer available due to age or date changes
* - Maintains data consistency during HTMX form updates
* - Prevents form validation errors from stale skipass selections
*
* Dependencies: dateOfBirth (must be processed first for age evaluation)
*/
class ParticipantSkiPassFieldHandler extends AbstractParticipantFieldHandler
{
/**
* Returns the form field name this handler processes.
*
* @return string The field name 'skiPass'
*/
public function getFieldName(): string
{
return 'skiPass';
}
/**
* Returns the field dependencies for proper processing order.
*
* This handler depends on dateOfBirth being processed first because
* age evaluation requires the participant's birth date to be available.
*
* @return string[] Array containing 'dateOfBirth' dependency
*/
public function getDependencies(): array
{
return ['dateOfBirth'];
}
/**
* Determines if this handler should process the field based on submitted data.
*
* For service selection fields like skipasses, 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
* skipass 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 skiPass field for a specific participant.
*
* This method extracts the skipass selection from submitted form data,
* validates the selection against the participant's age constraints and
* travel date constraints, and updates the participant DTO with the valid
* selection. If the skipass is no longer appropriate for the participant's
* age or exceeds the travel date range, 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;
}
// Extract current skipass selection from submitted data
$selectedSkiPass = $this->getFieldValue($submittedData, $this->getFieldName());
// Get available skipasses from travel data (with date filtering)
$availableSkipasses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true, true);
// For single selection, validate the selected skipass and convert ID to Service object
$validSelection = null;
if (null !== $selectedSkiPass) {
if ($this->isServiceValidForParticipant($selectedSkiPass, $availableSkipasses, $bookingDto, $participantIndex)) {
// Convert the submitted ID back to the Service object
$validSelection = $this->findServiceInAvailableServices($selectedSkiPass, $availableSkipasses);
}
}
// Update participant with validated selection
$participant->skiPass = $validSelection;
}
/**
* Validates if a selected skipass is still valid for the participant.
*
* This method checks both age constraints (via birth year ranges) and
* date constraints (skipass dates must be within travel dates).
*
* @param mixed $selectedService The selected skipass to validate
* @param array $availableServices Array of available skipasses
* @param BookingDtoInterface $bookingDto The booking DTO for context
* @param int $participantIndex The participant index for age evaluation
*
* @return bool True if the skipass is valid for the participant, false otherwise
*/
private function isServiceValidForParticipant(
mixed $selectedService,
array $availableServices,
BookingDtoInterface $bookingDto,
int $participantIndex,
): bool {
// Find the service in available services
$service = $this->findServiceInAvailableServices($selectedService, $availableServices);
if (null === $service) {
return false; // Service not found in available services
}
// Check if service has age constraints
$ageEvaluator = new ServiceAgeEvaluator();
if ($ageEvaluator->canEvaluate($service)) {
// Validate service against participant's age
if (false === $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex)) {
return false; // Age constraints not met
}
}
// Date constraints are already handled by the travel->getAdditionalServicesBySubTypes
// method with date filtering enabled (true, true parameters), so if the service
// is in the available services list, it already passed date validation.
return true; // Service passed both age and date validation
}
/**
* Finds a selected skipass in the list of available skipasses.
*
* @param mixed $selectedService The selected skipass to find
* @param array $availableServices Array of available Service objects
*
* @return Service|null The found service or null if not found
*/
private function findServiceInAvailableServices(mixed $selectedService, array $availableServices): ?Service
{
foreach ($availableServices as $availableService) {
if ($this->servicesMatch($selectedService, $availableService)) {
return $availableService;
}
}
return null;
}
/**
* Determines if a selected skipass matches an available skipass.
*
* @param mixed $selectedService The selected skipass from form data
* @param Service $availableService The available skipass to compare against
*
* @return bool True if the skipasses match, false otherwise
*/
private function servicesMatch(mixed $selectedService, Service $availableService): bool
{
// Direct object comparison
if ($selectedService === $availableService) {
return true;
}
// ID comparison for Service objects
if ($selectedService instanceof Service) {
return $selectedService->id === $availableService->id;
}
// ID comparison for numeric values
if (is_numeric($selectedService)) {
return (int) $selectedService === $availableService->id;
}
// String ID comparison
if (is_string($selectedService)) {
return $selectedService === (string) $availableService->id;
}
return false;
}
}