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

115 lines
4.1 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\BookingDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
* Handles parking service selection for self-organized transportation.
*
* Parking is only available when outbound transportation is PKW (car).
* This is because parking is needed at the destination for those arriving by car.
* Automatically clears parking when outbound transportation is not PKW.
*/
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 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 parking selection fields
*/
public function shouldProcess(array $submittedData, string $mode, int $participantIndex): bool
{
return true; // Always process for state changes
}
public function processField(array $submittedData, BookingDto $bookingDto, int $participantIndex): void
{
$participant = $this->getParticipant($bookingDto, $participantIndex);
if (null === $participant) {
return;
}
// Check if parking is applicable (outbound transportation is PKW)
if (!$this->isParkingApplicable($participant)) {
$participant->parking = false; // Clear parking when outbound is not PKW
$participant->parkingService = null; // Clear parking service object
return;
}
$parkingSelected = $this->getFieldValue($submittedData, $this->getFieldName());
$isParkingSelected = (bool) $parkingSelected;
// Store boolean value for backward compatibility
$participant->parking = $isParkingSelected;
// Store parking service object for pricing calculation
if (true === $isParkingSelected) {
$participant->parkingService = $this->findParkingService($bookingDto);
} else {
$participant->parkingService = null;
}
}
/**
* Checks if parking is applicable based on outbound transportation selection.
*
* Parking is only needed when arriving by car at the destination.
*
* @param object $participant The participant DTO
*
* @return bool True if parking is applicable, false otherwise
*/
private function isParkingApplicable(object $participant): bool
{
return DirectionMapper::SUBTYPE_CAR_API === $participant->transportationOutbound?->subType;
}
/**
* Finds the parking service from available services.
*
* Gets the first (and typically only) parking service for pricing calculation.
* Returns null if no parking services are available.
*
* @param BookingDto $bookingDto The booking DTO containing travel data
*
* @return Service|null The parking service object, or null if not found
*/
private function findParkingService(BookingDto $bookingDto): ?Service
{
$parkingServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_PARKING, true);
if (empty($parkingServices)) {
return null;
}
// Return the first parking service (there's typically only one parking type)
return reset($parkingServices);
}
}