ldc-cocotte/domain/Policies/CustomerOrderLimitPolicy.php

45 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?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,
2026-06-14 21:32:07 +02:00
) {
}
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);
2026-06-14 21:32:07 +02:00
}
private function getMaxWeeklyAmount(): Money
{
return new Money($this->maxWeeklyAmountCents, $this->currency);
2026-06-14 21:32:07 +02:00
}
}