-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/ArchitectureBundle/Application/Command/TransactionalCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/ArchitectureBundle/Application/CommandHandler/TransactionalCommandHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...reBundle/Bridge/Nette/DI/definitions/architecture_bundle/application.command_handler.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
- | ||
autowired: no | ||
factory: SixtyEightPublishers\ArchitectureBundle\Application\CommandHandler\TransactionalCommandHandler |