59 lines
1.4 KiB
PHP
59 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\BusProNet\Service;
|
|
|
|
use App\BusProNet\Service\Contract\BookingStatusRuleInterface;
|
|
use App\Form\Model\BookingDto;
|
|
|
|
/**
|
|
* Registry for booking status evaluation rules.
|
|
*
|
|
* Evaluates registered rules in priority order and returns the first matching
|
|
* status. Falls back to 'F' when no rule applies.
|
|
*/
|
|
class BookingStatusRuleRegistry
|
|
{
|
|
private const DEFAULT_STATUS = 'F';
|
|
|
|
/**
|
|
* @var BookingStatusRuleInterface[]
|
|
*/
|
|
private array $sortedRules;
|
|
|
|
/**
|
|
* @param BookingStatusRuleInterface[] $rules
|
|
*/
|
|
public function __construct(array $rules)
|
|
{
|
|
$this->sortedRules = $this->sortRulesByPriority($rules);
|
|
}
|
|
|
|
public function evaluateStatus(BookingDto $bookingDto): string
|
|
{
|
|
foreach ($this->sortedRules as $rule) {
|
|
if (true === $rule->evaluate($bookingDto)) {
|
|
return $rule->getStatus();
|
|
}
|
|
}
|
|
|
|
return self::DEFAULT_STATUS;
|
|
}
|
|
|
|
/**
|
|
* @param BookingStatusRuleInterface[] $rules
|
|
*
|
|
* @return BookingStatusRuleInterface[]
|
|
*/
|
|
private function sortRulesByPriority(array $rules): array
|
|
{
|
|
usort($rules, static fn (
|
|
BookingStatusRuleInterface $a,
|
|
BookingStatusRuleInterface $b,
|
|
): int => $b->getPriority() <=> $a->getPriority());
|
|
|
|
return $rules;
|
|
}
|
|
}
|