Completed
Push — master ( 71abfe...238f74 )
by Pablo
05:15
created

GuardCoverageTool::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0011

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 14
cts 15
cp 0.9333
rs 9.2
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
crap 2.0011
1
<?php
2
3
namespace PhpGitHooks\Module\PhpUnit\Service;
4
5
use PhpGitHooks\Module\PhpUnit\Model\GuardCoverageFileReaderInterface;
6
use PhpGitHooks\Module\PhpUnit\Model\GuardCoverageFileWriterInterface;
7
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter;
8
use PhpGitHooks\Module\PhpUnit\Model\StrictCoverageProcessorInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class GuardCoverageTool
12
{
13
    const CHECKING_MESSAGE = 'Checking your current coverage';
14
    /**
15
     * @var OutputInterface
16
     */
17
    private $output;
18
    /**
19
     * @var StrictCoverageProcessorInterface
20
     */
21
    private $strictCoverageProcessor;
22
    /**
23
     * @var GuardCoverageFileReaderInterface
24
     */
25
    private $guardReader;
26
    /**
27
     * @var GuardCoverageFileWriterInterface
28
     */
29
    private $guardWriter;
30
    /**
31
     * @var float;
32
     */
33
    private $currentCoverage;
34
    /**
35
     * @var float
36
     */
37
    private $previousCoverage;
38
39
    /**
40
     * GuardCoverageTool constructor.
41
     *
42
     * @param OutputInterface $output
43
     * @param StrictCoverageProcessorInterface $strictCoverageProcessor
44
     * @param GuardCoverageFileReaderInterface $guardReader
45
     * @param GuardCoverageFileWriterInterface $guardWriter
46
     */
47 2
    public function __construct(
48
        OutputInterface $output,
49
        StrictCoverageProcessorInterface $strictCoverageProcessor,
50
        GuardCoverageFileReaderInterface $guardReader,
51
        GuardCoverageFileWriterInterface $guardWriter
52
    ) {
53 2
        $this->output = $output;
54 2
        $this->strictCoverageProcessor = $strictCoverageProcessor;
55 2
        $this->guardReader = $guardReader;
56 2
        $this->guardWriter = $guardWriter;
57 2
    }
58
59
    /**
60
     * @param string $warningMessage
61
     */
62 2
    public function run($warningMessage)
63
    {
64 2
        $outputMessage = new PreCommitOutputWriter(self::CHECKING_MESSAGE);
65 2
        $this->output->write($outputMessage->getMessage());
66
67 2
        $this->currentCoverage = $this->strictCoverageProcessor->process();
68 2
        $this->previousCoverage = $this->guardReader->read();
69
70
71 2
        true === $this->isLowerCurrentCoverage() ? $this->output->writeln(
72 1
            sprintf(
73 1
                "\n<bg=yellow;options=bold>%s Previous coverage %s, current coverage %s.</>",
74
                $warningMessage,
75 1
                $this->previousCoverage,
76 1
                $this->currentCoverage
77
            )
78 1
        ) : $this->output->writeln(
79 1
            $outputMessage->getSuccessfulMessage() . $this->printGuardCoverage()
80
        );
81
82 2
        $this->guardWriter->write($this->currentCoverage);
83 2
    }
84
85
    /**
86
     * @return bool
87
     */
88 2
    private function isLowerCurrentCoverage()
89
    {
90 2
        return $this->currentCoverage < $this->previousCoverage;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 1
    private function printGuardCoverage()
97
    {
98
        return ' <comment>[' .
99 1
        round($this->currentCoverage, 0) .
100 1
        '% >= ' .
101 1
        round($this->previousCoverage, 0) .
102 1
        '%]</comment>';
103
    }
104
}
105