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

ReadModelsMapCodeGenerator::deleteFileIfExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
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\ReadModelMapperWriter;
12
use Gica\Cqrs\Command\CodeAnalysis\ReadModelEventHandlerDetector;
13
use Gica\Cqrs\ReadModel\ListenerClassValidator\OnlyReadModels;
14
use Gica\FileSystem\FileSystemInterface;
15
use Gica\FileSystem\OperatingSystemFileSystem;
16
use Psr\Log\LoggerInterface;
17
18 View Code Duplication
class ReadModelsMapCodeGenerator
1 ignored issue
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 $readModelMapTemplateClassName,
24
        string $searchDirectory,
25
        string $outputFilePath,
26
        string $outputShortClassName
27
    )
28
    {
29 1
        $fileSystem = $fileSystem ?? new OperatingSystemFileSystem();
30
31 1
        $classInfo = new \ReflectionClass($readModelMapTemplateClassName);
32
33 1
        $this->deleteFileIfExists($fileSystem, $outputFilePath);
34
35 1
        $discoverer = new MethodListenerDiscovery(
36 1
            new ReadModelEventHandlerDetector(),
37 1
            new OnlyReadModels(),
38 1
            new ByConstructorDependencySorter()
39
        );
40
41 1
        $discoverer->discoverListeners($searchDirectory);
42
43 1
        $map = $discoverer->getAllEventsListeners();
44
45 1
        $writer = new ReadModelMapperWriter();
46
47 1
        $template = $fileSystem->fileGetContents($classInfo->getFileName());
48
49 1
        $template = str_replace($classInfo->getShortName() /*ReadModelMapTemplate*/, $outputShortClassName /* ReadModelMap */, $template);
50
51 1
        $template = str_replace('--- This is just a template ---', '--- generated by ' . __FILE__ . ' at ' . date('c') . ' ---', $template);
52
53 1
        $code = $writer->generateAndGetFileContents($map, $template);
54
55 1
        $fileSystem->filePutContents($outputFilePath, $code);
56
57 1
        $fileSystem->fileSetPermissions($outputFilePath, 0777);
58
59 1
        $logger->info("Read models map wrote to: $outputFilePath (searched in $searchDirectory)");
60 1
    }
61
62 1
    private function deleteFileIfExists(FileSystemInterface $fileSystem, string $outputFilePath)
63
    {
64
        try {
65 1
            $fileSystem->fileDelete($outputFilePath);
66 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...
67
        }
68
    }
69
}