feat: extensible participant status determination for booking creation
Adds a rule-based system to determine participant status in CREATE payloads. Participants selecting a 'Begleitperson' service now receive status 'O' (Option), all others default to 'F' (Final). - Add ParticipantStatusRuleInterface for defining status rules - Add ParticipantStatusRuleRegistry for priority-based rule evaluation - Add ChaperonServiceStatusRule for Begleitperson detection - Integrate status evaluation into BookingPayloadBuilder
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\BusProNet\Service;
|
||||
|
||||
use App\BusProNet\Service\Contract\ParticipantStatusRuleInterface;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
|
||||
/**
|
||||
* Registry for participant status evaluation rules.
|
||||
*
|
||||
* Manages a collection of status rules and evaluates them in priority order
|
||||
* to determine the appropriate status code for a participant during booking
|
||||
* creation. Returns a default status of 'F' (Final) when no rules match.
|
||||
*/
|
||||
class ParticipantStatusRuleRegistry
|
||||
{
|
||||
private const DEFAULT_STATUS = 'F';
|
||||
|
||||
/**
|
||||
* @var ParticipantStatusRuleInterface[]
|
||||
*/
|
||||
private array $sortedRules;
|
||||
|
||||
/**
|
||||
* @param ParticipantStatusRuleInterface[] $rules The rules to register
|
||||
*/
|
||||
public function __construct(array $rules)
|
||||
{
|
||||
$this->sortedRules = $this->sortRulesByPriority($rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates all rules to determine the participant's status.
|
||||
*
|
||||
* Rules are evaluated in priority order (highest first). The first rule
|
||||
* that matches determines the status. Returns 'F' if no rules match.
|
||||
*
|
||||
* @param ParticipantDto $participant The participant to evaluate
|
||||
*
|
||||
* @return string The determined status code
|
||||
*/
|
||||
public function evaluateStatus(ParticipantDto $participant): string
|
||||
{
|
||||
foreach ($this->sortedRules as $rule) {
|
||||
if (true === $rule->evaluate($participant)) {
|
||||
return $rule->getStatus();
|
||||
}
|
||||
}
|
||||
|
||||
return self::DEFAULT_STATUS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts rules by priority in descending order.
|
||||
*
|
||||
* @param ParticipantStatusRuleInterface[] $rules The rules to sort
|
||||
*
|
||||
* @return ParticipantStatusRuleInterface[] The sorted rules
|
||||
*/
|
||||
private function sortRulesByPriority(array $rules): array
|
||||
{
|
||||
usort($rules, static fn (
|
||||
ParticipantStatusRuleInterface $a,
|
||||
ParticipantStatusRuleInterface $b,
|
||||
): int => $b->getPriority() <=> $a->getPriority());
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user