Skip to content

Commit

Permalink
Added command TransactionalCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
tg666 committed Jan 8, 2024
1 parent f5a07e7 commit 9ba7e0e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\ArchitectureBundle\Application\Command;

use SixtyEightPublishers\ArchitectureBundle\Command\CommandInterface;
use Symfony\Component\Messenger\Stamp\StampInterface;

final class TransactionalCommand implements CommandInterface
{
/**
* @param array<array{0: CommandInterface, 1: array<StampInterface>}> $commands
*/
private function __construct(
public readonly array $commands,
) {}

public static function create(): self
{
return new self([]);
}

/**
* @param array<StampInterface> $stamps
*/
public function with(CommandInterface $command, array $stamps = []): self
{
$commands = $this->commands;
$commands[] = [$command, $stamps];

return new self($commands);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\ArchitectureBundle\Application\CommandHandler;

use SixtyEightPublishers\ArchitectureBundle\Application\Command\TransactionalCommand;
use SixtyEightPublishers\ArchitectureBundle\Bus\CommandBusInterface;
use SixtyEightPublishers\ArchitectureBundle\Command\CommandHandlerInterface;

/**
* Transactions are internally handled via StoreTransactionMiddleware
*/
final class TransactionalCommandHandler implements CommandHandlerInterface
{
public function __construct(
private readonly CommandBusInterface $commandBus,
) {}

public function __invoke(TransactionalCommand $command): void
{
foreach ($command->commands as [$command, $stamps]) {
$this->commandBus->dispatch($command, $stamps);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
-
autowired: no
factory: SixtyEightPublishers\ArchitectureBundle\Application\CommandHandler\TransactionalCommandHandler

0 comments on commit 9ba7e0e

Please sign in to comment.