Passed
Pull Request — master (#35)
by Anton
03:28
created

AbstractCommand::setInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 10
    public function setInput(InputInterface $input) : void
49
    {
50 10
        $this->input = $input;
51 10
    }
52
53
    /**
54
     * @return InputInterface
55
     */
56 10
    public function getInput()
57
    {
58 10
        return $this->input;
59
    }
60
61
    /**
62
     * @param OutputInterface $output
63
     */
64 10
    public function setOutput(OutputInterface $output) : void
65
    {
66 10
        $this->output = $output;
67 10
    }
68
69
    /**
70
     * @return OutputInterface
71
     */
72 10
    public function getOutput()
73
    {
74 10
        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 6
    public function getFs()
89
    {
90 6
        if (!$this->fs) {
91 6
            $this->fs = new Filesystem();
92
        }
93 6
        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 10
    final public function initialize(InputInterface $input, OutputInterface $output)
104
    {
105 10
        parent::initialize($input, $output);
106
107 10
        $this->setInput($input);
108 10
        $this->setOutput($output);
109
110 10
        putenv('BLUZ_ENV=' . ($input->getOption('env') ?: getenv('BLUZ_ENV')));
111
112 10
        $config = new \Bluz\Config\Config();
113 10
        $config->setPath(PATH_APPLICATION);
114 10
        $config->setEnvironment($input->getOption('env'));
115 10
        $config->init();
116
117 10
        Config::setInstance($config);
118 10
    }
119
120
    /**
121
     * @param $message
122
     * @return void
123
     */
124 10
    public function write($message) : void
125
    {
126 10
        $this->getOutput()->writeln($message);
127 10
    }
128
129
    /**
130
     * @param $message
131
     * @return void
132
     */
133
    public function info($message) : void
134
    {
135
        $this->write("<info>$message</info>");
136
    }
137
138
    /**
139
     * @param $message
140
     * @return void
141
     */
142
    public function comment($message) : void
143
    {
144
        $this->write("<comment>$message</comment>");
145
    }
146
147
    /**
148
     * @param $message
149
     * @return void
150
     */
151
    public function question($message) : void
152
    {
153
        $this->write("<question>$message</question>:");
154
    }
155
156
    /**
157
     * @param $message
158
     * @return void
159
     */
160 4
    public function error($message) : void
161
    {
162 4
        $this->write("<error>$message</error>");
163 4
    }
164
165
    /**
166
     * @internal param $output
167
     */
168
    public function callForContribute()
169
    {
170
        $this->info(
171
            ' This command is not implemented yet. Don\'t be indifferent - you can contribute!' .
172
            ' https://github.com/bluzphp/bluzman. '
173
        );
174
    }
175
176
    /**
177
     * Add Module Argument
178
     *
179
     * @param int $required
180
     * @return void
181
     */
182 14
    protected function addModuleArgument($required = InputArgument::REQUIRED) : void
183
    {
184 14
        $module = new InputArgument('module', $required, 'Module name is required');
185 14
        $this->getDefinition()->addArgument($module);
186 14
    }
187
188
    /**
189
     * Validate Module Argument
190
     *
191
     * @return void
192
     * @throws \Bluzman\Input\InputException
193
     */
194 5 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...
195
    {
196 5
        $module = $this->getInput()->getArgument('module');
197
198 5
        $validator = Validator::create()
199 5
            ->string()
200 5
            ->alphaNumeric('-_')
201 5
            ->noWhitespace();
202
203 5
        if ($this->getDefinition()->getArgument('module')->isRequired()
204 3
            && !$validator->validate($module)) {
205
            throw new InputException($validator->getError());
206
        }
207 5
    }
208
209
    /**
210
     * Add Controller Argument
211
     *
212
     * @param int $required
213
     * @return void
214
     */
215 14
    protected function addControllerArgument($required = InputArgument::REQUIRED) : void
216
    {
217 14
        $controller = new InputArgument('controller', $required, 'Controller name is required');
218 14
        $this->getDefinition()->addArgument($controller);
219 14
    }
220
221
    /**
222
     * Validate Module Argument
223
     *
224
     * @return void
225
     * @throws \Bluzman\Input\InputException
226
     */
227 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...
228
    {
229 1
        $controller = $this->getInput()->getArgument('controller');
230
231 1
        $validator = Validator::create()
232 1
            ->string()
233 1
            ->alphaNumeric('-_')
234 1
            ->noWhitespace();
235
236 1
        if ($this->getDefinition()->getArgument('controller')->isRequired()
237 1
            && !$validator->validate($controller)) {
238
            throw new InputException($validator->getError());
239
        }
240 1
    }
241
}
242