Completed
Push — master ( c1f966...73bbc7 )
by Nicolas
03:19
created

ControllerMakeCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 123
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

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