GenerateControllerCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace LaravelModulize\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use LaravelModulize\Support\Entity;
7
8
class GenerateControllerCommand extends BaseGeneratorCommand
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'modulize:make:controller {module} {name} {--m|model=}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Generate a controller for the module';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return mixed
28
     */
29
    public function handle()
30
    {
31
        parent::handle();
32
    }
33
34
    /**
35
     * Get the stub file for the generator.
36
     *
37
     * @return string
38
     */
39
    protected function getStub(): string
40
    {
41
        if ($this->option('model')) {
42
            return __DIR__ . '/stubs/ModelController.stub';
43
        }
44
45
        return __DIR__ . '/stubs/Controller.stub';
46
    }
47
48
    /**
49
     * Get the destination class path.
50
     *
51
     * @param  string  $name
52
     * @return string
53
     */
54
    protected function getPath($name): string
55
    {
56
        return $this->repository->controllerPath($this->module) . '/' . $this->getNameInput() . '.php';
57
    }
58
59
    /**
60
     * Build the class with the given name.
61
     *
62
     * @param  string  $name
63
     * @return string
64
     */
65
    protected function buildClass($name)
66
    {
67
        $dummies = [
68
            'DummyClass',
69
            'DummyModel',
70
            'DummyLowercasedModel',
71
            'DummyControllerNamespace',
72
            'DummyModuleNamespace',
73
            'ModelName',
74
        ];
75
76
        $modelName = Entity::getClassName($this->option('model'));
0 ignored issues
show
Unused Code introduced by
The assignment to $modelName is dead and can be removed.
Loading history...
77
78
        $replacements = [
79
            Entity::getClassName($this->getNameInput()),
80
            str_replace('/', '\\', $this->option('model')),
81
            Str::lower($this->option('model')),
82
            Entity::getClassNamespace($this->getNameInput()),
83
            $this->repository->getModuleNamespace($this->module),
84
        ];
85
86
        return str_replace($dummies, $replacements, parent::buildClass($name));
87
    }
88
89
    /**
90
     * Get the default namespace for the class.
91
     *
92
     * @param  string  $rootNamespace
93
     * @return string
94
     */
95
    protected function getDefaultNamespace($rootNamespace)
96
    {
97
        return $this->repository->getModuleNamespace($this->module);
98
    }
99
}
100