Completed
Push — master ( 8a4be1...fe7d42 )
by Constantin
06:22
created

CommandValidatorsMapCodeGenerator::generate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 24

Duplication

Lines 40
Ratio 100 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
dl 40
loc 40
ccs 19
cts 19
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 6
crap 1
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2017 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\CodeGeneration;
7
8
9
use Gica\CodeAnalysis\MethodListenerDiscovery;
10
use Gica\CodeAnalysis\MethodListenerDiscovery\ClassSorter\ByConstructorDependencySorter;
11
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator\AnyPhpClassIsAccepted;
12
use Gica\CodeAnalysis\MethodListenerDiscovery\MethodListenerMapperWriter;
13
use Gica\Cqrs\Command\CodeAnalysis\AggregateCommandValidatorDetector;
14
use Gica\FileSystem\FileSystemInterface;
15
use Gica\FileSystem\OperatingSystemFileSystem;
16
use Psr\Log\LoggerInterface;
17
18 View Code Duplication
class CommandValidatorsMapCodeGenerator
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20 1
    public function generate(
21
        LoggerInterface $logger,
22
        FileSystemInterface $fileSystem = null,
23
        string $commandValidatorSubscriberTemplateClassName,
24
        string $searchDirectory,
25
        string $outputFilePath,
26
        string $outputShortClassName
27
    )
28
    {
29 1
        $fileSystem = $fileSystem ?? new OperatingSystemFileSystem();
30
31 1
        $classInfo = new \ReflectionClass($commandValidatorSubscriberTemplateClassName);
32
33 1
        $this->deleteFileIfExists($fileSystem, $outputFilePath);
34
35 1
        $discoverer = new MethodListenerDiscovery(
36 1
            new AggregateCommandValidatorDetector(),
37 1
            new AnyPhpClassIsAccepted,
38 1
            new ByConstructorDependencySorter());
39
40 1
        $discoverer->discoverListeners($searchDirectory);
41
42 1
        $map = $discoverer->getEventToListenerMap();
43
44 1
        $writer = new MethodListenerMapperWriter();
45
46 1
        $template = $fileSystem->fileGetContents($classInfo->getFileName());
47
48 1
        $template = str_replace($classInfo->getShortName() /*CommandValidatorSubscriberTemplate*/, $outputShortClassName /*CommandValidatorSubscriber*/, $template);
49
50 1
        $template = str_replace('--- This is just a template ---', '--- generated by ' . __FILE__ . ' at ' . date('c') . ' ---', $template);
51
52 1
        $code = $writer->generateAndGetFileContents($map, $template);
53
54 1
        $fileSystem->filePutContents($outputFilePath, $code);
55
56 1
        $fileSystem->fileSetPermissions($outputFilePath, 0777);
57
58 1
        $logger->info("Commands validators wrote to: $outputFilePath");
59 1
    }
60
61 1
    private function deleteFileIfExists(FileSystemInterface $fileSystem, string $outputFilePath)
62
    {
63
        try {
64 1
            $fileSystem->fileDelete($outputFilePath);
65 1
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
66
        }
67
    }
68
}