Completed
Pull Request — master (#354)
by Martin
02:25
created

ControllerMakeCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 120
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDestinationFilePath() 0 8 1
A getTemplateContents() 0 17 1
A getArguments() 0 7 1
A getOptions() 0 6 1
A getControllerName() 0 10 2
A getDefaultNamespace() 0 4 1
A getStubName() 0 8 2
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Support\Stub;
6
use Nwidart\Modules\Traits\ModuleCommandTrait;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class ControllerMakeCommand 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 54
    public function getDestinationFilePath()
41
    {
42 54
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
43
44 54
        $controllerPath = $this->laravel['modules']->config('paths.generator.controller');
45
46 54
        return $path . $controllerPath . '/' . $this->getControllerName() . '.php';
47
    }
48
49
    /**
50
     * @return string
51
     */
52 54
    protected function getTemplateContents()
53
    {
54 54
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
55
56 54
        return (new Stub($this->getStubName(), [
57 54
            'MODULENAME'        => $module->getStudlyName(),
58 54
            'CONTROLLERNAME'    => $this->getControllerName(),
59 54
            'NAMESPACE'         => $module->getStudlyName(),
60 54
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
61 54
            'CLASS'             => $this->getControllerName(),
62 54
            'LOWER_NAME'        => $module->getLowerName(),
63 54
            'MODULE'            => $this->getModuleName(),
64 54
            'NAME'              => $this->getModuleName(),
65 54
            'STUDLY_NAME'       => $module->getStudlyName(),
66 54
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
67 54
        ]))->render();
68
    }
69
70
    /**
71
     * Get the console command arguments.
72
     *
73
     * @return array
74
     */
75 60
    protected function getArguments()
76
    {
77
        return array(
78 60
            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
     */
86 60
    protected function getOptions()
87
    {
88
        return [
89 60
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null],
90
        ];
91
    }
92
93
    /**
94
     * @return array|string
95
     */
96 54
    protected function getControllerName()
97
    {
98 54
        $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 54
        if (str_contains(strtolower($controller), 'controller') === false) {
101 2
            $controller .= 'Controller';
102
        }
103
104 54
        return $controller;
105
    }
106
107
    /**
108
     * Get default namespace.
109
     *
110
     * @return string
111
     */
112 54
    public function getDefaultNamespace()
113
    {
114 54
        return 'Http\Controllers';
115
    }
116
117
    /**
118
     * Get the stub file name based on the plain option
119
     * @return string
120
     */
121 54
    private function getStubName()
122
    {
123 54
        if ($this->option('plain') === true) {
124 1
            return '/controller-plain.stub';
125
        }
126
127 54
        return '/controller.stub';
128
    }
129
}
130