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

103 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service;
use App\BusProNet\Model\Service;
use App\BusProNet\Utility\DirectionMapper;
use App\Form\Model\BookingDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
* Handles processing of outbound transportation service selection.
*
* This handler manages outbound (HIN) transportation options including bus and
* self-organized (PKW) services with pricing and availability validation.
* It processes the transportationOutbound field from form submissions and updates
* the participant DTO with validated selections.
*/
class ParticipantTransportationOutboundFieldHandler extends AbstractParticipantFieldHandler
{
public function getFieldName(): string
{
return 'transportationOutbound';
}
/**
* Determines if this handler should process the field based on submitted data.
*
* For transportation selection fields, 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 transportation is selected.
*
* @param array<string, mixed> $submittedData The submitted participant form data
* @param string $mode The booking mode (BookingDto::MODE_CREATE or MODE_EDIT)
* @param int $participantIndex The index of the participant being processed
*
* @return bool Always returns true for transportation selection fields
*/
public function shouldProcess(array $submittedData, string $mode, int $participantIndex): bool
{
return true; // Always process to handle deselection cases
}
public function processField(array $submittedData, BookingDto $bookingDto, int $participantIndex): void
{
$participant = $this->getParticipant($bookingDto, $participantIndex);
if (null === $participant) {
return;
}
$selectedTransportation = $this->getFieldValue($submittedData, $this->getFieldName());
// Get available outbound transportation services
$availableServices = $bookingDto->travel->getTransportationServicesByDirection(
DirectionMapper::OUTBOUND_TRAVEL,
true // filter available
);
// Validate and convert selection to Service object
$validSelection = null;
if (null !== $selectedTransportation) {
$validSelection = $this->findValidTransportationService(
$selectedTransportation,
$availableServices
);
}
// Update participant with validated selection
$participant->transportationOutbound = $validSelection;
// Backward compatibility: also update deprecated property
$participant->transportationServiceTo = $validSelection;
}
/**
* Finds a valid transportation service from available services.
*
* @param mixed $selectedServiceId The submitted service ID
* @param array<int, Service> $availableServices Array of available transportation services
*
* @return Service|null The valid service object, or null if invalid
*/
private function findValidTransportationService(
mixed $selectedServiceId,
array $availableServices,
): ?Service {
if (null === $selectedServiceId || false === is_string($selectedServiceId) && false === is_int($selectedServiceId)) {
return null;
}
$serviceId = (int) $selectedServiceId;
foreach ($availableServices as $service) {
if ($service->id === $serviceId) {
return $service;
}
}
return null;
}
}