Completed
Pull Request — master (#1088)
by
unknown
02:55
created

ControllerMakeCommand::getDefaultNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Stub;
8
use Nwidart\Modules\Traits\ModuleCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ControllerMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommandTrait;
15
16
    /**
17
     * The name of argument being used.
18
     *
19
     * @var string
20
     */
21
    protected $argumentName = 'controller';
22
23
    /**
24
     * The console command name.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'module:make-controller';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Generate new restful controller for the specified module.';
36
37
    /**
38
     * Get controller name.
39
     *
40
     * @return string
41
     */
42
    public function getDestinationFilePath()
43
    {
44
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
45
46
        $controllerPath = GenerateConfigReader::read('controller');
47
48
        return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php';
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    protected function getTemplateContents()
55
    {
56
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
57
58
        return (new Stub($this->getStubName(), [
59
            'MODULENAME'        => $module->getStudlyName(),
60
            'CONTROLLERNAME'    => $this->getControllerName(),
61
            'NAMESPACE'         => $module->getStudlyName(),
62
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
63
            'CLASS'             => $this->getControllerNameWithoutNamespace(),
64
            'LOWER_NAME'        => $module->getLowerName(),
65
            'MODULE'            => $this->getModuleName(),
66
            'NAME'              => $this->getModuleName(),
67
            'STUDLY_NAME'       => $module->getStudlyName(),
68
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
69
        ]))->render();
70
    }
71
72
    /**
73
     * Get the console command arguments.
74
     *
75
     * @return array
76
     */
77 4
    protected function getArguments()
78
    {
79
        return [
80 4
            ['controller', InputArgument::REQUIRED, 'The name of the controller class.'],
81
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
82
        ];
83
    }
84
85
    /**
86
     * @return array
87
     */
88 4 View Code Duplication
    protected function getOptions()
89
    {
90
        return [
91 4
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null],
92
            ['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller.'],
93
        ];
94
    }
95
96
    /**
97
     * @return array|string
98
     */
99
    protected function getControllerName()
100
    {
101
        $controller = Str::studly($this->argument('controller'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('controller') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, Illuminate\Support\Str::studly() 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...
102
103
        if (Str::contains(strtolower($controller), 'controller') === false) {
104
            $controller .= 'Controller';
105
        }
106
107
        return $controller;
108
    }
109
110
    /**
111
     * @return array|string
112
     */
113
    private function getControllerNameWithoutNamespace()
114
    {
115
        return class_basename($this->getControllerName());
0 ignored issues
show
Bug introduced by
It seems like $this->getControllerName() targeting Nwidart\Modules\Commands...nd::getControllerName() can also be of type array; however, class_basename() does only seem to accept string|object, 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...
116
    }
117
118
    public function getDefaultNamespace() : string
119
    {
120
        $module = $this->laravel['modules'];
121
122
        return $module->config('paths.generator.controller.namespace') ?: $module->config('paths.generator.controller.path', 'Http/Controllers');
123
    }
124
125
    /**
126
     * Get the stub file name based on the options
127
     * @return string
128
     */
129
    protected function getStubName()
130
    {
131
        if ($this->option('plain') === true) {
132
            $stub = '/controller-plain.stub';
133
        } elseif ($this->option('api') === true) {
134
            $stub = '/controller-api.stub';
135
        } else {
136
            $stub = '/controller.stub';
137
        }
138
139
        return $stub;
140
    }
141
}
142