Completed
Push — master ( f582f6...122e91 )
by Constantin
06:16
created

CommandHandlersMapCodeGenerator::groupMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\ClassSorter\ByConstructorDependencySorter;
10
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator\AnyPhpClassIsAccepted;
11
use Gica\CodeAnalysis\MethodListenerDiscovery\MapCodeGenerator\GroupedByEventMapCodeGenerator;
12
use Gica\CodeAnalysis\MethodListenerDiscovery\MapGrouper\GrouperByEvent;
13
use Gica\Cqrs\Command\CodeAnalysis\AggregateCommandHandlerDetector;
14
use Gica\FileSystem\FileSystemInterface;
15
use Psr\Log\LoggerInterface;
16
17
class CommandHandlersMapCodeGenerator
18
{
19 2 View Code Duplication
    public function generate(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
        LoggerInterface $logger,
21
        FileSystemInterface $fileSystem = null,
22
        string $commandSubscriberTemplateClassName,
23
        string $searchDirectory,
24
        string $outputFilePath,
25
        string $outputShortClassName = 'CommandHandlerSubscriber')
26
    {
27 2
        (new CodeGenerator(new GroupedByEventMapCodeGenerator(), $fileSystem))
28 2
            ->discoverAndPutContents(
29 2
                $this->discover($searchDirectory),
30
                $commandSubscriberTemplateClassName,
31
                $outputFilePath,
32
                $outputShortClassName
33
            );
34
35 1
        $logger->info("Commands map wrote to: $outputFilePath (searched in $searchDirectory)");
36 1
    }
37
38 2
    private function validateMap(array $map)
39
    {
40 2
        foreach ($map as $command => $commandHandlers) {
41 2
            if (count($commandHandlers) > 1) {
42 1
                throw new \Exception(
43 2
                    sprintf("multiple handlers exists for command %s", $command));
44
            }
45
        }
46 1
    }
47
48 2
    private function discover(string $searchDirectory)
49
    {
50 2
        $discoverer = new MethodListenerDiscovery(
51 2
            new AggregateCommandHandlerDetector(),
52 2
            new AnyPhpClassIsAccepted,
53 2
            new ByConstructorDependencySorter());
54
55 2
        $map = $discoverer->discoverListeners($searchDirectory);
56
57 2
        $this->validateMap($this->groupMap($map));
58
59 1
        return $map;
60
    }
61
62 2
    private function groupMap(array $map)
63
    {
64 2
        return (new GrouperByEvent())->groupMap($map);
65
    }
66
}