Passed
Push — master ( cae1f7...69f6f5 )
by Sebastian
02:32
created

IOUtil::mapConfigVerbosity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Console;
11
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * IOUtil class
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/captainhookphp/captainhook
20
 * @since   Class available since Release 0.9.0
21
 */
22
abstract class IOUtil
23
{
24
    /**
25
     * Maps config values to Symfony verbosity values
26
     *
27
     * @var array
28
     */
29
    private static $verbosityMap = [
30
        'quiet'        => OutputInterface::VERBOSITY_QUIET,
31
        'normal'       => OutputInterface::VERBOSITY_NORMAL,
32
        'verbose'      => OutputInterface::VERBOSITY_VERBOSE,
33
        'very-verbose' => OutputInterface::VERBOSITY_VERY_VERBOSE,
34
        'debug'        => OutputInterface::VERBOSITY_DEBUG
35
    ];
36
37
    /**
38
     * Return the Symfony verbosity for a given config value
39
     *
40
     * @param  string $verbosity
41
     * @return int
42
     */
43 9
    public static function mapConfigVerbosity(string $verbosity) : int
44
    {
45 9
        return self::$verbosityMap[strtolower($verbosity)] ?? OutputInterface::VERBOSITY_NORMAL;
46
    }
47
48
    /**
49
     * Convert a user answer to boolean
50
     *
51
     * @param  string $answer
52
     * @return bool
53
     */
54 11
    public static function answerToBool($answer) : bool
55
    {
56 11
        return in_array($answer, ['y', 'yes', 'ok']);
57
    }
58
59
    /**
60
     * Return cli line separator string
61
     *
62
     * @param  int    $length
63
     * @param  string $char
64
     * @return string
65
     */
66 21
    public static function getLineSeparator(int $length = 80, string $char = '=') : string
67
    {
68 21
        return str_repeat($char, $length);
69
    }
70
71
    /**
72
     * Convert everything to a string
73
     *
74
     * @param  array<string>|bool|string|null $arg
75
     * @param  string                         $default
76
     * @return string
77
     */
78 14
    public static function argToString($arg, $default = '') : string
79
    {
80 14
        return is_string($arg) ? $arg : $default;
81
    }
82
83
    /**
84
     * Convert everything to a boolean
85
     *
86
     * @param  array<string>|bool|string|null $arg
87
     * @param  bool                           $default
88
     * @return bool
89
     */
90 4
    public static function argToBool($arg, $default = false) : bool
91
    {
92 4
        return is_bool($arg) ? $arg : $default;
93
    }
94
}
95