Completed
Branch guard_coverage (f2aaf0)
by Pablo
03:07
created

GuardCoverageTool::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
crap 1
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
            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($outputMessage->getSuccessfulMessage());
79
80 2
        $this->guardWriter->write($this->currentCoverage);
81 2
    }
82
83
    /**
84
     * @return bool
85
     */
86 2
    private function isLowerCurrentCoverage()
87
    {
88 2
        return $this->currentCoverage < $this->previousCoverage;
89
    }
90
}
91