/** * Withdraw Money Command Handle * * Processes business logic for the withdraw money command. * @param WithdrawMoney $command Command object. * @return MoneyWithdrawn Event object. */ protected function handleWithdrawMoney(WithdrawMoney $command) { $data = $command->getPayload(); $withdrawal = new UsDollar($data->withdrawal); if ($withdrawal->getValue() < 0) { throw new MinimumWithdrawalException('You cannot withdraw a negative amount.'); } if ($withdrawal->getValue() > $this->root->balance->getValue()) { throw new InsufficientFundsException(sprintf('Insufficient funds. You tried to withdraw $%s but only have $%s in your account.', $withdrawal->getValue(), $this->balance->getValue())); } return new MoneyWithdrawn($command->getId(), compact('withdrawal')); }
<?php use RayEmitter\Example\BankAccount\CreateAccount; use RayEmitter\Example\BankAccount\DepositMoney; use RayEmitter\Example\BankAccount\GetAccountBalance; use RayEmitter\Example\BankAccount\WithdrawMoney; $data = ['owner' => 'Owner Name', 'deposit' => 64]; $command = new CreateAccount($data); $command->run(); $account = '12345-45771...'; $data = ['deposit' => 10]; $command = new DepositMoney($account, $data, 0); $command->run(); $data = ['withdrawl' => 306]; $command = new WithdrawMoney($account, $data, 1); $command->setExpectedSequence(1)->run(); $query = new GetAccountBalance($account); echo $query->run(); // As of sequence 1, return value should be 360.