SimpleCommandBus::execute()   A
last analyzed

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\Command;
7
use BSP\CommandBus\Contracts\CommandBus;
8
use BSP\CommandBus\Contracts\CommandHandlerMap;
9
use BSP\CommandBus\Exception\CommandHandlerIsNotCallable;
10
use BSP\CommandBus\Exception\CommandHandlerNotFound;
11
12
final class SimpleCommandBus implements CommandBus
13
{
14
    private $handlers;
15
16 3
    public function __construct(CommandHandlerMap $commandHandlerMap)
17
    {
18 3
        $this->handlers = $commandHandlerMap->map();
19 3
    }
20
21 3
    public function execute(Command $command): void
22
    {
23 3
        $commandClass = \get_class($command);
24
25 3
        if (false === isset($this->handlers[$commandClass])) {
26 1
            throw CommandHandlerNotFound::forCommand($commandClass);
27
        }
28
29 2
        if (false === is_callable($this->handlers[$commandClass])) {
30 1
            throw new CommandHandlerIsNotCallable();
31
        }
32
33 1
        ($this->handlers[$commandClass])($command);
34 1
    }
35
}
36