У меня есть бизнес-правило, что каждая транзакция имеет комиссию 10%, если общий объем ежедневных транзакций пользователей не превышает 100, тогда комиссия составляет 5%.
Учитывая, что реализация калькулятора комиссий выглядит следующим образом:
class PercentageFeeCalculator implements FeeCalculator
{
private $feePercent;
public function __construct(float $feePercent)
{
$this->feePercent = $feePercent;
}
public function calculate(Transaction $transaction): Money
{
return $transaction->getAmount()->multiply($this->feePercent)->divide(100);
}
}
Было бы нормально, согласно DDD, реализовать это бизнес-решение на Фабрике или это должно быть сделано другим способом?
class FeeCalculatorFactory
{
const DISCOUNT_ELIGIBILITY_THRESHOLD = 100;
private $transactionRepository;
public function __construct(TransactionRepository $transactionRepository)
{
$transactionRepository = $transactionRepository;
}
public function create(UserId $userId)
{
if ($this->isEligibleForVolumeDiscount($userId)) {
return new PercentageFeeCalculator(5.0);
}
return new PercentageFeeCalculator(10.0);
}
private function isEligibleForVolumeDiscount(UserId $userId): bool
{
$dailyVolume = $this->transactionRepository->getDailyVolumeByUser($userId);
if ($dailyVolume > self::DISCOUNT_ELIGIBILITY_THRESHOLD) {
return true;
}
return false;
}
}