25 lines
595 B
PHP
25 lines
595 B
PHP
<?php
|
|
|
|
namespace DistributingCarriers\Infrastructure\Messaging;
|
|
|
|
class CommandBus
|
|
{
|
|
private $handlers = [];
|
|
|
|
public function register(string $commandClass, callable $handler): void
|
|
{
|
|
$this->handlers[$commandClass] = $handler;
|
|
}
|
|
|
|
public function dispatch(object $command): void
|
|
{
|
|
$commandClass = \get_class($command);
|
|
if (!isset($this->handlers[$commandClass])) {
|
|
throw new \Exception("No handler registered for command: $commandClass");
|
|
}
|
|
|
|
$handler = $this->handlers[$commandClass];
|
|
$handler($command);
|
|
}
|
|
}
|