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

SimpleCommandBus   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

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
    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