Passed
Pull Request — master (#379)
by Fabien
02:31
created

MaxScoreChecker::isOverThreshold()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Churn\Command\Helper;
6
7
use Churn\Result\ResultReporter;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\ConsoleOutputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * @internal
14
 */
15
final class MaxScoreChecker
16
{
17
    /**
18
     * @var float|null
19
     */
20
    private $maxScoreThreshold;
21
22
    /**
23
     * @param float|null $maxScoreThreshold The max score threshold.
24
     */
25
    public function __construct(?float $maxScoreThreshold)
26
    {
27
        $this->maxScoreThreshold = $maxScoreThreshold;
28
    }
29
30
    /**
31
     * @param InputInterface $input Input.
32
     * @param OutputInterface $output Output.
33
     * @param ResultReporter $report The report containing the scores.
34
     */
35
    public function isOverThreshold(InputInterface $input, OutputInterface $output, ResultReporter $report): bool
36
    {
37
        $maxScore = $report->getMaxScore();
38
39
        if (null === $this->maxScoreThreshold || null === $maxScore || $maxScore <= $this->maxScoreThreshold) {
40
            return false;
41
        }
42
43
        $this->printErrorMessage($input, $output);
44
45
        return true;
46
    }
47
48
    /**
49
     * @param InputInterface $input Input.
50
     * @param OutputInterface $output Output.
51
     */
52
    private function printErrorMessage(InputInterface $input, OutputInterface $output): void
53
    {
54
        if ('text' !== $input->getOption('format') && '' === (string) $input->getOption('output')) {
55
            return;
56
        }
57
58
        $output = $output instanceof ConsoleOutputInterface
59
            ? $output->getErrorOutput()
60
            : $output;
61
        $output->writeln('<error>Max score is over the threshold</>');
62
    }
63
}
64