Completed
Push — master ( 236db5...b6d10e )
by Pablo
7s
created

PreCommitTool::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.3142
cc 3
eloc 11
nc 3
nop 0
crap 3
1
<?php
2
3
namespace PhpGitHooks\Module\Git\Service;
4
5
use PhpGitHooks\Infrastructure\CommandBus\CommandBus\CommandBus;
6
use PhpGitHooks\Infrastructure\CommandBus\QueryBus\QueryBus;
7
use PhpGitHooks\Module\Composer\Contract\Command\ComposerToolCommand;
8
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinderQuery;
9
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse;
10
use PhpGitHooks\Module\Configuration\Contract\Response\PreCommitResponse;
11
use PhpGitHooks\Module\Files\Contract\Query\PhpFilesExtractorQuery;
12
use PhpGitHooks\Module\Files\Contract\Response\PhpFilesResponse;
13
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse;
14
use PhpGitHooks\Module\Git\Infrastructure\Files\FilesCommittedExtractor;
15
use PhpGitHooks\Module\Git\Infrastructure\OutputWriter\ToolTittleOutputWriter;
16
use PhpGitHooks\Module\JsonLint\Contract\Command\JsonLintToolCommand;
17
use PhpGitHooks\Module\PhpCs\Contract\Command\PhpCsToolCommand;
18
use PhpGitHooks\Module\PhpCsFixer\Contract\Command\PhpCsFixerToolCommand;
19
use PhpGitHooks\Module\PhpLint\Contract\Command\PhpLintToolCommand;
20
use PhpGitHooks\Module\PhpMd\Contract\Command\PhpMdToolCommand;
21
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverageCommand;
22
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand;
23
use PhpGitHooks\Module\PhpUnit\Contract\Command\StrictCoverageCommand;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class PreCommitTool
27
{
28
    const NO_FILES_CHANGED_MESSAGE = '<comment>-\_(ö)_/- No files changed.</comment>';
29
    const TITLE = 'Pre-Commit tool';
30
    /**
31
     * @var OutputInterface
32
     */
33
    private $output;
34
    /**
35
     * @var FilesCommittedExtractor
36
     */
37
    private $filesCommittedExtractor;
38
    /**
39
     * @var CommandBus
40
     */
41
    private $commandBus;
42
    /**
43
     * @var QueryBus
44
     */
45
    private $queryBus;
46
    /**
47
     * @var ToolTittleOutputWriter
48
     */
49
    private $tittleOutputWriter;
50
51
    /**
52
     * PreCommitTool constructor.
53
     *
54
     *
55
     * @param OutputInterface         $output
56
     * @param FilesCommittedExtractor $filesCommittedExtractor
57
     * @param QueryBus                $queryBus
58
     * @param CommandBus              $commandBus
59
     * @param ToolTittleOutputWriter  $tittleOutputWriter
60
     */
61 2
    public function __construct(
62
        OutputInterface $output,
63
        FilesCommittedExtractor $filesCommittedExtractor,
64
        QueryBus $queryBus,
65
        CommandBus $commandBus,
66
        ToolTittleOutputWriter $tittleOutputWriter
67
    ) {
68 2
        $this->filesCommittedExtractor = $filesCommittedExtractor;
69 2
        $this->output = $output;
70 2
        $this->commandBus = $commandBus;
71 2
        $this->queryBus = $queryBus;
72 2
        $this->tittleOutputWriter = $tittleOutputWriter;
73 2
    }
74
75 2
    public function execute()
76
    {
77 2
        $this->tittleOutputWriter->writeTitle(self::TITLE);
78 2
        $committedFiles = $this->filesCommittedExtractor->getFiles();
79
80 2
        if (1 === count($committedFiles)) {
81 1
            $this->output->writeln(static::NO_FILES_CHANGED_MESSAGE);
82
83 1
            return;
84
        }
85
86
        /** @var ConfigurationDataResponse $configurationData */
87 1
        $configurationData = $this->queryBus->handle(new ConfigurationDataFinderQuery());
88 1
        $preCommit = $configurationData->getPreCommit();
89
90 1
        if (true === $preCommit->isPreCommit()) {
91 1
            $this->executeTools($preCommit, $committedFiles);
92 1
        }
93
94 1
        $this->output->writeln(GoodJobLogoResponse::paint($preCommit->getRightMessage()));
95 1
    }
96
97
    /**
98
     * @param PreCommitResponse $preCommitResponse
99
     * @param array             $committedFiles
100
     */
101 1
    private function executeTools(PreCommitResponse $preCommitResponse, array $committedFiles)
102
    {
103 1
        if (true === $preCommitResponse->isComposer()) {
104 1
            $this->commandBus->handle(
105 1
                new ComposerToolCommand($committedFiles, $preCommitResponse->getErrorMessage())
106 1
            );
107 1
        }
108
109 1
        if (true === $preCommitResponse->isJsonLint()) {
110 1
            $this->commandBus->handle(
111 1
                new JsonLintToolCommand($committedFiles, $preCommitResponse->getErrorMessage())
112 1
            );
113 1
        }
114
115 1
        $phpFiles = $this->getPhpFiles($committedFiles);
116
117 1
        if (true === $this->isPhpFiles($phpFiles)) {
118 1
            if (true === $preCommitResponse->isPhpLint()) {
119 1
                $this->commandBus->handle(
120 1
                    new PhpLintToolCommand($phpFiles, $preCommitResponse->getErrorMessage())
121 1
                );
122 1
            }
123
124 1
            $phpCsResponse = $preCommitResponse->getPhpCs();
125
126 1
            if (true === $phpCsResponse->isPhpCs()) {
127 1
                $this->commandBus->handle(
128 1
                    new PhpCsToolCommand(
129 1
                        $phpFiles,
130 1
                        $phpCsResponse->getPhpCsStandard(),
131 1
                        $preCommitResponse->getErrorMessage()
132 1
                    )
133 1
                );
134 1
            }
135
136 1
            $phpCsFixerResponse = $preCommitResponse->getPhpCsFixer();
137
138 1
            if (true === $phpCsFixerResponse->isPhpCsFixer()) {
139 1
                $this->commandBus->handle(
140 1
                    new PhpCsFixerToolCommand(
141 1
                        $phpFiles,
142 1
                        $phpCsFixerResponse->isPhpCsFixerPsr0(),
143 1
                        $phpCsFixerResponse->isPhpCsFixerPsr1(),
144 1
                        $phpCsFixerResponse->isPhpCsFixerPsr2(),
145 1
                        $phpCsFixerResponse->isPhpCsFixerSymfony(),
146 1
                        $preCommitResponse->getErrorMessage()
147 1
                    )
148 1
                );
149 1
            }
150
151 1
            $phpMdResponse = $preCommitResponse->getPhpMd();
152
153 1
            if (true === $phpMdResponse->isPhpMd()) {
154 1
                $this->commandBus->handle(
155 1
                    new PhpMdToolCommand(
156 1
                        $phpFiles,
157 1
                        $phpMdResponse->getPhpMdOptions(),
158 1
                        $preCommitResponse->getErrorMessage()
159 1
                    )
160 1
                );
161 1
            }
162
163 1
            $phpunitResponse = $preCommitResponse->getPhpUnit();
164
165 1 View Code Duplication
            if (true === $phpunitResponse->isPhpunit()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
166 1
                $this->commandBus->handle(
167 1
                    new PhpUnitToolCommand(
168 1
                        $phpunitResponse->isPhpunitRandomMode(),
169 1
                        $phpunitResponse->getPhpunitOptions(),
170 1
                        $preCommitResponse->getErrorMessage()
171 1
                    )
172 1
                );
173
174 1
                $phpunitStrictCoverageResponse = $preCommitResponse->getPhpUnitStrictCoverage();
175
176 1
                if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) {
177 1
                    $this->commandBus->handle(
178 1
                        new StrictCoverageCommand(
179 1
                            $phpunitStrictCoverageResponse->getMinimum(),
180 1
                            $preCommitResponse->getErrorMessage()
181 1
                        )
182 1
                    );
183 1
                }
184
185 1
                $phpunitGuardCoverageResponse = $preCommitResponse->getPhpUnitGuardCoverage();
186
187 1
                if (true === $phpunitGuardCoverageResponse->isEnabled()) {
188 1
                    $this->commandBus->handle(
189 1
                        new GuardCoverageCommand(
190 1
                            $phpunitGuardCoverageResponse->getWarningMessage()
191 1
                        )
192 1
                    );
193 1
                }
194 1
            }
195 1
        }
196 1
    }
197
198
    /**
199
     * @param array $files
200
     *
201
     * @return bool
202
     */
203 1
    private function isPhpFiles(array $files)
204
    {
205 1
        return 0 < $files;
206
    }
207
208
    /**
209
     * @param array $committedFiles
210
     *
211
     * @return array
212
     */
213 1
    private function getPhpFiles(array $committedFiles)
214
    {
215
        /** @var PhpFilesResponse $phpFilesResponse */
216 1
        $phpFilesResponse = $this->queryBus->handle(new PhpFilesExtractorQuery($committedFiles));
217
218 1
        return $phpFilesResponse->getFiles();
219
    }
220
}
221