Added CustomerOrderLimitPolicy to manage order limits based on configuration values. Updated tests to reflect changes in constructor parameters.
44 lines
1.2 KiB
PHP
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);
|
|
}
|
|
}
|