-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCommandBus.php
43 lines (35 loc) · 1.13 KB
/
SimpleCommandBus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace RockujemyWpExt\Utilities\SimpleCommandBus;
use RockujemyWpExt\Validation\Exception\ValidationException;
final class SimpleCommandBus
{
private $handlers = [];
public function registerHandler(string $commandClass, $handler): void
{
if (!is_object($handler)) {
throw new \RuntimeException(
sprintf('Handler has to be "object", "%s" given.', \gettype($handler))
);
}
if (!method_exists($handler, 'handle')) {
throw new \RuntimeException(
sprintf('Handler doesn\'t have method "handle"')
);
}
$this->handlers[$commandClass] = $handler;
}
/**
* @throws \Exception
* @throws ValidationException
*/
public function handle(object $command): void
{
if (!array_key_exists(get_class($command), $this->handlers)) {
throw new \RuntimeException(
sprintf('Command "%s" doesn\'t have registered handler', get_class($command))
);
}
$this->handlers[get_class($command)]->handle($command);
}
}