ldc-cocotte/domain/Policies/CustomerOrderLimitPolicy.php
2026-10-27 09:58:12 +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 = 20000,
private readonly int $maxWeeklyAmountCents = 20000,
private readonly ?Currency $currency = null
) {
}
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 ?? new Currency('EUR'));
}
private function getMaxWeeklyAmount(): Money
{
return new Money($this->maxWeeklyAmountCents, $this->currency ?? new Currency('EUR'));
}
}