Completed
Push — master ( e9f29b...412a91 )
by Nicolas
19s
created

SimpleCommandBus   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 14 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