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

ReadModelsMapCodeGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 52
loc 52
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 41 41 1
A deleteFileIfExists() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}