| @@ 12-44 (lines=33) @@ | ||
| 9 | use Gica\Cqrs\Command\CommandValidator; |
|
| 10 | use Gica\Cqrs\Command\Exception\CommandValidationFailed; |
|
| 11 | ||
| 12 | class CommandDispatcherWithValidator implements CommandDispatcher |
|
| 13 | { |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @var CommandDispatcher |
|
| 17 | */ |
|
| 18 | private $commandDispatcher; |
|
| 19 | /** |
|
| 20 | * @var CommandValidator |
|
| 21 | */ |
|
| 22 | private $commandValidator; |
|
| 23 | ||
| 24 | public function __construct( |
|
| 25 | CommandDispatcher $commandDispatcher, |
|
| 26 | CommandValidator $commandValidator |
|
| 27 | ||
| 28 | ) |
|
| 29 | { |
|
| 30 | $this->commandDispatcher = $commandDispatcher; |
|
| 31 | $this->commandValidator = $commandValidator; |
|
| 32 | } |
|
| 33 | ||
| 34 | public function dispatchCommand(Command $command, $commandMetadata = null) |
|
| 35 | { |
|
| 36 | $errors = $this->commandValidator->validateCommand($command); |
|
| 37 | ||
| 38 | if (!empty($errors)) { |
|
| 39 | throw new CommandValidationFailed($errors); |
|
| 40 | } |
|
| 41 | ||
| 42 | $this->commandDispatcher->dispatchCommand($command); |
|
| 43 | } |
|
| 44 | } |
|
| @@ 13-45 (lines=33) @@ | ||
| 10 | use Gica\Cqrs\Command\CommandTester; |
|
| 11 | use Gica\Cqrs\Command\CommandValidator; |
|
| 12 | ||
| 13 | class CommandTesterWithValidator implements CommandTester |
|
| 14 | { |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @var CommandTester |
|
| 18 | */ |
|
| 19 | private $commandTester; |
|
| 20 | /** |
|
| 21 | * @var CommandValidator |
|
| 22 | */ |
|
| 23 | private $commandValidator; |
|
| 24 | ||
| 25 | public function __construct( |
|
| 26 | CommandTester $commandTester, |
|
| 27 | CommandValidator $commandValidator |
|
| 28 | ||
| 29 | ) |
|
| 30 | { |
|
| 31 | $this->commandTester = $commandTester; |
|
| 32 | $this->commandValidator = $commandValidator; |
|
| 33 | } |
|
| 34 | ||
| 35 | public function canExecuteCommand(Command $command): bool |
|
| 36 | { |
|
| 37 | $errors = $this->commandValidator->validateCommand($command); |
|
| 38 | ||
| 39 | if (!empty($errors)) { |
|
| 40 | return false; |
|
| 41 | } |
|
| 42 | ||
| 43 | return $this->commandTester->canExecuteCommand($command); |
|
| 44 | } |
|
| 45 | } |
|