Completed
Branch release/1.3 (364b03)
by Johnny
02:53
created

Parser   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 119
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setFilter() 0 6 1
A getCommand() 0 5 1
B parse() 0 32 7
B buildOptions() 0 22 6
1
<?php
2
namespace Redbox\Cli\Arguments;
3
4
5
6
/**
7
 * This class will parse the given arguments.
8
 *
9
 * @package Redbox\Cli\Arguments
10
 */
11
class Parser
12
{
13
    /**
14
     * @var \Redbox\Cli\Arguments\Filter
15
     */
16
    protected $filter;
17
18
    /**
19
     * @var \Redbox\Cli\Arguments\Manager
20
     */
21
    protected $manager;
22
23
    /**
24
     * Parser constructor.
25
     * @var array
26
     */
27
    protected $arguments;
28
    
29
    /**
30
     * Parser constructor.
31
     *
32
     * @param Manager $manager
33
     */
34 13
    public function __construct(Manager $manager = NULL)
35
    {
36 13
        $this->manager = $manager;
37 13
    }
38
39
    /**
40
     * Set the filter for the parser.
41
     *
42
     * @param Filter $filter
43
     * @param array $arguments
44
     */
45 9
    public function setFilter(Filter $filter, array $arguments = [])
46
    {
47 9
        $this->filter    = $filter;
48 9
        $this->arguments = $arguments;
49 9
        $this->filter->setArguments($arguments);
50 9
    }
51
    
52
    /**
53
     * Return the script name.
54
     *
55
     * @return mixed
56
     */
57 2
    public function getCommand()
58
    {
59 2
        global $argv;
60 2
        return $argv[0];
61
    }
62
    
63
    
64
    /**
65
     * This is it, we parse the given arguments.
66
     *
67
     * @throws \Exception
68
     */
69 9
    public function parse()
70
    {
71 9
        list($shortOptions, $longOptions) = $this->buildOptions();
72 9
        $results = getopt($shortOptions, $longOptions);
73
        
74 9
        foreach ($this->arguments as $argument) {
75 9
            $name  = $argument->name;
76 9
            $value = '';
77
78 9
            if (isset($results[$argument->prefix]) || isset($results[$argument->longPrefix])) {
79 3
                $value = isset($results[$argument->prefix]) ? $results[$argument->prefix] : $results[$argument->longPrefix];
80
            } else {
81
                /**
82
                 * If we set the default value for this argument we also add it to
83
                 * the result array or it will fail the argument has the option required by mistake.
84
                 */
85 6
                if ($argument->defaultValue) {
86 4
                    $value = $argument->defaultValue;
87 4
                    $this->manager->setHasDefaultValue($name, true);
88 4
                    $results[$argument->name] = $value;
89
                } else {
90 3
                    if ($argument->required === true) {
91 1
                        throw new \Exception(
92
                            'The following arguments are required: '
93 1
                            . print_r($argument->name, true) . '.'
94
                        );
95
                    }
96
                }
97
            }
98 8
            $this->manager->set($name, $value);
99
        }
100 8
    }
101
102
    /**
103
     * Build the option arrays that could be used with getopt()
104
     *
105
     * @return array
106
     */
107 9
    private function buildOptions()
108
    {
109 9
        $short_prefixes = $this->filter->withShortPrefix();
110 9
        $long_prefixes  = $this->filter->withLongPrefix();
111
112 9
        $short = '';
113 9
        $long  = array();
114
115 9
        foreach ($short_prefixes as $argument) {
116 9
            $short .= $argument->prefix;
117 9
            $short .= ($argument->required == true) ? ':' : '::';
118
        }
119
120 9
        foreach ($long_prefixes as $argument) {
121 9
            $rule  = $argument->longPrefix;
122 9
            if (strlen($rule) > 0) {
123 6
                $rule .= ($argument->required == true) ? ':' : '::';
124 6
                $long[] = $rule;
125
            }
126
        }
127 9
        return [$short, $long];
128
    }
129
}