AbstractCommandHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 2
1
<?php
2
3
/*
4
 * cqrs (https://github.com/phpgears/cqrs).
5
 * CQRS base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/cqrs
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\CQRS;
15
16
use Gears\CQRS\Exception\InvalidCommandException;
17
use Gears\CQRS\Exception\InvalidQueryException;
18
19
abstract class AbstractCommandHandler implements CommandHandler
20
{
21
    /**
22
     * {@inheritdoc}
23
     *
24
     * @throws InvalidQueryException
25
     */
26
    final public function handle(Command $command): void
27
    {
28
        if ($command->getCommandType() !== $this->getSupportedCommandType()) {
29
            throw new InvalidCommandException(\sprintf(
30
                'Command handler "%s" can only handle "%s" commands, "%s" given.',
31
                static::class,
32
                $this->getSupportedCommandType(),
33
                $command->getCommandType()
34
            ));
35
        }
36
37
        $this->handleCommand($command);
38
    }
39
40
    /**
41
     * Get supported command type.
42
     *
43
     * @return string
44
     */
45
    abstract protected function getSupportedCommandType(): string;
46
47
    /**
48
     * Handle command.
49
     *
50
     * @param Command $command
51
     */
52
    abstract protected function handleCommand(Command $command): void;
53
}
54