CommandHandlersMapCodeGenerator::validateMap()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2017 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\CodeGeneration;
7
8
use Gica\CodeAnalysis\MethodListenerDiscovery;
9
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator\AnyPhpClassIsAccepted;
10
use Gica\CodeAnalysis\MethodListenerDiscovery\MapGrouper\GrouperByEvent;
11
use Gica\Cqrs\CodeGeneration\Traits\GroupedByEventTrait;
12
use Gica\Cqrs\Command\CodeAnalysis\AggregateCommandHandlerDetector;
13
14
class CommandHandlersMapCodeGenerator
15
{
16
    use GroupedByEventTrait;
17
18 1
    protected function log($outputFilePath)
19
    {
20 1
        $this->logger->info("Commands map wrote to: $outputFilePath");
21 1
    }
22
23 2
    private function validateMap(array $map)
24
    {
25 2
        foreach ($map as $command => $commandHandlers) {
26 2
            if (count($commandHandlers) > 1) {
27 1
                throw new \Exception(
28 2
                    sprintf("multiple handlers exists for command %s", $command));
29
            }
30
        }
31 1
    }
32
33 2
    protected function discover(\Iterator $files)
34
    {
35 2
        $discoverer = new MethodListenerDiscovery(
36 2
            new AggregateCommandHandlerDetector(),
37 2
            new AnyPhpClassIsAccepted);
38
39 2
        $map = $discoverer->discoverListeners($files);
40
41 2
        $this->validateMap($this->groupMap($map));
42
43 1
        return $map;
44
    }
45
46 2
    private function groupMap(array $map)
47
    {
48 2
        return (new GrouperByEvent())->groupMap($map);
49
    }
50
}