Skip to content

Commit

Permalink
Added messenger stamp NoTransactionStamp
Browse files Browse the repository at this point in the history
  • Loading branch information
tg666 committed Nov 1, 2023
1 parent 82f77f9 commit b465696
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SixtyEightPublishers\ArchitectureBundle\Bridge\Symfony\Messenger\Middleware;

use SixtyEightPublishers\ArchitectureBundle\Bridge\Symfony\Messenger\Stamp\NoTransactionStamp;
use SixtyEightPublishers\ArchitectureBundle\Infrastructure\Common\PersistenceAdapter\PersistenceAdapterInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
Expand All @@ -23,20 +24,22 @@ public function __construct(
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
if ($this->persistenceAdapter->supportsTransactions()) {
$transactionsEnabled = null === $envelope->last(NoTransactionStamp::class);

if ($transactionsEnabled && $this->persistenceAdapter->supportsTransactions()) {
$this->persistenceAdapter->beginTransaction();
}

try {
$envelope = $stack->next()->handle($envelope, $stack);

if ($this->persistenceAdapter->supportsTransactions()) {
if ($transactionsEnabled && $this->persistenceAdapter->supportsTransactions()) {
$this->persistenceAdapter->commitTransaction();
}

return $envelope;
} catch (Throwable $exception) {
if ($this->persistenceAdapter->supportsTransactions()) {
if ($transactionsEnabled && $this->persistenceAdapter->supportsTransactions()) {
$this->persistenceAdapter->rollbackTransaction();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\ArchitectureBundle\Bridge\Symfony\Messenger\Stamp;

use Symfony\Component\Messenger\Stamp\StampInterface;

final class NoTransactionStamp implements StampInterface
{
}

0 comments on commit b465696

Please sign in to comment.