commandHandlerNotFound()   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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PhpDDD\DomainDrivenDesign\Exception;
4
5
use PhpDDD\DomainDrivenDesign\Command\AbstractCommand;
6
use PhpDDD\DomainDrivenDesign\Command\CommandHandlerInterface;
7
8
/**
9
 *
10
 */
11
final class CommandDispatcherException extends \LogicException implements ExceptionInterface
12
{
13
    /**
14
     * @param AbstractCommand $command
15
     *
16
     * @return CommandDispatcherException
17
     */
18
    public static function commandHandlerNotFound(AbstractCommand $command)
19
    {
20
        return new self(sprintf('There is no known handler for the command type %s.', get_class($command)));
21
    }
22
23
    /**
24
     * @param AbstractCommand           $command
25
     * @param CommandHandlerInterface[] $commandHandlers
26
     *
27
     * @return CommandDispatcherException
28
     */
29
    public static function tooManyCommandHandler(AbstractCommand $command, array $commandHandlers)
30
    {
31
        $handlerClassNames = array_map(
32
            function (CommandHandlerInterface $commandHandler) {
33
                return get_class($commandHandler);
34
            },
35
            $commandHandlers
36
        );
37
38
        return new self(
39
            sprintf(
40
                'There are too many command handler for the command type %s. Candidates are %s.',
41
                get_class($command),
42
                implode(', ', $handlerClassNames)
43
            )
44
        );
45
    }
46
47
    /**
48
     * @param string                  $commandClassName
49
     * @param CommandHandlerInterface $previous
50
     * @param CommandHandlerInterface $current
51
     *
52
     * @return CommandDispatcherException
53
     */
54
    public static function commandHandlerAlreadyDefinedForCommand(
55
        $commandClassName,
56
        CommandHandlerInterface $previous,
57
        CommandHandlerInterface $current
58
    ) {
59
        $message = 'Trying to add handler %3$s handling command %1$s which is already handled by %2$s.';
60
        if (get_class($previous) === get_class($current)) {
61
            $message = 'Trying to add multiple handling method for the command %1$s in the handler %2$s';
62
        }
63
64
        return new self(sprintf($message, $commandClassName, get_class($previous), get_class($current)));
65
    }
66
}
67