Completed
Branch master (bff1a4)
by Filipe
02:53
created

GenerateController::getControllerName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Console\Command;
11
12
use Slick\Mvc\Console\Command\Task\CreateController;
13
use Slick\Mvc\Console\Command\Task\CreatedCrudController;
14
use Slick\Mvc\Exception\FileNotFoundException;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Generate:controller command
23
 *
24
 * @package Slick\Mvc\Console\Command
25
 * @author  Filipe Silva <[email protected]>
26
 */
27
class GenerateController extends Command
28
{
29
30
    /**
31
     * @var string
32
     */
33
    protected $controllerName;
34
35
    /**
36
     * @var InputInterface
37
     */
38
    protected $input;
39
40
    /**
41
     * @var OutputInterface
42
     */
43
    protected $output;
44
45
    /**
46
     * @var string
47
     */
48
    protected $path;
49
50
    /**
51
     * @var string
52
     */
53
    protected $namespace;
54
55
    /**
56
     * @var string|null
57
     */
58
    protected $entityName;
59
60
    /**
61
     * @var CreateController
62
     */
63
    protected $controllerGenerator;
64
65
    /**
66
     * Configures the current command.
67
     */
68 10
    protected function configure()
69
    {
70 5
        $this
71 10
            ->setName("generate:controller")
72 10
            ->setDescription("Generate a controller class file.")
73 10
            ->addArgument(
74 10
                'controllerName',
75 10
                InputArgument::REQUIRED,
76 5
                'Controller class name.'
77 5
            )
78 10
            ->addOption(
79 10
                'entity-name',
80 10
                'e',
81 10
                InputOption::VALUE_OPTIONAL,
82 5
                'Creates a CRUD controller for provided entity.'
83 5
            )
84 10
            ->addOption(
85 10
                'source-path',
86 10
                'p',
87 10
                InputOption::VALUE_OPTIONAL,
88 10
                'Sets the application source path',
89 10
                getcwd().'/src'
90 5
            )
91 10
            ->addOption(
92 10
                'name-space',
93 10
                'o',
94 10
                InputOption::VALUE_OPTIONAL,
95 10
                'The controller namespace.',
96 5
                'Controller'
97 5
            )
98
        ;
99 10
    }
100
101
    /**
102
     * Executes the current command.
103
     *
104
     * This method is not abstract because you can use this class
105
     * as a concrete class. In this case, instead of defining the
106
     * execute() method, you set the code to execute by passing
107
     * a Closure to the setCode() method.
108
     *
109
     * @param InputInterface  $input  An InputInterface instance
110
     * @param OutputInterface $output An OutputInterface instance
111
     *
112
     * @return null|integer null or 0 if everything went fine, or an error code
113
     *
114
     * @throws \LogicException When this abstract method is not implemented
115
     * @see    setCode()
116
     */
117 2
    protected function execute(InputInterface $input, OutputInterface $output)
118
    {
119 2
        $this->setInput($input)->setOutput($output);
120 2
        $output->writeln('Slick MVC <info>v1.2.0</info>');
121 2
        $result = $this->getControllerGenerator()
122 2
            ->setInput($input)
123 2
            ->setOutput($output)
124 2
            ->setCommand($this)
125 2
            ->run();
126 2
        $output->writeln('<info>...Done!</info>');
127 2
        return $result;
128
    }
129
130
    /**
131
     * Gets controllerName property
132
     *
133
     * @return string
134
     */
135 6
    public function getControllerName()
136
    {
137 6
        if (null == $this->controllerName) {
138 6
            $this->controllerName = ucfirst($this->input->getArgument('controllerName'));
139 3
        }
140 6
        return $this->controllerName;
141
    }
142
143
    /**
144
     * Gets path property
145
     *
146
     * @return string
147
     */
148 6
    public function getPath()
149
    {
150 6
        if (null == $this->path) {
151 6
            $this->path = $this->input->getOption('source-path');
152 6
            if (!is_dir($this->path)) {
153 2
                throw new FileNotFoundException(
154 1
                    "The provided path was not found in your system."
155 1
                );
156
            }
157 2
        }
158 4
        return $this->path;
159
    }
160
161
    /**
162
     * Get controller namespace
163
     *
164
     * @return string
165
     */
166 4
    public function getNameSpace()
167
    {
168 4
        if (null == $this->namespace) {
169 4
            $this->namespace = $this->input->getOption('name-space');
170 2
        }
171 4
        return $this->namespace;
172
    }
173
174
    /**
175
     * Get the entity name
176
     *
177
     * @return null|string
178
     */
179 6
    public function getEntityName()
180
    {
181 6
        if (null == $this->entityName) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->entityName of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
182 6
            $this->entityName = $this->input->getOption('entity-name');
183 3
        }
184 6
        return $this->entityName;
185
    }
186
187
    /**
188
     * Gets controllerGenerator property
189
     *
190
     * @return CreateController
191
     */
192 8
    public function getControllerGenerator()
193
    {
194 8
        if (null == $this->controllerGenerator) {
195 6
            $class = $this->getTaskClass();
196 6
            $this->setControllerGenerator(
197 6
                new $class([
198 6
                    'entityName' => $this->getEntityName(),
199 6
                    'controllerName' => $this->getControllerName(),
200 6
                    'sourcePath' => $this->getPath(),
201 4
                    'namespace' => $this->getNameSpace()
202 2
                ])
203 2
            );
204 2
        }
205 6
        return $this->controllerGenerator;
206
    }
207
208
    /**
209
     * Sets controllerGenerator property
210
     *
211
     * @param CreateController $controllerGenerator
212
     *
213
     * @return GenerateController
214
     */
215 6
    public function setControllerGenerator(
216
        CreateController $controllerGenerator
217
    ) {
218 6
        $this->controllerGenerator = $controllerGenerator;
219 6
        return $this;
220
    }
221
222
    /**
223
     * Sets input property
224
     *
225
     * @param InputInterface $input
226
     *
227
     * @return GenerateController
228
     */
229 8
    public function setInput(InputInterface $input)
230
    {
231 8
        $this->input = $input;
232 8
        return $this;
233
    }
234
235
    /**
236
     * Sets output property
237
     *
238
     * @param OutputInterface $output
239
     *
240
     * @return GenerateController
241
     */
242 8
    public function setOutput(OutputInterface $output)
243
    {
244 8
        $this->output = $output;
245 8
        return $this;
246
    }
247
248
    /**
249
     * Gets the task for this command
250
     *
251
     * @return string
252
     */
253 6
    protected function getTaskClass()
254
    {
255 6
        $info = "Generate controller '{$this->getControllerName()}'...";
256 6
        $class = CreateController::class;
257 6
        if (null !== $this->getEntityName()) {
258 4
            $info = "Generate CRUD controller '{$this->getControllerName()}'...";
259 4
            $class = CreatedCrudController::class;
260 2
        }
261 6
        $this->output->writeln("<info>{$info}</info>");
262 6
        return $class;
263
    }
264
265
}