Completed
Push — master ( 233d6f...b0653b )
by Nicolas
11:16
created

ControllerCommand::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1.037
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 27
     */
40
    public function getDestinationFilePath()
41 27
    {
42
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
43 27
44
        $controllerPath = $this->laravel['modules']->config('paths.generator.controller');
45 27
46
        return $path . $controllerPath . '/' . $this->getControllerName() . '.php';
47
    }
48
49
    /**
50
     * @return string
51 27
     */
52
    protected function getTemplateContents()
53 27
    {
54
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
55 27
56 27
        return (new Stub($this->getStubName(), [
57 27
            'MODULENAME'        => $module->getStudlyName(),
58 27
            'CONTROLLERNAME'    => $this->getControllerName(),
59 27
            'NAMESPACE'         => $module->getStudlyName(),
60 27
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
61 27
            'CLASS'             => $this->getClass(),
62 27
            'LOWER_NAME'        => $module->getLowerName(),
63 27
            'MODULE'            => $this->getModuleName(),
64 27
            'NAME'              => $this->getModuleName(),
65 27
            'STUDLY_NAME'       => $module->getStudlyName(),
66 27
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
67
        ]))->render();
68
    }
69
70
    /**
71
     * Get the console command arguments.
72
     *
73
     * @return array
74 27
     */
75
    protected function getArguments()
76
    {
77 27
        return array(
78
            array('controller', InputArgument::REQUIRED, 'The name of the controller class.'),
79
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
80
        );
81
    }
82
83
    /**
84
     * @return array
85 27
     */
86
    protected function getOptions()
87 27
    {
88
        return [
89 27
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null],
90
        ];
91
    }
92
93 27
    /**
94
     * @return array|string
95
     */
96
    protected function getControllerName()
97
    {
98
        $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
        if (str_contains(strtolower($controller), 'controller') === false) {
101 27
            $controller .= 'Controller';
102
        }
103 27
104
        return $controller;
105
    }
106
107
    /**
108
     * Get default namespace.
109
     *
110
     * @return string
111
     */
112
    public function getDefaultNamespace()
113
    {
114
        return 'Http\Controllers';
115
    }
116
117
    /**
118
     * Get the stub file name based on the plain option
119
     * @return string
120
     */
121
    private function getStubName()
122
    {
123
        if ($this->option('plain') === true) {
124
            return '/controller-plain.stub';
125
        }
126
127
        return '/controller.stub';
128
    }
129
}
130