Specification pattern
Добавлено: 2018.07.28, 14:39
Всем привет!
Есть такой хендлер
Напрягает куча if'ов. Есть мнение, что здесь применим паттерн Specification для валидации бизнес правил. Но пока не могу сообразить как сделать, т.к. все проверяемые объекты разных типов, а метод isSatisfiedBy() y спецификации по идее должен принимать только один параметр одно типа. Как быть в этом случае? Возможно я ошибаюсь о приминении здесь Specification?
Есть такой хендлер
Код: Выделить всё
public function handle(RequestWithdrawTransactionCommand $command): WithdrawTransaction
{
$currency = Currency::get($command->getCurrency());
$user = $this->userRepository->get($command->getUserId());
$currencyConfig = $this->currencyConfigRepository->getByCurrency($currency);
$amount = Amount::fromString($command->getAmount());
if ($this->withdrawChecker->isReachLimit($command->getUserId(), $amount, $currency)) {
throw new LimitException('Withdraw limit');
}
if ($currencyConfig === null) {
throw new \DomainException('Currency config not found');
}
if ($amount->isNegative() || $amount->isZero()) {
throw new \DomainException('Wrong amount value');
}
if (!$currencyConfig->isWithdrawEnabled()) {
throw new \DomainException('Withdraw disabled');
}
$currencyConfig->getWithdrawLimit()->assertAmount($amount);
$transaction = WithdrawTransaction::requestWithdraw(
$user,
$command->getAddress(),
$amount,
$currency,
$currencyConfig->getWithdrawFee(),
$this->confirmationCodeGenerator
);
$this->transactionRepository->add($transaction);
return $transaction;
}