diff --git a/README.md b/README.md index e1f9f1f..6285fe8 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,18 @@ $subscriber->addListener(MessageFailedEvent::class, function (MessageFailedEvent ### Transaction +Transaction Middleware accepts three function arguments, each for every stage of the transaction: begin, commit, and rollback. +Going with this approach allows you to use any ORM you prefer or even using the native \PDO object to interact with your persistence layer. +```php +$pdo = new \PDO('{connection_dsn}') + +$transaction = new \SasaB\MessageBus\Middleware\TransactionMiddleware( + fn(): void => $pdo->beginTransaction();, + fn(): void => $pdo->commit();, + fn(\Throwable $error): void => $pdo->rollBack();, +); +``` + ### Response Types Library wraps the Handler return values into __Response value objects__ to provide a consistent API and so that you can @@ -139,6 +151,8 @@ All Response value objects extend the `SasaB\MessageBus\Response` abstract class 2. `SasaB\MessageBus\Response\Delegated` which wraps objects and delegates calls to properties and methods to the underlying object 3. `SasaB\MessageBus\Response\Collection` and `SasaB\MessageBus\Response\Map` which wrap number indexed arrays (lists) and string indexed arrays (maps) and implement `\Countable`, `\ArrayAccess` and `\IteratorAggregate` interfaces +You can also add your own custom Response value objects by extending the abstract class `SasaB\MessageBus\Response` and returning them in the appropriate handler. + ## Getting Started ### Stand-alone usage diff --git a/src/Middleware/TransactionMiddleware.php b/src/Middleware/TransactionMiddleware.php index 84249bf..4f166ad 100644 --- a/src/Middleware/TransactionMiddleware.php +++ b/src/Middleware/TransactionMiddleware.php @@ -29,7 +29,7 @@ public function __invoke(Message $message, \Closure $next): mixed try { $result = $next($message); } catch (\Exception $e) { - $this->rollback->call($this); + $this->rollback->call($this, $e); throw MiddlewareException::handler(handler: __CLASS__, error: $e); } $this->commit->call($this); diff --git a/tests/Unit/MiddlewareTest.php b/tests/Unit/MiddlewareTest.php index 1f64b50..19e2605 100644 --- a/tests/Unit/MiddlewareTest.php +++ b/tests/Unit/MiddlewareTest.php @@ -112,7 +112,7 @@ function () { public function test_it_rollbacks_transaction_on_error(): void { $this->expectOutputString( - "Begin|Rollback" + "Begin|Rollback Command Fails" ); $transactionMiddleware = new TransactionMiddleware( @@ -122,8 +122,8 @@ function () { function () { echo "|Commit"; }, - function () { - echo "Rollback"; + function (\Throwable $throwable) { + echo "Rollback ".$throwable->getMessage(); }, );