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

GuardCoverageTool   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 4
c 3
b 1
f 1
lcom 1
cbo 5
dl 0
loc 80
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A run() 0 20 2
A isLowerCurrentCoverage() 0 4 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