wip: transportation services
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Service;
|
||||
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\BusProNet\Utility\DirectionMapper;
|
||||
use App\Form\Model\BookingDtoInterface;
|
||||
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
|
||||
|
||||
/**
|
||||
* Handles processing of inbound transportation service selection.
|
||||
*
|
||||
* This handler manages inbound (RUECK) transportation options including bus and
|
||||
* self-organized (PKW) services with pricing and availability validation.
|
||||
* It processes the transportationInbound field from form submissions and updates
|
||||
* the participant DTO with validated selections.
|
||||
*/
|
||||
class ParticipantTransportationInboundFieldHandler extends AbstractParticipantFieldHandler
|
||||
{
|
||||
public function getFieldName(): string
|
||||
{
|
||||
return 'transportationInbound';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int $participantIndex The index of the participant being processed
|
||||
*
|
||||
* @return bool Always returns true for transportation selection fields
|
||||
*/
|
||||
public function shouldProcess(array $submittedData, int $participantIndex): bool
|
||||
{
|
||||
return true; // Always process to handle deselection cases
|
||||
}
|
||||
|
||||
public function processField(array $submittedData, BookingDtoInterface $bookingDto, int $participantIndex): void
|
||||
{
|
||||
$participant = $this->getParticipant($bookingDto, $participantIndex);
|
||||
if (null === $participant) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selectedTransportation = $this->getFieldValue($submittedData, $this->getFieldName());
|
||||
|
||||
// Get available inbound transportation services
|
||||
$availableServices = $bookingDto->travel->getTransportationServicesByDirection(
|
||||
DirectionMapper::INBOUND_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->transportationInbound = $validSelection;
|
||||
|
||||
// Backward compatibility: also update deprecated property
|
||||
$participant->transportationServiceFro = $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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user