Passed
Push — master ( 42d472...9d6aee )
by Timm
02:01
created

AppParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 105
rs 10
wmc 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
B route() 0 59 9
B run() 0 35 6
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
                $callable = $cmd->getCallable();
26
27
                if ($callable !== null) {
28
                    $callable($cmd);
29
                    return;
30
                }
31
32
                $methodName = $cmd->getMethodName();
33
34
                if (method_exists($app, $methodName)) {
35
                    $app->$methodName($cmd);
36
                    return;
37
                }
38
39
40
            }
41
        } catch (Exception $e) {
42
43
            App::eol();
44
            App::echo('Uups, someting went wrong!', Color::FOREGROUND_COLOR_RED);
45
            App::eol();
46
            App::echo($e->getMessage(), Color::FOREGROUND_COLOR_RED);
47
            App::eol();
48
        }
49
50
    }
51
52
    /**
53
     * @param App $app
54
     * @param array $argv
55
     * @return Cmd|null
56
     */
57
    public static function route(App $app, array $argv): ?Cmd {
58
59
        $cmd = $app->setup();
60
61
        $appspecs = $cmd->getOptionCollection();
62
63
        $parser = new ContinuousOptionParser($appspecs);
64
65
        try {
66
            $cmd->optionResult = $parser->parse($argv);
67
        } catch (Exception $e) {
68
            $cmd->optionParseException = $e;
69
            return $cmd;
70
        }
71
72
        while (!$parser->isEnd()) {
73
74
            $currentArgument = $parser->getCurrentArgument();
75
76
            if ($cmd->hasSubCmd($currentArgument)) {
77
78
                $parser->advance();
79
80
                $cmd = $cmd->getSubCmd($currentArgument);
81
            }
82
83
            if ($cmd instanceof Cmd) {
84
85
                $options_parsed = $cmd->optionResult !== null;
86
87
                if (!$options_parsed) {
88
89
                    $cmd->optionResult = new OptionResult();
90
91
                    if (!empty($cmd->optionSpecs)) {
92
93
                        $specs = $cmd->getOptionCollection();
94
95
                        $parser->setSpecs($specs);
96
97
                        try {
98
                            $cmd->optionResult = $parser->continueParse();
99
                        } catch (Exception $e) {
100
                            $cmd->optionParseException = $e;
101
                            return $cmd;
102
                        }
103
104
                        continue;
105
                    }
106
                }
107
108
                if ($options_parsed) {
109
                    $cmd->arguments[] = $parser->advance();
110
                }
111
            }
112
113
        }
114
115
        return $cmd;
116
    }
117
118
119
}