Passed
Push — master ( 3d076e...fba7a8 )
by Timm
02:10
created

CmdRunner::setCmd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stefaminator\Cli;
5
6
7
abstract class CmdRunner {
8
9
    /**
10
     * @var Cmd
11
     */
12
    private $cmd;
13
14
    /**
15
     * CmdRunner constructor.
16
     * @param Cmd $cmd
17
     */
18 21
    public function __construct(Cmd $cmd = null) {
19 21
        if ($cmd !== null) {
20 4
            $this->cmd = $cmd;
21 4
            return;
22
        }
23 21
        $this->cmd = new Cmd('__root');
24 21
    }
25
26
    /**
27
     * @param Cmd $cmd
28
     */
29 21
    public function init(Cmd $cmd): void {
30 21
        $this->cmd = $cmd;
31 21
    }
32
33
    /**
34
     * @return Cmd
35
     */
36 5
    public function getCmd(): Cmd {
37 5
        return $this->cmd;
38
    }
39
40
    /**
41
     * Run the cmd
42
     */
43
    abstract public function run(): void;
44
45
    /**
46
     * Overwrite this method for extended help
47
     */
48 3
    public function help(): void {
49
50 3
    }
51
52
53
    public const EOL = "\n";
54
55
56 6
    public static function eol(): void {
57 6
        echo self::EOL;
58 6
    }
59
60 6
    public static function echo(string $str, ?string $foreground_color = null): void {
61
62 6
        $lines = preg_split("/\r\n|\n|\r/", $str);
63
64 6
        $output = [];
65 6
        foreach ($lines as $line) {
66 6
            if ($foreground_color !== null) {
67 6
                $line = Color::getColoredString($line, $foreground_color);
68
            }
69 6
            $output[] = $line;
70
        }
71
72 6
        echo implode(self::EOL, $output);
73 6
    }
74
75
}