It provides an easy way to wrap command handling in a database transaction.
@TODO The intro should explain what it does.
To use the middleware provided by the library, set up a command bus, if you didn’t already do this:
1 2 3 4
use SimpleBus\Message\Bus\Middleware\MessageBusSupportingMiddleware; $commandBus = new MessageBusSupportingMiddleware(); ...
Make sure to also properly set up a Doctrine connection:
// $connection is an instance of Doctrine\DBAL\Driver\Connection $connection = ...;
It is generally a good idea to wrap command handling in a database
transaction. If you want to do this, add the
WrapsMessageHandlingInTransaction middleware to the command bus.
Provide an instance of the Doctrine
Connection interface that you
want to use.
1 2 3 4 5 6 7 8
use SimpleBus\DoctrineDBALBridge\MessageBus\WrapsMessageHandlingInTransaction; // $connection is an instance of Doctrine\DBAL\Driver\Connection $connection = ...; $transactionalMiddleware = new WrapsMessageHandlingInTransaction($connection); $commandBus->addMiddleware($transactionalMiddleware);
When an exception is thrown, the transaction will be rolled back. If not, the transaction is committed.