Passed
Push — master ( 963d3f...e19a8b )
by Anton
03:38
created

AbstractCommand::validateModuleArgument()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 10
cts 10
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command;
8
9
use Bluz\Proxy\Config;
10
use Bluz\Validator\Validator;
11
use Bluzman\Application\Application;
12
use Bluzman\Input\InputArgument;
13
use Bluzman\Input\InputException;
14
use Symfony\Component\Console;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Filesystem\Filesystem;
18
19
/**
20
 * Class AbstractCommand
21
 * @package Bluzman\Command
22
 *
23
 * @method Application getApplication()
24
 *
25
 * @author Pavel Machekhin
26
 * @created 2013-11-28 15:47
27
 */
28
abstract class AbstractCommand extends Console\Command\Command
29
{
30
    /**
31
     * @var InputInterface
32
     */
33
    protected $input;
34
35
    /**
36
     * @var OutputInterface
37
     */
38
    protected $output;
39
40
    /**
41
     * @var Filesystem
42
     */
43
    protected $fs;
44
45
    /**
46
     * @param InputInterface $input
47
     */
48 11
    public function setInput(InputInterface $input) : void
49
    {
50 11
        $this->input = $input;
51 11
    }
52
53
    /**
54
     * @return InputInterface
55
     */
56 11
    public function getInput()
57
    {
58 11
        return $this->input;
59
    }
60
61
    /**
62
     * @param OutputInterface $output
63
     */
64 11
    public function setOutput(OutputInterface $output) : void
65
    {
66 11
        $this->output = $output;
67 11
    }
68
69
    /**
70
     * @return OutputInterface
71
     */
72 11
    public function getOutput()
73
    {
74 11
        return $this->output;
75
    }
76
77
    /**
78
     * @param \Symfony\Component\Filesystem\Filesystem $fs
79
     */
80
    public function setFs($fs) : void
81
    {
82
        $this->fs = $fs;
83
    }
84
85
    /**
86
     * @return \Symfony\Component\Filesystem\Filesystem
87
     */
88 7
    public function getFs()
89
    {
90 7
        if (!$this->fs) {
91 7
            $this->fs = new Filesystem();
92
        }
93 7
        return $this->fs;
94
    }
95
96
    /**
97
     * @param  InputInterface  $input
98
     * @param  OutputInterface $output
99
     *
100
     * @return void
101
     * @throws \Bluz\Config\ConfigException
102
     */
103 11
    final public function initialize(InputInterface $input, OutputInterface $output)
104
    {
105 11
        parent::initialize($input, $output);
106
107 11
        $this->setInput($input);
108 11
        $this->setOutput($output);
109
110 11
        putenv('BLUZ_ENV=' . ($input->getOption('env') ?: getenv('BLUZ_ENV')));
111
112 11
        $loader = new \Bluz\Config\ConfigLoader();
113 11
        $loader->setPath(PATH_APPLICATION);
114 11
        $loader->setEnvironment($input->getOption('env'));
115 11
        $loader->load();
116
117 11
        $config = new \Bluz\Config\Config();
118 11
        $config->setFromArray($loader->getConfig());
119
120 11
        Config::setInstance($config);
121 11
    }
122
123
    /**
124
     * @param $message
125
     * @return void
126
     */
127 11
    public function write($message) : void
128
    {
129 11
        $this->getOutput()->writeln($message);
130 11
    }
131
132
    /**
133
     * @param $message
134
     * @return void
135
     */
136
    public function info($message) : void
137
    {
138
        $this->write("<info>$message</info>");
139
    }
140
141
    /**
142
     * @param $message
143
     * @return void
144
     */
145
    public function comment($message) : void
146
    {
147
        $this->write("<comment>$message</comment>");
148
    }
149
150
    /**
151
     * @param $message
152
     * @return void
153
     */
154
    public function question($message) : void
155
    {
156
        $this->write("<question>$message</question>:");
157
    }
158
159
    /**
160
     * @param $message
161
     * @return void
162
     */
163 5
    public function error($message) : void
164
    {
165 5
        $this->write("<error>$message</error>");
166 5
    }
167
168
    /**
169
     * @internal param $output
170
     */
171
    public function callForContribute()
172
    {
173
        $this->info(
174
            ' This command is not implemented yet. Don\'t be indifferent - you can contribute!' .
175
            ' https://github.com/bluzphp/bluzman. '
176
        );
177
    }
178
179
    /**
180
     * Add Module Argument
181
     *
182
     * @param int $required
183
     * @return void
184
     */
185 15
    protected function addModuleArgument($required = InputArgument::REQUIRED) : void
186
    {
187 15
        $module = new InputArgument('module', $required, 'Module name is required');
188 15
        $this->getDefinition()->addArgument($module);
189 15
    }
190
191
    /**
192
     * Validate Module Argument
193
     *
194
     * @return void
195
     * @throws \Bluzman\Input\InputException
196
     */
197 6 View Code Duplication
    protected function validateModuleArgument() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
198
    {
199 6
        $module = $this->getInput()->getArgument('module');
200
201 6
        $validator = Validator::create()
202 6
            ->string()
203 6
            ->alphaNumeric('-_')
204 6
            ->noWhitespace();
205
206 6
        if ($this->getDefinition()->getArgument('module')->isRequired()
207 6
            && !$validator->validate($module)) {
208 1
            throw new InputException($validator->getError());
209
        }
210 5
    }
211
212
    /**
213
     * Add Controller Argument
214
     *
215
     * @param int $required
216
     * @return void
217
     */
218 15
    protected function addControllerArgument($required = InputArgument::REQUIRED) : void
219
    {
220 15
        $controller = new InputArgument('controller', $required, 'Controller name is required');
221 15
        $this->getDefinition()->addArgument($controller);
222 15
    }
223
224
    /**
225
     * Validate Module Argument
226
     *
227
     * @return void
228
     * @throws \Bluzman\Input\InputException
229
     */
230 1 View Code Duplication
    protected function validateControllerArgument() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
231
    {
232 1
        $controller = $this->getInput()->getArgument('controller');
233
234 1
        $validator = Validator::create()
235 1
            ->string()
236 1
            ->alphaNumeric('-_')
237 1
            ->noWhitespace();
238
239 1
        if ($this->getDefinition()->getArgument('controller')->isRequired()
240 1
            && !$validator->validate($controller)) {
241
            throw new InputException($validator->getError());
242
        }
243 1
    }
244
}
245