116 lines
4.0 KiB
PHP
116 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Form\Service;
|
|
|
|
use App\BusProNet\Constants;
|
|
use App\BusProNet\Model\Service;
|
|
use App\BusProNet\Utility\DirectionMapper;
|
|
use App\Form\Model\BookingDtoInterface;
|
|
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
|
|
|
|
/**
|
|
* Handles parking service selection for self-organized transportation.
|
|
*
|
|
* Parking is only available when at least one direction uses
|
|
* self-organized (PKW) transportation. Automatically clears parking
|
|
* when both transportation directions are bus-only.
|
|
*/
|
|
class ParticipantParkingFieldHandler extends AbstractParticipantFieldHandler
|
|
{
|
|
public function getFieldName(): string
|
|
{
|
|
return 'parking';
|
|
}
|
|
|
|
public function getDependencies(): array
|
|
{
|
|
return ['transportationOutbound', 'transportationInbound'];
|
|
}
|
|
|
|
/**
|
|
* Determines if this handler should process the field based on submitted data.
|
|
*
|
|
* For parking selection fields, we always need to process to handle cases
|
|
* where the parking is cleared due to transportation changes. This ensures the
|
|
* participant DTO is updated correctly when transportation switches to bus-only.
|
|
*
|
|
* @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 parking selection fields
|
|
*/
|
|
public function shouldProcess(array $submittedData, int $participantIndex): bool
|
|
{
|
|
return true; // Always process for state changes
|
|
}
|
|
|
|
public function processField(array $submittedData, BookingDtoInterface $bookingDto, int $participantIndex): void
|
|
{
|
|
$participant = $this->getParticipant($bookingDto, $participantIndex);
|
|
if (null === $participant) {
|
|
return;
|
|
}
|
|
|
|
// Check if parking is applicable (at least one PKW direction)
|
|
if (!$this->isParkingApplicable($participant)) {
|
|
$participant->parking = null; // Clear parking for bus-only transport
|
|
|
|
return;
|
|
}
|
|
|
|
$selectedParking = $this->getFieldValue($submittedData, $this->getFieldName());
|
|
|
|
// Get available parking services (subtype PAR)
|
|
$availableParkingServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_PARKING, true);
|
|
|
|
$validSelection = null;
|
|
if (null !== $selectedParking) {
|
|
$validSelection = $this->findValidParkingService($selectedParking, $availableParkingServices);
|
|
}
|
|
|
|
$participant->parking = $validSelection;
|
|
}
|
|
|
|
/**
|
|
* Checks if parking is applicable based on transportation selections.
|
|
*
|
|
* @param object $participant The participant DTO
|
|
*
|
|
* @return bool True if parking is applicable, false otherwise
|
|
*/
|
|
private function isParkingApplicable(object $participant): bool
|
|
{
|
|
$outboundIsPkw = DirectionMapper::SUBTYPE_CAR_API === $participant->transportationOutbound?->subType;
|
|
$inboundIsPkw = DirectionMapper::SUBTYPE_CAR_API === $participant->transportationInbound?->subType;
|
|
|
|
return $outboundIsPkw || $inboundIsPkw;
|
|
}
|
|
|
|
/**
|
|
* Finds a valid parking service from available parking services.
|
|
*
|
|
* @param mixed $selectedServiceId The submitted service ID
|
|
* @param array<int, Service> $availableServices Array of available parking services
|
|
*
|
|
* @return Service|null The valid service object, or null if invalid
|
|
*/
|
|
private function findValidParkingService(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;
|
|
}
|
|
}
|