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

CommandValidatorsMapCodeGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
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 51
loc 51
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 40 40 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\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
}