Passed
Push — master ( 2f27bd...7f89e7 )
by Anton
01:49
created

AbstractCommand::addModuleArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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