Completed
Push — master ( ea8fd7...b1104c )
by Oleg
03:59
created

CliResolver   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 1
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getApp() 0 4 1
A getAction() 0 4 2
C getOption() 0 24 16
1
<?php /** MicroConsoleResolver */
2
3
namespace Micro\Cli;
4
5
use Micro\Base\Exception;
6
use Micro\base\ResolverInterface;
7
8
/**
9
 * CLI Resolver class file.
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/linpax/microphp-framework
13
 * @copyright Copyright (c) 2013 Oleg Lunegov
14
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
15
 * @package Micro
16
 * @subpackage Resolver
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
class CliResolver implements ResolverInterface
21
{
22
    /**
23
     * Get instance application
24
     *
25
     * @access public
26
     *
27
     * @return Console
28
     */
29
    public function getApp()
30
    {
31
        return new Console();
32
    }
33
34
    /**
35
     * Get action from request
36
     *
37
     * @access public
38
     *
39
     * @return string
40
     * @throws Exception
41
     */
42
    public function getAction()
43
    {
44
        return $this->getOption('a', 'action') ?: 'default';
45
    }
46
47
    /**
48
     * Get arguments from command line
49
     *
50
     * @access public
51
     *
52
     * @param string $char -a .. -z option char
53
     * @param string $name --optionName_string
54
     * @param bool|null $required Required value?
55
     *
56
     * @return mixed
57
     */
58
    public function getOption($char = '', $name = '', $required = null)
59
    {
60
        if (!$char && !$name) {
61
            return false;
62
        }
63
        if ($char && (1 < strlen($char) || 1 !== preg_match('/^\w$/', $char))) {
64
            return false;
65
        }
66
        if ($name && (1 !== preg_match('/^\w+$/', $name))) {
67
            return false;
68
        }
69
        switch ($required) {
70
            case true:
71
                $char = $char ? $char . ':' : $char;
72
                $name = $name ? $name . ':' : $name;
73
                break;
74
            case false:
75
                $char = $char ? $char . '::' : $char;
76
                $name = $name ? $name . '::' : $name;
77
                break;
78
        }
79
        $argv = ($opts = getopt($char, [$name])) ? array_shift($opts) : [];
80
        return is_array($argv) ? array_shift($argv) : $argv;
81
    }
82
}
83