wip: check eligibility based on ski pass availability
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\Form\Model\BookingDtoInterface;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Spatie\Blink\Blink;
|
||||
|
||||
/**
|
||||
* Service for evaluating participant eligibility for booking.
|
||||
*
|
||||
* A participant is considered eligible when at least one skipass is available
|
||||
* for their age at the travel date. This service provides the business logic
|
||||
* for eligibility checks used by both the conditional field state system and
|
||||
* the view layer via Twig extension.
|
||||
*
|
||||
* Results are cached per request using Blink to avoid redundant calculations
|
||||
* when checking the same participant multiple times.
|
||||
*/
|
||||
class ParticipantEligibilityService
|
||||
{
|
||||
/**
|
||||
* Checks if a participant is eligible for booking.
|
||||
*
|
||||
* Returns true when the participant can book services (at least one skipass available).
|
||||
* Returns false when the participant cannot book (no skipasses available for their age).
|
||||
*
|
||||
* Results are cached per request to avoid redundant calculations.
|
||||
*
|
||||
* @param BookingDtoInterface $bookingDto The current booking data
|
||||
* @param int $participantIndex The index of the participant being evaluated
|
||||
*
|
||||
* @return bool True if participant is eligible (has available skipasses)
|
||||
*/
|
||||
public function isParticipantEligible(BookingDtoInterface $bookingDto, int $participantIndex): bool
|
||||
{
|
||||
$participant = $bookingDto->getParticipant($participantIndex);
|
||||
|
||||
if (null === $participant || null === $participant->dateOfBirth) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create cache key based on participant's birth date, travel date, and index
|
||||
$cacheKey = sprintf(
|
||||
'participant_eligibility_%s_%s_%d',
|
||||
$participant->dateOfBirth->format('Y-m-d'),
|
||||
$bookingDto->travel->dateFrom->format('Y-m-d'),
|
||||
$participantIndex
|
||||
);
|
||||
|
||||
return Blink::global()->once($cacheKey, function () use ($bookingDto, $participantIndex) {
|
||||
$allSkiPasses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true, true);
|
||||
$availableSkiPasses = array_filter(
|
||||
$allSkiPasses,
|
||||
fn (Service $service) => $this->isSkiPassAvailableForParticipant($service, $bookingDto, $participantIndex)
|
||||
);
|
||||
|
||||
return !empty($availableSkiPasses);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a skipass service is available for the given participant based on age constraints.
|
||||
*/
|
||||
private function isSkiPassAvailableForParticipant(Service $service, BookingDtoInterface $bookingDto, int $participantIndex): bool
|
||||
{
|
||||
$participant = $bookingDto->getParticipant($participantIndex);
|
||||
|
||||
if (null === $participant || null === $participant->dateOfBirth) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// No age constraints = available to all
|
||||
if (null === $service->ageConstraintType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$travelStartDate = CarbonImmutable::instance($bookingDto->travel->dateFrom);
|
||||
$birthDate = CarbonImmutable::instance($participant->dateOfBirth);
|
||||
$ageAtTravelStart = $travelStartDate->diffInYears($birthDate);
|
||||
$birthYear = (int) $birthDate->format('Y');
|
||||
|
||||
$constraintType = $service->ageConstraintType ?? 'absolute_age';
|
||||
|
||||
if ('absolute_age' === $constraintType || 'mixed' === $constraintType) {
|
||||
$minAge = $service->ageFrom;
|
||||
$maxAge = $service->ageTo;
|
||||
|
||||
if (null !== $minAge && $ageAtTravelStart < $minAge) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null !== $maxAge && $ageAtTravelStart > $maxAge) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ('birth_year' === $constraintType || 'mixed' === $constraintType) {
|
||||
$minBirthYear = $service->birthYearFrom;
|
||||
$maxBirthYear = $service->birthYearTo;
|
||||
|
||||
if (null !== $minBirthYear && $birthYear < $minBirthYear) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null !== $maxBirthYear && $birthYear > $maxBirthYear) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user