Passed
Push — master ( f833b1...244bf6 )
by Timm
02:07
created

AppParser::callRunner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stefaminator\Cli;
5
6
7
use Exception;
8
use GetOptionKit\ContinuousOptionParser;
9
use GetOptionKit\OptionResult;
10
11
class AppParser {
12
13 5
    public static function run(App $app): void {
14 5
        global $argv;
15
16
        try {
17 5
            $cmd = self::parse($app, $argv);
18
19 5
            if ($cmd !== null) {
20
21 5
                if ($cmd->handleOptionParseException()) {
22 1
                    return;
23
                }
24
25 4
                self::callStack($app, $cmd);
26
27
            }
28 1
        } catch (Exception $e) {
29
30 1
            CmdRunner::eol();
31 1
            CmdRunner::echo('Uups, someting went wrong!', Color::FOREGROUND_COLOR_RED);
32 1
            CmdRunner::eol();
33 1
            CmdRunner::echo($e->getMessage(), Color::FOREGROUND_COLOR_RED);
34 1
            CmdRunner::eol();
35
        }
36
37 4
    }
38
39
    /**
40
     * @param App $app
41
     * @param Cmd $cmd
42
     * @throws Exception
43
     */
44 4
    private static function callStack(App $app, Cmd $cmd): void {
45
46 4
        if (self::callRunner($cmd)) {
47 3
            return;
48
        }
49
50
        if (self::callMethod($app, $cmd)) {
51
            return;
52
        }
53
    }
54
55
    /**
56
     * @param Cmd $cmd
57
     * @return bool
58
     * @throws Exception
59
     */
60 4
    private static function callRunner(Cmd $cmd): bool {
61
62 4
        $runner = $cmd->getRunner();
63
64 4
        if($runner !== null) {
65 4
            $runner->run();
66 3
            return true;
67
        }
68
69
        return false;
70
    }
71
72
73
    /**
74
     * @param App $app
75
     * @param Cmd $cmd
76
     * @return bool
77
     * @throws Exception
78
     */
79
    private static function callMethod(App $app, Cmd $cmd): bool {
80
81
        $methodName = $cmd->getMethodName();
82
83
        if (method_exists($app, $methodName)) {
84
            $app->$methodName($cmd);
85
            return true;
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @param App $app
93
     * @param array $argv
94
     * @return Cmd
95
     */
96 20
    public static function parse(App $app, array $argv): Cmd {
97
98 20
        $cmd = $app->setup();
99
100 20
        $appspecs = $cmd->getOptionCollection();
101
102 20
        $parser = new ContinuousOptionParser($appspecs);
103
104
        try {
105 20
            $cmd->optionResult = $parser->parse($argv);
106
107 1
        } catch (Exception $e) {
108
109 1
            $cmd->optionParseException = $e;
110
111 1
            return $cmd;
112
        }
113
114 19
        while (!$parser->isEnd()) {
115
116 16
            $currentArgument = $parser->getCurrentArgument();
117
118 16
            $subcommand = self::getSubcommand($currentArgument, $cmd);
119
120 16
            if ($subcommand !== null) {
121
122 10
                $cmd = $subcommand;
123
124
                try {
125 10
                    self::parseSubcommand($parser, $cmd);
126 1
                } catch (Exception $e) {
127 1
                    $cmd->optionParseException = $e;
128 10
                    return $cmd;
129
                }
130
131
            } else {
132 7
                $cmd->arguments[] = $parser->advance();
133
            }
134
        }
135
136 18
        return $cmd;
137
    }
138
139
    /**
140
     * @param string $argument
141
     * @param Cmd $cmd
142
     * @return Cmd|null
143
     */
144 16
    private static function getSubcommand(string $argument, Cmd $cmd): ?Cmd {
145
146 16
        $subcommand = null;
147 16
        if ($cmd->hasSubCmd($argument)) {
148 10
            $subcommand = $cmd->getSubCmd($argument);
149
        }
150
151 16
        return $subcommand;
152
    }
153
154
    /**
155
     * @param ContinuousOptionParser $parser
156
     * @param Cmd $cmd
157
     * @throws Exception
158
     */
159 10
    private static function parseSubcommand(ContinuousOptionParser $parser, Cmd $cmd): void {
160
161 10
        $parser->advance();
162
163 10
        $cmd->optionResult = new OptionResult();
164
165 10
        if (!empty($cmd->optionSpecs)) {
166
167 6
            $specs = $cmd->getOptionCollection();
168
169 6
            $parser->setSpecs($specs);
170
171 6
            $cmd->optionResult = $parser->continueParse();
172
        }
173 9
    }
174
175
176
}