Skip to content

Commit

Permalink
Update Transaction model and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-b committed Jan 18, 2023
1 parent a9c9c25 commit d58bcfb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/TransactionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -122,8 +122,8 @@ function () {
function () {
echo "|Commit";
},
function () {
echo "Rollback";
function (\Throwable $throwable) {
echo "Rollback ".$throwable->getMessage();
},
);

Expand Down

0 comments on commit d58bcfb

Please sign in to comment.