Completed
Push — master ( b0653b...b88c60 )
by Nicolas
05:50
created

ControllerCommand::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Traits\ModuleCommandTrait;
6
use Pingpong\Support\Stub;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class ControllerCommand extends GeneratorCommand
11
{
12
    use ModuleCommandTrait;
13
14
    /**
15
     * The name of argument being used.
16
     *
17
     * @var string
18
     */
19
    protected $argumentName = 'controller';
20
21
    /**
22
     * The console command name.
23
     *
24
     * @var string
25
     */
26
    protected $name = 'module:make-controller';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Generate new restful controller for the specified module.';
34
35
    /**
36
     * Get controller name.
37
     *
38
     * @return string
39
     */
40 31
    public function getDestinationFilePath()
41
    {
42 31
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
43
44 31
        $controllerPath = $this->laravel['modules']->config('paths.generator.controller');
45
46 31
        return $path . $controllerPath . '/' . $this->getControllerName() . '.php';
47
    }
48
49
    /**
50
     * @return string
51
     */
52 31
    protected function getTemplateContents()
53
    {
54 31
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
55
56 31
        return (new Stub($this->getStubName(), [
57 31
            'MODULENAME'        => $module->getStudlyName(),
58 31
            'CONTROLLERNAME'    => $this->getControllerName(),
59 31
            'NAMESPACE'         => $module->getStudlyName(),
60 31
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
61 31
            'CLASS'             => $this->getClass(),
62 31
            'LOWER_NAME'        => $module->getLowerName(),
63 31
            'MODULE'            => $this->getModuleName(),
64 31
            'NAME'              => $this->getModuleName(),
65 31
            'STUDLY_NAME'       => $module->getStudlyName(),
66 31
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
67 31
        ]))->render();
68
    }
69
70
    /**
71
     * Get the console command arguments.
72
     *
73
     * @return array
74
     */
75 31
    protected function getArguments()
76
    {
77
        return array(
78 31
            array('controller', InputArgument::REQUIRED, 'The name of the controller class.'),
79 31
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
80 31
        );
81
    }
82
83
    /**
84
     * @return array
85
     */
86 31
    protected function getOptions()
87 31
    {
88
        return [
89 31
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null],
90 31
        ];
91
    }
92
93
    /**
94
     * @return array|string
95
     */
96 31
    protected function getControllerName()
97
    {
98 31
        $controller = studly_case($this->argument('controller'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('controller') targeting Illuminate\Console\Command::argument() can also be of type array; however, studly_case() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
99
100 31
        if (str_contains(strtolower($controller), 'controller') === false) {
101 1
            $controller .= 'Controller';
102 1
        }
103
104 31
        return $controller;
105
    }
106
107
    /**
108
     * Get default namespace.
109
     *
110
     * @return string
111
     */
112 31
    public function getDefaultNamespace()
113
    {
114 31
        return 'Http\Controllers';
115
    }
116
117
    /**
118
     * Get the stub file name based on the plain option
119
     * @return string
120
     */
121 31
    private function getStubName()
122
    {
123 31
        if ($this->option('plain') === true) {
124 1
            return '/controller-plain.stub';
125
        }
126
127 31
        return '/controller.stub';
128
    }
129
}
130