Skip to content

Command Bus

Pau F. Grau edited this page Jan 10, 2021 · 2 revisions

🎯 Command Bus

We use the command bus for all use cases need be executed with transaction

We use the follow open source packages:

If you want add a new command you need do the follow steps:

Example of use:

CLI:

     ...
     
     $command = new ImportAuthorsCommand($data);

     $this->commandBus->dispatch($command);

     ...

Command Handler

    ...
    public function __invoke(ImportAuthorsCommand $command): void
    {
        $collection = [];
        foreach ($command->data() as $item) {
            Assert::keyExists($item, 'author');
            $author = Author::create($item['author']);

            if (isset($collection[$author->id()])) {
                continue;
            }

            $collection[$author->id()] = $author->id();

            $this->eventBus->publish(...$author->pullDomainEvents());
        }
    }
    ...
Clone this wiki locally