Completed
Pull Request — master (#5)
by Nicolas
01:37 queued 44s
created

SimpleCommandBus::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 3
    public function __construct(CommandHandlerMap $commandHandlerMap)
16
    {
17 3
        $this->handlers = $commandHandlerMap->map();
18 3
    }
19
20 3
    public function execute(object $command): void
21
    {
22 3
        $commandClass = \get_class($command);
23
24 3
        if (!isset($this->handlers[$commandClass])) {
25 1
            throw CommandHandlerNotFound::forCommand($commandClass);
26
        }
27
28 2
        if (!is_callable($this->handlers[$commandClass])) {
29 1
            throw new CommandHandlerIsNotCallable();
30
        }
31
32 1
        ($this->handlers[$commandClass])($command);
33 1
    }
34
}
35