Completed
Pull Request — master (#5)
by Nicolas
10:11
created

SimpleCommandBus::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace BSP\CommandBus;
5
6
use BSP\CommandBus\Contracts\CommandBus;
7
use BSP\CommandBus\Contracts\CommandHandlerMap;
8
use BSP\CommandBus\Exception\CommandHandlerIsNotCallable;
9
use BSP\CommandBus\Exception\CommandHandlerNotFound;
10
11
final class SimpleCommandBus implements CommandBus
12
{
13
    private $handlers;
14
15
    public function __construct(CommandHandlerMap $commandHandlerMap)
16
    {
17
        $this->handlers = $commandHandlerMap->map();
18
    }
19
20
    public function execute(object $command): void
21
    {
22
        $commandClass = \get_class($command);
23
24
        if (!isset($this->handlers[$commandClass])) {
25
            throw CommandHandlerNotFound::forCommand($commandClass);
26
        }
27
28
        if (!is_callable($this->handlers[$commandClass])) {
29
            throw new CommandHandlerIsNotCallable();
30
        }
31
32
        ($this->handlers[$commandClass])($command);
33
    }
34
}
35