Completed
Push — master ( 8a4ca9...acd23c )
by Marco
02:46
created

src/Shell/Controller.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Comodojo\Extender\Shell;
2
3
use \Comodojo\Exception\ShellException;
4
use \Exception;
5
use \Console_CommandLine;
6
use \Console_CommandLine_Exception;
7
use \Monolog\Logger;
8
use \Comodojo\Extender\TasksTable;
9
use \Comodojo\Extender\Command\CommandInterface;
10
use \Spyc;
11
12
/**
13
 * The commands controller
14
 *
15
 * It process command from ConsoleCommandline
16
 *
17
 * @package     Comodojo extender
18
 * @author      Marco Giovinazzi <[email protected]>
19
 * @license     GPL-3.0+
20
 *
21
 * LICENSE:
22
 * 
23
 * This program is free software: you can redistribute it and/or modify
24
 * it under the terms of the GNU Affero General Public License as
25
 * published by the Free Software Foundation, either version 3 of the
26
 * License, or (at your option) any later version.
27
 *
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31
 * GNU Affero General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU Affero General Public License
34
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35
 */
36
37
class Controller {
38
39
    /**
40
     * Array of commands' classes
41
     *
42
     * @var array
43
     */
44
    private $command_classes = array();
45
    
46
    /**
47
     * Current logger
48
     *
49
     * @var \Monolog\logger
50
     */
51
    private $logger = null;
52
53
    /**
54
     * Current Parser
55
     *
56
     * @var \Console_CommandLine
57
     */
58
    private $parser = null;
59
60
    /**
61
     * Class constructor
62
     *
63
     * @param \Console_CommandLine  $parser
64
     * @param \Monolog\logger       $logger
65
     */
66
    public function __construct(Console_CommandLine $parser, Logger $logger) {
67
68
        $this->logger = $logger;
69
70
        $this->parser = $parser;
71
72
    }
73
74
    /**
75
     * Class constructor
76
     *
77
     * @param \Console_CommandLine  $parser
0 ignored issues
show
There is no parameter named $parser. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
78
     * @param \Monolog\logger       $logger
0 ignored issues
show
There is no parameter named $logger. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
79
     */
80
    public function add($command, $parameters) {
81
82 View Code Duplication
        if ( empty($parameters["class"]) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
84
            $this->logger->error("Skipping command ".$command.": invalid command definition", array(
85
                "NAME"       => $command,
86
                "PARAMETERS" => $parameters
87
            ));
88
89
            return false;
90
91
        }
92
93
        // replace double backslashes from classname (if any!)
94
        $class = str_replace('\\\\', '\\', $parameters["class"]);
95
96 View Code Duplication
        if ( !class_exists($class) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
98
            $this->logger->error("Skipping command ".$command.": missing class", array(
99
                "NAME" => $command,
100
                "CLASS" => $class
101
            ));
102
103
            return false;
104
105
        } 
106
107
        $this->command_classes[$command] = $class;
108
109
        $params = array();
110
111
        if ( array_key_exists('description', $parameters) ) $params['description'] = $parameters['description'];
112
        if ( array_key_exists('aliases', $parameters) && is_array($parameters['aliases']) ) $params['aliases'] = $parameters['aliases'];
113
114
        $command = $this->parser->addCommand($command, $params);
115
116
        if ( array_key_exists('options', $parameters) && is_array($parameters['options']) ) {
117
118
            foreach ( $parameters['options'] as $option => $option_parameters ) {
119
                
120
                $command->addOption($option, $option_parameters);
121
122
            }
123
124
        }
125
126
        if ( array_key_exists('arguments', $parameters) && is_array($parameters['arguments']) ) {
127
128
            foreach ( $parameters['arguments'] as $argument => $argument_parameters ) {
129
                
130
                $command->addArgument($argument, $argument_parameters);
131
132
            }
133
134
        }
135
136
        return true;
137
138
    }
139
140
    /**
141
     * Execute command
142
     *
143
     * @param   string          $command   Command to execute
0 ignored issues
show
There is no parameter named $command. Did you maybe mean $command_name?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
144
     * @param   array           $options   Options provided
145
     * @param   array           $args      Arguments provided
146
     * @param   Console_Color2  $color     Injected Console_Color2 instance
147
     * @param   array           $tasks     Array of available tasks
148
     *
149
     * @return  string
150
     */
151
    public function execute($command_name, $options, $args, $color, $logger, TasksTable $tasks) {
152
153
        if ( array_key_exists($command_name, $this->command_classes) ) {
154
155
            $command_class = $this->command_classes[$command_name];
156
157
        } else {
158
159
            throw new ShellException("Command ".$command_name." not defined");
160
161
        }
162
163
        try {
164
            
165
            $command = new $command_class();
166
167
            if ( ($command instanceof CommandInterface) === false ) throw new ShellException("Command ".$command_name." is not compatible with econtrol");
168
169
            $command->setOptions($options);
170
171
            $command->setArguments($args);
172
173
            $command->setColor($color);
174
175
            $command->setTasks($tasks);
176
177
            $command->setLogger($logger);
178
179
            $return = $command->execute();
180
181
        } catch (ShellException $se) {
182
            
183
            throw $se;
184
185
        } catch (Exception $e) {
186
            
187
            throw $e;
188
189
        }
190
191
        return $return;
192
193
    }
194
195
    public static function load(Console_CommandLine $parser, Logger $logger) {
196
197
        $controller = new Controller($parser, $logger);
198
199
        if ( is_readable(EXTENDER_COMMANDS_CONFIG) ) {
200
201
            $commands = Spyc::YAMLLoad(EXTENDER_COMMANDS_CONFIG);
202
203
            foreach ($commands as $command => $parameters) {
204
                
205
                $controller->add($command, $parameters["data"]);
206
207
            }
208
209
        }
210
211
        return $controller;
212
213
    }
214
215
}
216