CommandSubscriberByMap   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 29
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerForCommand() 0 19 4
getCommandHandlersDefinitions() 0 1 ?
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\Command\CommandSubscriber;
7
8
9
use Gica\Cqrs\Command;
10
use Gica\Cqrs\Command\CommandSubscriber;
11
use Gica\Cqrs\Command\Exception\CommandHandlerNotFound;
12
use Gica\Cqrs\Command\ValueObject\CommandHandlerDescriptor;
13
14
abstract class CommandSubscriberByMap implements CommandSubscriber
15
{
16
    /**
17
     * @param Command $command
18
     * @return CommandHandlerDescriptor
19
     * @throws CommandHandlerNotFound
20
     */
21 2
    public function getHandlerForCommand(Command $command)
22
    {
23 2
        $definitions = $this->getCommandHandlersDefinitions();
24
25 2
        if (isset($definitions[get_class($command)])) {
26 1
            $handlersForCommand = $definitions[get_class($command)];
27
28 1
            if ($handlersForCommand) {
29 1
                foreach ($handlersForCommand as $commandDefinition) {
30
31 1
                    list($aggregateClass, $methodName) = $commandDefinition;
32
33 1
                    return new CommandHandlerDescriptor($aggregateClass, $methodName);
34
                }
35
            }
36
        }
37
38 1
        throw new CommandHandlerNotFound(sprintf("A handler for command %s was not found", get_class($command)));
39
    }
40
41
    abstract protected function getCommandHandlersDefinitions(): array;
42
}