Passed
Push — master ( 9d6aee...74eb15 )
by Timm
02:09
created

AppParser::run()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 2
b 0
f 0
nc 9
nop 1
dl 0
loc 28
rs 9.1111
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
    public static function run(App $app): void {
14
        global $argv;
15
16
        try {
17
            $cmd = self::route($app, $argv);
18
19
            if ($cmd !== null) {
20
21
                if ($cmd->handleOptionParseException()) {
22
                    return;
23
                }
24
25
                if(self::callCallable($cmd)) {
26
                    return;
27
                }
28
29
                if(self::callMethod($app, $cmd)) {
30
                    return;
31
                }
32
33
            }
34
        } catch (Exception $e) {
35
36
            App::eol();
37
            App::echo('Uups, someting went wrong!', Color::FOREGROUND_COLOR_RED);
38
            App::eol();
39
            App::echo($e->getMessage(), Color::FOREGROUND_COLOR_RED);
40
            App::eol();
41
        }
42
43
    }
44
45
    /**
46
     * @param Cmd $cmd
47
     * @return bool
48
     * @throws Exception
49
     */
50
    private static function callCallable(Cmd $cmd): bool {
51
52
        $callable = $cmd->getCallable();
53
54
        if ($callable !== null) {
55
            $callable($cmd);
56
            return true;
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * @param App $app
64
     * @param Cmd $cmd
65
     * @return bool
66
     * @throws Exception
67
     */
68
    private static function callMethod(App $app, Cmd $cmd): bool {
69
70
        $methodName = $cmd->getMethodName();
71
72
        if (method_exists($app, $methodName)) {
73
            $app->$methodName($cmd);
74
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * @param App $app
82
     * @param array $argv
83
     * @return Cmd
84
     */
85
    public static function route(App $app, array $argv): Cmd {
86
87
        $cmd = $app->setup();
88
89
        $appspecs = $cmd->getOptionCollection();
90
91
        $parser = new ContinuousOptionParser($appspecs);
92
93
        try {
94
            $cmd->optionResult = $parser->parse($argv);
95
        } catch (Exception $e) {
96
            $cmd->optionParseException = $e;
97
            return $cmd;
98
        }
99
100
        while (!$parser->isEnd()) {
101
102
            $currentArgument = $parser->getCurrentArgument();
103
104
            $subcommand = null;
105
            if ($cmd->hasSubCmd($currentArgument)) {
106
                $_cmd = $cmd->getSubCmd($currentArgument);
107
108
                if($_cmd !== null) {
109
                    $subcommand = $_cmd;
110
                }
111
            }
112
113
            if($subcommand !== null) {
114
115
                $cmd = $subcommand;
116
                $parser->advance();
117
118
                $cmd->optionResult = new OptionResult();
119
120
                if (!empty($cmd->optionSpecs)) {
121
122
                    $specs = $cmd->getOptionCollection();
123
124
                    $parser->setSpecs($specs);
125
126
                    try {
127
                        $cmd->optionResult = $parser->continueParse();
128
                    } catch (Exception $e) {
129
                        $cmd->optionParseException = $e;
130
                        return $cmd;
131
                    }
132
133
                    continue;
134
                }
135
136
            } else {
137
                $cmd->arguments[] = $parser->advance();
138
            }
139
        }
140
141
        return $cmd;
142
    }
143
144
145
}