Completed
Push — master ( 0fd405...f8ea68 )
by Nicolas
8s
created

ControllerMakeCommand::getDefaultNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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