Created
November 29, 2014 09:46
-
-
Save immutef/bd574f7308efbade7b56 to your computer and use it in GitHub Desktop.
ValidatingCommandBus for Broadway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<services> | |
<!-- ... --> | |
<service id="broadway.command_handling.validating_command_bus" class="App\Bundle\Broadway\CommandHandling\ValidatingCommandBus" public="false"> | |
<argument type="service" id="broadway.command_handling.event_dispatching_command_bus"/> | |
<argument type="service" id="validator"/> | |
</service> | |
<service id="broadway.command_handling.command_bus" alias="broadway.command_handling.validating_command_bus"/> | |
</services> | |
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Bundle\Broadway\CommandHandling; | |
use App\Bundle\Exception\ValidationFailedException; | |
use Broadway\CommandHandling\CommandBusInterface; | |
use Broadway\CommandHandling\CommandHandlerInterface; | |
use Symfony\Component\Validator\Validator\ValidatorInterface; | |
class ValidatingCommandBus implements CommandBusInterface | |
{ | |
private $commandBus; | |
private $validator; | |
public function __construct(CommandBusInterface $commandBus, ValidatorInterface $validator) | |
{ | |
$this->commandBus = $commandBus; | |
$this->validator = $validator; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function dispatch($command) | |
{ | |
$constraintViolationList = $this->validator->validate($command); | |
if (count($constraintViolationList) > 0) { | |
throw new ValidationFailedException($constraintViolationList); | |
} | |
$this->commandBus->dispatch($command); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function subscribe(CommandHandlerInterface $handler) | |
{ | |
$this->commandBus->subscribe($handler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment