AbstractCommandHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 45
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
getSupportedCommandClassName() 0 1 ?
A handle() 0 14 2
execute() 0 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