feat: performance improvements
This commit is contained in:
@@ -35,6 +35,9 @@ use App\Service\ServiceAvailabilityCalculator;
|
||||
*/
|
||||
class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
|
||||
{
|
||||
/** @var array<string, array<string, mixed>> Request-scoped cache for field options */
|
||||
private array $optionsCache = [];
|
||||
|
||||
/**
|
||||
* Initializes the provider with required dependencies.
|
||||
*
|
||||
@@ -57,6 +60,39 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves form field options with request-scoped caching.
|
||||
*
|
||||
* Overrides parent to add instance-level caching, preventing redundant
|
||||
* field option calculations when the same field is accessed multiple times
|
||||
* during form building (happens ~16 times per participant × 50 participants = 800 calls).
|
||||
*
|
||||
* Cache key includes: field name, participant index, mode, and date of birth.
|
||||
* The cache is automatically cleared between requests since the service is request-scoped.
|
||||
*
|
||||
* @param string $fieldName The name of the field to configure
|
||||
* @param BookingDto $bookingDto The current booking data for context
|
||||
* @param int $participantIndex The index of the participant being configured
|
||||
* @param array $options Additional options to customize field behavior
|
||||
*
|
||||
* @return array<string, mixed> Symfony form field options, or empty array if field not supported
|
||||
*/
|
||||
public function getFieldOptions(string $fieldName, BookingDto $bookingDto, int $participantIndex, array $options = []): array
|
||||
{
|
||||
// Generate cache key based on factors that affect field options
|
||||
$participant = $bookingDto->getParticipant($participantIndex);
|
||||
$cacheKey = sprintf(
|
||||
'%s_%d_%s_%s',
|
||||
$fieldName,
|
||||
$participantIndex,
|
||||
$bookingDto->getMode(),
|
||||
$participant?->dateOfBirth?->format('Y-m-d') ?? 'no_dob'
|
||||
);
|
||||
|
||||
// Return cached result if available, otherwise compute and cache
|
||||
return $this->optionsCache[$cacheKey] ??= parent::getFieldOptions($fieldName, $bookingDto, $participantIndex, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers all field option providers during service initialization.
|
||||
*
|
||||
|
||||
@@ -20,6 +20,9 @@ use App\Form\Model\ParticipantDto;
|
||||
*/
|
||||
class BookingPriceCalculatorService
|
||||
{
|
||||
/** @var array<string, float> Request-scoped cache for participant prices */
|
||||
private array $participantPriceCache = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly ParticipantEligibilityService $participantEligibilityService,
|
||||
private readonly InsuranceService $insuranceService,
|
||||
@@ -324,22 +327,45 @@ class BookingPriceCalculatorService
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
$totalPrice = 0.0;
|
||||
// Generate cache key based on participant state that affects pricing
|
||||
$stateComponents = [
|
||||
'room' => $participant->assignedRoomId ?? 'none',
|
||||
'skiPass' => $participant->skiPass?->id ?? 'none',
|
||||
'rentalInsurance' => $participant->rentalInsurance?->id ?? 'none',
|
||||
'transportationOut' => $participant->transportationOutbound?->id ?? 'none',
|
||||
'transportationIn' => $participant->transportationInbound?->id ?? 'none',
|
||||
'pickup' => $participant->pickup?->id ?? 'none',
|
||||
'parking' => $participant->parkingService?->id ?? 'none',
|
||||
'courses' => implode('_', array_map(fn ($s) => $s->id, $participant->courses ?? [])),
|
||||
'additionalServices' => implode('_', array_map(fn ($s) => $s->id, $participant->additionalServices ?? [])),
|
||||
'board' => implode('_', array_map(fn ($s) => $s->id, $participant->board ?? [])),
|
||||
'rentals' => implode('_', array_map(fn ($s) => $s->id, $participant->rentals ?? [])),
|
||||
];
|
||||
|
||||
// Add room price if participant is assigned to a room
|
||||
if (null !== $participant->assignedRoomId) {
|
||||
$room = $this->getRoomById($bookingDto, $participant->assignedRoomId);
|
||||
if (null !== $room && null !== $room->price) {
|
||||
// Each participant pays the full room price
|
||||
$totalPrice += $room->price;
|
||||
$cacheKey = sprintf(
|
||||
'participant_price_%d_%s',
|
||||
$participantIndex,
|
||||
md5(json_encode($stateComponents))
|
||||
);
|
||||
|
||||
return $this->participantPriceCache[$cacheKey] ??= (function () use ($bookingDto, $participant) {
|
||||
$totalPrice = 0.0;
|
||||
|
||||
// Add room price if participant is assigned to a room
|
||||
if (null !== $participant->assignedRoomId) {
|
||||
$room = $this->getRoomById($bookingDto, $participant->assignedRoomId);
|
||||
if (null !== $room && null !== $room->price) {
|
||||
// Each participant pays the full room price
|
||||
$totalPrice += $room->price;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add service prices for this participant (excluding insurance)
|
||||
// For insurance eligibility calculation, only include services marked with versicherungsberechnung='J'
|
||||
$totalPrice += $this->calculateParticipantServiceTotal($participant, false, null, true);
|
||||
// Add service prices for this participant (excluding insurance)
|
||||
// For insurance eligibility calculation, only include services marked with versicherungsberechnung='J'
|
||||
$totalPrice += $this->calculateParticipantServiceTotal($participant, false, null, true);
|
||||
|
||||
return $totalPrice;
|
||||
return $totalPrice;
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,20 +10,23 @@ use App\BusProNet\Traits\SortByPriceTrait;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use Carbon\Carbon;
|
||||
use Spatie\Blink\Blink;
|
||||
|
||||
/**
|
||||
* Consolidated service for all insurance-related operations.
|
||||
*
|
||||
* Handles insurance eligibility evaluation, filtering, caching, and assignment logic.
|
||||
* Uses request-scoped caching via Blink to optimize performance for bookings with many participants.
|
||||
* Uses request-scoped instance-level caching to optimize performance for bookings with many participants.
|
||||
* This service is stateless and has no dependencies to avoid circular dependency issues.
|
||||
*/
|
||||
class InsuranceService
|
||||
{
|
||||
use SortByPriceTrait;
|
||||
|
||||
private const CACHE_KEY_PREFIX = 'selectable_insurances_';
|
||||
/** @var array<string, array<Insurance>> Request-scoped cache for selectable insurances */
|
||||
private array $selectableInsurancesCache = [];
|
||||
|
||||
/** @var array<string, array<Insurance>> Request-scoped cache for eligible insurances */
|
||||
private array $eligibleInsurancesCache = [];
|
||||
|
||||
/**
|
||||
* Returns selectable (non-complementary) insurances for a travel with request-scoped caching.
|
||||
@@ -38,11 +41,9 @@ class InsuranceService
|
||||
*/
|
||||
public function getSelectableInsurances(Travel $travel): array
|
||||
{
|
||||
$cacheKey = self::CACHE_KEY_PREFIX.$travel->id;
|
||||
$cacheKey = 'selectable_insurances_'.$travel->id;
|
||||
|
||||
return Blink::global()->once($cacheKey, function () use ($travel) {
|
||||
return $this->filterNonComplementary($travel->insurances ?? []);
|
||||
});
|
||||
return $this->selectableInsurancesCache[$cacheKey] ??= $this->filterNonComplementary($travel->insurances ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,24 +69,42 @@ class InsuranceService
|
||||
return []; // Cannot evaluate without travel dates
|
||||
}
|
||||
|
||||
$bookingDate = Carbon::now()->toDateTimeImmutable();
|
||||
$travelDurationDays = $travelStartDate->diff($travelEndDate)->days;
|
||||
|
||||
$eligibleInsurances = array_filter(
|
||||
$insurances,
|
||||
fn (Insurance $insurance) => $this->isInsuranceEligible(
|
||||
$insurance,
|
||||
$participant,
|
||||
$booking,
|
||||
$travelStartDate,
|
||||
$travelEndDate,
|
||||
$bookingDate,
|
||||
$travelPrice,
|
||||
$travelDurationDays
|
||||
)
|
||||
// Generate cache key based on all eligibility criteria
|
||||
// Use spl_object_id() for insurance identification (not $insurance->id):
|
||||
// - Works with test fixtures where IDs may be null
|
||||
// - Negligible overhead (~0.002ms for 20 insurances vs ~50ms saved by caching)
|
||||
// - Insurance objects are stable within a single request (loaded from session)
|
||||
$insuranceHashes = array_map(fn (Insurance $i) => spl_object_id($i), $insurances);
|
||||
$cacheKey = sprintf(
|
||||
'eligible_insurances_%d_%s_%s_%s_%s_%s',
|
||||
$participant->index,
|
||||
number_format(round($travelPrice, 2), 2, '.', ''),
|
||||
$booking->getMode(),
|
||||
$travelStartDate->format('Y-m-d'),
|
||||
$participant->dateOfBirth?->format('Y-m-d') ?? 'no_dob',
|
||||
md5(implode('_', $insuranceHashes))
|
||||
);
|
||||
|
||||
return $this->sortByPrice($eligibleInsurances);
|
||||
return $this->eligibleInsurancesCache[$cacheKey] ??= (function () use ($insurances, $participant, $booking, $travelStartDate, $travelEndDate, $travelPrice) {
|
||||
$bookingDate = Carbon::now()->toDateTimeImmutable();
|
||||
$travelDurationDays = $travelStartDate->diff($travelEndDate)->days;
|
||||
|
||||
$eligibleInsurances = array_filter(
|
||||
$insurances,
|
||||
fn (Insurance $insurance) => $this->isInsuranceEligible(
|
||||
$insurance,
|
||||
$participant,
|
||||
$booking,
|
||||
$travelStartDate,
|
||||
$travelEndDate,
|
||||
$bookingDate,
|
||||
$travelPrice,
|
||||
$travelDurationDays
|
||||
)
|
||||
);
|
||||
|
||||
return $this->sortByPrice($eligibleInsurances);
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\Form\Model\BookingDto;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Spatie\Blink\Blink;
|
||||
|
||||
/**
|
||||
* Service for evaluating participant eligibility for booking.
|
||||
@@ -18,11 +17,13 @@ use Spatie\Blink\Blink;
|
||||
* 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
|
||||
* Results are cached per request using instance-level arrays to avoid redundant calculations
|
||||
* when checking the same participant multiple times.
|
||||
*/
|
||||
class ParticipantEligibilityService
|
||||
{
|
||||
/** @var array<string, bool> Request-scoped cache for participant eligibility */
|
||||
private array $eligibilityCache = [];
|
||||
/**
|
||||
* Checks if a participant is eligible for booking.
|
||||
*
|
||||
@@ -52,7 +53,7 @@ class ParticipantEligibilityService
|
||||
$participantIndex
|
||||
);
|
||||
|
||||
return Blink::global()->once($cacheKey, function () use ($bookingDto, $participantIndex) {
|
||||
return $this->eligibilityCache[$cacheKey] ??= (function () use ($bookingDto, $participantIndex) {
|
||||
$allSkiPasses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true, true);
|
||||
$availableSkiPasses = array_filter(
|
||||
$allSkiPasses,
|
||||
@@ -60,7 +61,7 @@ class ParticipantEligibilityService
|
||||
);
|
||||
|
||||
return !empty($availableSkiPasses);
|
||||
});
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user