ldc-cocotte/domain/Policies/CustomerOrderLimitPolicy.php
Jérémy Munsch c4d9c84408
feat(policy): implement customer order limit policy with config values
Added CustomerOrderLimitPolicy to manage order limits based on configuration values.
Updated tests to reflect changes in constructor parameters.
2026-11-15 19:39:35 +01:00

44 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Domain\Policies;
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
use Money\Currency;
use Money\Money;
class CustomerOrderLimitPolicy
{
public function __construct(
private readonly int $maxOrderAmountCents,
private readonly int $maxWeeklyAmountCents,
private readonly Currency $currency,
) {
}
public function assertCanPlaceOrder(Money $basketTotal): void
{
if ($basketTotal->greaterThan($this->getMaxOrderAmount())) {
throw new OrderLimitExceededException();
}
}
public function assertCanPlaceWeeklyOrder(Money $basketTotal, Money $weeklyTotal): void
{
if ($weeklyTotal->add($basketTotal)->greaterThan($this->getMaxWeeklyAmount())) {
throw new WeeklyLimitExceededException();
}
}
private function getMaxOrderAmount(): Money
{
return new Money($this->maxOrderAmountCents, $this->currency);
}
private function getMaxWeeklyAmount(): Money
{
return new Money($this->maxWeeklyAmountCents, $this->currency);
}
}