Passed
Pull Request — master (#69)
by Dave
02:21
created

CliConfigReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 62
rs 10
c 2
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOption() 0 14 3
A getStdin() 0 16 3
A getOptionWithDefaultValue() 0 9 2
A getArgument() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\SarbException;
8
use LogicException;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\StreamableInputInterface;
11
12
class CliConfigReader
13
{
14
    public static function getArgument(InputInterface $input, string $argumentName): string
15
    {
16
        $value = $input->getArgument($argumentName);
17
18
        if (is_string($value)) {
19
            return $value;
20
        }
21
22
        // Should never happen. Configured option should only return string or null.
23
        throw new LogicException("Incorrectly configured option [$argumentName]"); // @codeCoverageIgnore
24
    }
25
26
    public static function getOption(InputInterface $input, string $optionName): ?string
27
    {
28
        $value = $input->getOption($optionName);
29
30
        if (null === $value) {
31
            return null;
32
        }
33
34
        if (is_string($value)) {
35
            return $value;
36
        }
37
38
        // Should never happen. Configured option should only return string or null.
39
        throw new LogicException("Incorrectly configured option [$optionName]"); // @codeCoverageIgnore
40
    }
41
42
    public static function getOptionWithDefaultValue(InputInterface $input, string $optionName): string
43
    {
44
        $value = self::getOption($input, $optionName);
45
        if (null === $value) {
46
            // Should never happen. Configured option should always have a default value.
47
            throw new LogicException("Incorrectly configured option. No default value set for option {$optionName}"); // @codeCoverageIgnore
48
        }
49
50
        return $value;
51
    }
52
53
    /**
54
     * Returns STDIN as a string.
55
     *
56
     * @throws SarbException
57
     */
58
    public static function getStdin(InputInterface $input): string
59
    {
60
        // If testing this will get input added by `CommandTester::setInputs` method.
61
        $inputSteam = ($input instanceof StreamableInputInterface) ? $input->getStream() : null;
62
63
        // If nothing from input stream use STDIN instead.
64
        $inputSteam = $inputSteam ?? STDIN;
65
66
        $input = stream_get_contents($inputSteam);
67
68
        if (false === $input) {
69
            // No way of easily testing this
70
            throw new SarbException('Can not read input stream'); // @codeCoverageIgnore
71
        }
72
73
        return $input;
74
    }
75
}
76