Ccg::generate()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 30
rs 9.3222
c 1
b 0
f 0
cc 5
nc 4
nop 0
1
<?php
2
3
use enums\Config as EnumConfig;
4
use terminal\Terminal;
5
use filesystem\Filesystem;
6
use autocomplete\Autocomplete;
7
8
/**
9
 * Enter point for all commands
10
 */
11
class Ccg
12
{
13
    private $config;
14
    private $terminal;
15
    private $filesystem;
16
    private $autocomplete;
17
18
    function __construct(
19
        Config              $config,
20
        Terminal            $terminal,
21
        Filesystem          $filesystem,
22
        Autocomplete        $autocomplete
23
    )
24
    {
25
        $this->config               = $config;
26
        $this->terminal             = $terminal;
27
        $this->filesystem           = $filesystem;
28
        $this->autocomplete         = $autocomplete;
29
    }
30
31
    public function generate()
32
    {
33
        $generator          = $this->config->get(EnumConfig::$GENERATOR);
34
        $command            = $this->config->get(EnumConfig::$COMMAND) ?: 'index';
35
        $controllerClass    = 'controllers\\' . $generator;
36
        
37
        if (!$generator) {
38
            throw new \InvalidArgumentException('Provide a command. See php ccg.php help');
39
        }
40
41
        if (!class_exists($controllerClass)) {
42
            throw new \InvalidArgumentException('Command not recognized. See php ccg.php help');
43
        }
44
45
        $refl = new ReflectionClass($controllerClass);
46
        $controller = $refl->newInstanceArgs([
47
            $this->config,
48
            $this->terminal,
49
            $this->filesystem,
50
            $this->autocomplete
51
        ]);
52
        
53
        $allowedMethods = $controller::getAllowedMethods();
54
        if (in_array($command, $allowedMethods)) {
55
            $controller->{$command}();
56
        } else {
57
            throw new \InvalidArgumentException('Command not recognized. See php ccg.php help');
58
        }
59
60
        return $this;
61
    }
62
63
    public function autocomplete($arguments)
64
    {
65
        $generator          = $arguments['generator'];
66
        $command            = $arguments['command'];
67
        $prev               = $arguments['prev'];
68
        $cur                = $arguments['cur'];
69
        $controllerClass    = 'controllers\\' . $generator;
70
        
71
        /**
72
         * @todo add autogeneration this list
73
         */
74
        $generators = [
75
            'addon',
76
            'addon-xml',
77
            'help'
78
        ];
79
80
        $autocompletes = [];
81
82
        if (class_exists($controllerClass)) {
83
            $refl = new ReflectionClass($controllerClass);
84
            $controller = $refl->newInstanceArgs([
85
                $this->config,
86
                $this->terminal,
87
                $this->filesystem,
88
                $this->autocomplete
89
            ]);
90
            
91
            $method = $command;
92
            $is_method_exists = method_exists($controllerClass, $method);
93
            
94
            if ($method) {
95
                $method_autocomplete = $command . 'Autocomplete';
96
                $autocompletes = method_exists($controllerClass, $method_autocomplete) ? $controller->{$method_autocomplete}($prev, $cur, $arguments) : [];
97
            }
98
            
99
            if (empty($autocompletes) && !$is_method_exists) {
100
                $allowedMethods = $controller::getAllowedMethods();
101
                $autocompletes = array_map(function($method) use ($generator) {
102
                    return to_lower_case($generator . '/' . $method);
103
                }, $allowedMethods);
104
            }
105
        } else {
106
            $autocompletes = $generators;
107
        }
108
109
        return $autocompletes;
110
    }
111
}
112