Completed
Push — master ( f0680b...bff1a4 )
by Filipe
12:26
created

GenerateController::getTaskClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
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
    protected function configure()
69
    {
70
        $this
71
            ->setName("generate:controller")
72
            ->setDescription("Generate a controller class file.")
73
            ->addArgument(
74
                'controllerName',
75
                InputArgument::REQUIRED,
76
                'Controller class name.'
77
            )
78
            ->addOption(
79
                'entity-name',
80
                'e',
81
                InputOption::VALUE_OPTIONAL,
82
                'Creates a CRUD controller for provided entity.'
83
            )
84
            ->addOption(
85
                'source-path',
86
                'p',
87
                InputOption::VALUE_OPTIONAL,
88
                'Sets the application source path',
89
                getcwd().'/src'
90
            )
91
            ->addOption(
92
                'name-space',
93
                'o',
94
                InputOption::VALUE_OPTIONAL,
95
                'The controller namespace.',
96
                'Controller'
97
            )
98
        ;
99
    }
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
    protected function execute(InputInterface $input, OutputInterface $output)
118
    {
119
        $this->setInput($input)->setOutput($output);
120
        $output->writeln('Slick MVC <info>v1.2.0</info>');
121
        $result = $this->getControllerGenerator()
122
            ->setInput($input)
123
            ->setOutput($output)
124
            ->setCommand($this)
125
            ->run();
126
        $output->writeln('<info>...Done!</info>');
127
        return $result;
128
    }
129
130
    /**
131
     * Gets controllerName property
132
     *
133
     * @return string
134
     */
135
    public function getControllerName()
136
    {
137
        if (null == $this->controllerName) {
138
            $this->controllerName = ucfirst($this->input->getArgument('controllerName'));
139
        }
140
        return $this->controllerName;
141
    }
142
143
    /**
144
     * Gets path property
145
     *
146
     * @return string
147
     */
148
    public function getPath()
149
    {
150
        if (null == $this->path) {
151
            $this->path = $this->input->getOption('source-path');
152
            if (!is_dir($this->path)) {
153
                throw new FileNotFoundException(
154
                    "The provided path was not found in your system."
155
                );
156
            }
157
        }
158
        return $this->path;
159
    }
160
161
    /**
162
     * Get controller namespace
163
     *
164
     * @return string
165
     */
166
    public function getNameSpace()
167
    {
168
        if (null == $this->namespace) {
169
            $this->namespace = $this->input->getOption('name-space');
170
        }
171
        return $this->namespace;
172
    }
173
174
    /**
175
     * Get the entity name
176
     *
177
     * @return null|string
178
     */
179
    public function getEntityName()
180
    {
181
        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
            $this->entityName = $this->input->getOption('entity-name');
183
        }
184
        return $this->entityName;
185
    }
186
187
    /**
188
     * Gets controllerGenerator property
189
     *
190
     * @return CreateController
191
     */
192
    public function getControllerGenerator()
193
    {
194
        if (null == $this->controllerGenerator) {
195
            $class = $this->getTaskClass();
196
            $this->setControllerGenerator(
197
                new $class([
198
                    'entityName' => $this->getEntityName(),
199
                    'controllerName' => $this->getControllerName(),
200
                    'sourcePath' => $this->getPath(),
201
                    'namespace' => $this->getNameSpace()
202
                ])
203
            );
204
        }
205
        return $this->controllerGenerator;
206
    }
207
208
    /**
209
     * Sets controllerGenerator property
210
     *
211
     * @param CreateController $controllerGenerator
212
     *
213
     * @return GenerateController
214
     */
215
    public function setControllerGenerator(
216
        CreateController $controllerGenerator
217
    ) {
218
        $this->controllerGenerator = $controllerGenerator;
219
        return $this;
220
    }
221
222
    /**
223
     * Sets input property
224
     *
225
     * @param InputInterface $input
226
     *
227
     * @return GenerateController
228
     */
229
    public function setInput(InputInterface $input)
230
    {
231
        $this->input = $input;
232
        return $this;
233
    }
234
235
    /**
236
     * Sets output property
237
     *
238
     * @param OutputInterface $output
239
     *
240
     * @return GenerateController
241
     */
242
    public function setOutput(OutputInterface $output)
243
    {
244
        $this->output = $output;
245
        return $this;
246
    }
247
248
    /**
249
     * Gets the task for this command
250
     *
251
     * @return string
252
     */
253
    protected function getTaskClass()
254
    {
255
        $info = "Generate controller '{$this->getControllerName()}'...";
256
        $class = CreateController::class;
257
        if (null !== $this->getEntityName()) {
258
            $info = "Generate CRUD controller '{$this->getControllerName()}'...";
259
            $class = CreatedCrudController::class;
260
        }
261
        $this->output->writeln("<info>{$info}</info>");
262
        return $class;
263
    }
264
265
}