Passed
Pull Request — master (#99)
by Sebastian
01:56
created

IOUtil   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 37
c 0
b 0
f 0
dl 0
loc 121
ccs 10
cts 10
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A argToString() 0 3 2
A getLineSeparator() 0 3 1
A argToBool() 0 3 2
A answerToBool() 0 3 1
A mapConfigVerbosity() 0 3 1
A formatHeadline() 0 11 2
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Console;
13
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * IOUtil class
18
 *
19
 * @package CaptainHook
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/captainhookphp/captainhook
22
 * @since   Class available since Release 0.9.0
23
 */
24
abstract class IOUtil
25
{
26
    // @codingStandardsIgnoreStart
27
    /**
28
     * @var string
29
     */
30
    public static $tplError = <<<ERRORTPL
31
32
<error>                                                                                </error>
33
<error>   </error>                      _______________________________________             <error>   </error>
34
<error>   </error>                     /                                       \            <error>   </error>
35
<error>   </error>                     | Avast! <error>Hook execution failed!</error>          |           <error>   </error>
36
<error>   </error>                     |                                        |           <error>   </error>
37
<error>   </error>                    /  Yer git command did not go through!    |           <error>   </error>
38
<error>   </error>                   /_                                         |           <error>   </error>  
39
<error>   </error>           /(o)\     | For further details check the output   |           <error>   </error>
40
<error>   </error>          /  ()/ /)  | or run CaptainHook in verbose or debug |           <error>   </error>
41
<error>   </error>         /.;.))'".)  | mode.                                  |           <error>   </error>
42
<error>   </error>         //////.-'   \_______________________________________/            <error>   </error>
43 9
<error>   </error>=========))=))===()                                                       <error>   </error>
44
<error>   </error>      ///'                                                                <error>   </error>
45 9
<error>   </error>     //                                                                   <error>   </error>
46
<error>   </error>    '                                                                     <error>   </error>
47
<error>                                                                                </error>
48
ERRORTPL;
49
    // @codingStandardsIgnoreEnd
50
51
    /**
52
     * Maps config values to Symfony verbosity values
53
     *
54 11
     * @var array<string, int>
55
     */
56 11
    private static $verbosityMap = [
57
        'quiet'        => OutputInterface::VERBOSITY_QUIET,
58
        'normal'       => OutputInterface::VERBOSITY_NORMAL,
59
        'verbose'      => OutputInterface::VERBOSITY_VERBOSE,
60
        'very-verbose' => OutputInterface::VERBOSITY_VERY_VERBOSE,
61
        'debug'        => OutputInterface::VERBOSITY_DEBUG
62
    ];
63
64
    /**
65
     * Return the Symfony verbosity for a given config value
66 21
     *
67
     * @param  string $verbosity
68 21
     * @return int
69
     */
70
    public static function mapConfigVerbosity(string $verbosity): int
71
    {
72
        return self::$verbosityMap[strtolower($verbosity)] ?? OutputInterface::VERBOSITY_NORMAL;
73
    }
74
75
    /**
76
     * Convert a user answer to boolean
77
     *
78 13
     * @param  string $answer
79
     * @return bool
80 13
     */
81
    public static function answerToBool($answer): bool
82
    {
83
        return in_array($answer, ['y', 'yes', 'ok']);
84
    }
85
86
    /**
87
     * Return cli line separator string
88
     *
89
     * @param  int    $length
90 4
     * @param  string $char
91
     * @return string
92 4
     */
93
    public static function getLineSeparator(int $length = 80, string $char = '='): string
94
    {
95
        return str_repeat($char, $length);
96
    }
97
98
    /**
99
     * Create formatted cli headline
100
     *
101
     * >>>> HEADLINE <<<<
102
     * ==== HEADLINE ====
103
     *
104
     * @param  string $headline
105
     * @param  int    $length
106
     * @param  string $pre
107
     * @param  string $post
108
     * @return string
109
     */
110
    public static function formatHeadline(string $headline, int $length, string $pre = '=', string $post = '='): string
111
    {
112
        $headlineLength = strlen($headline);
113
        if ($headlineLength > ($length - 3)) {
114
            return $headline;
115
        }
116
117
        $prefix = (int) floor(($length - $headlineLength - 2) / 2);
118
        $suffix = (int) ceil(($length - $headlineLength - 2) / 2);
119
120
        return str_repeat($pre, $prefix) . ' ' . $headline . ' ' . str_repeat($post, $suffix);
121
    }
122
123
    /**
124
     * Convert everything to a string
125
     *
126
     * @param  array<string>|bool|string|null $arg
127
     * @param  string                         $default
128
     * @return string
129
     */
130
    public static function argToString($arg, $default = ''): string
131
    {
132
        return is_string($arg) ? $arg : $default;
133
    }
134
135
    /**
136
     * Convert everything to a boolean
137
     *
138
     * @param  array<string>|bool|string|null $arg
139
     * @param  bool                           $default
140
     * @return bool
141
     */
142
    public static function argToBool($arg, $default = false): bool
143
    {
144
        return is_bool($arg) ? $arg : $default;
145
    }
146
}
147