feat: refactoring and cleanup
This commit is contained in:
@@ -182,4 +182,33 @@ abstract class AbstractParticipantFieldHandler implements ParticipantFieldHandle
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds an item by ID from an array of objects.
|
||||
*
|
||||
* Generic helper for validating a selected ID against an array of available
|
||||
* items and returning the matching object. Used by handlers that need to
|
||||
* validate service, pickup, or other selections against available options.
|
||||
*
|
||||
* @param mixed $selectedId The submitted ID to find (string or int)
|
||||
* @param object[] $items Array of objects with an `id` property
|
||||
*
|
||||
* @return object|null The matching item, or null if not found or invalid ID
|
||||
*/
|
||||
protected function findItemById(mixed $selectedId, array $items): ?object
|
||||
{
|
||||
if (null === $selectedId || (false === is_string($selectedId) && false === is_int($selectedId))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = (int) $selectedId;
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item->id === $id) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Service;
|
||||
|
||||
use App\BusProNet\Model\Pickup;
|
||||
use App\BusProNet\Utility\DirectionMapper;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
|
||||
@@ -58,36 +57,6 @@ class ParticipantPickupFieldHandler extends AbstractParticipantFieldHandler
|
||||
$selectedPickup = $this->getFieldValue($submittedData, $this->getFieldName());
|
||||
|
||||
// Validate pickup selection against available outbound pickups
|
||||
$validSelection = null;
|
||||
if (null !== $selectedPickup) {
|
||||
$validSelection = $this->findValidPickup($selectedPickup, $bookingDto->travel->pickupsOutbound);
|
||||
}
|
||||
|
||||
$participant->pickup = $validSelection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a valid pickup from available pickups.
|
||||
*
|
||||
* @param mixed $selectedPickupId The submitted pickup ID
|
||||
* @param array<int, Pickup> $availablePickups Array of available pickup locations
|
||||
*
|
||||
* @return Pickup|null The valid pickup object, or null if invalid
|
||||
*/
|
||||
private function findValidPickup(mixed $selectedPickupId, array $availablePickups): ?Pickup
|
||||
{
|
||||
if (null === $selectedPickupId || false === is_string($selectedPickupId) && false === is_int($selectedPickupId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pickupId = (int) $selectedPickupId;
|
||||
|
||||
foreach ($availablePickups as $pickup) {
|
||||
if ($pickup->id === $pickupId) {
|
||||
return $pickup;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
$participant->pickup = $this->findItemById($selectedPickup, $bookingDto->travel->pickupsOutbound);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ 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;
|
||||
@@ -57,43 +56,7 @@ class ParticipantTransportationInboundFieldHandler extends AbstractParticipantFi
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
// Validate and update participant with selection
|
||||
$participant->transportationInbound = $this->findItemById($selectedTransportation, $availableServices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ 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;
|
||||
@@ -57,43 +56,7 @@ class ParticipantTransportationOutboundFieldHandler extends AbstractParticipantF
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
// Validate and update participant with selection
|
||||
$participant->transportationOutbound = $this->findItemById($selectedTransportation, $availableServices);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user