Completed
Push — master ( 91a0e7...338b70 )
by Nicolas
18s
created

CommandBus::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace BSP\CommandBus;
5
6
/**
7
 * CommandBus execute Command by handling it with the right CommandHandler.
8
 *
9
 * In order to do so, CommandBus::handlers needs to be filled with handler in its constructor:
10
 *
11
 * Exemple:
12
 * public function __constructor(DomainCommandHandler $domainCommandHandler) {
13
 *     $this->handlers[DomainCommand::class] = $domainCommandHandler;
14
 * }
15
 */
16
abstract class CommandBus
17
{
18
    /** @var CommandHandler[] */
19
    protected $handlers = [];
20
21
    /**
22
     * @throws CommandBusException
23
     */
24 2
    public function execute(Command $command): void
25
    {
26 2
        if (!isset($this->handlers[get_class($command)])) {
27 1
            throw new CommandBusException(sprintf('This Commandbus cannot handle command "%s".', get_class($command)));
28
        }
29
30 1
        $this->handlers[get_class($command)]->handle($command);
31 1
    }
32
}
33