AbstractCommandHandler::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace PhpDDD\Command\Handler;
3
4
use InvalidArgumentException;
5
use PhpDDD\Command\CommandInterface;
6
7
/**
8
 * generic implementation of CommandHandlerInterface
9
 */
10
abstract class AbstractCommandHandler implements CommandHandlerInterface
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function supports($commandClassName)
16
    {
17
        return $this->getSupportedCommandClassName() === $commandClassName;
18
    }
19
20
    /**
21
     * @return string the fully qualified name of the command class that this handler can accept.
22
     */
23
    abstract public function getSupportedCommandClassName();
24
25
    /**
26
     * @param CommandInterface $command
27
     *
28
     * @return mixed
29
     * @throws InvalidArgumentException
30
     */
31
    public function handle(CommandInterface $command)
32
    {
33
        if (!$this->supports(get_class($command))) {
34
            throw new InvalidArgumentException(
35
                sprintf(
36
                    'The command must be an instance of "%s". "%s" given.',
37
                    $this->getSupportedCommandClassName(),
38
                    get_class($command)
39
                )
40
            );
41
        }
42
43
        return $this->execute($command);
44
    }
45
46
    /**
47
     * Handle the command assuming that this command can be handled by the current handler.
48
     *
49
     * @param CommandInterface $command
50
     *
51
     * @return mixed
52
     */
53
    abstract protected function execute(CommandInterface $command);
54
}
55