ControllerMakeCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 33
c 1
b 0
f 0
dl 0
loc 78
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDestinationFilePath() 0 7 1
A getControllerName() 0 9 2
A getTemplateContents() 0 19 1
A getStubName() 0 7 2
A getDefaultNamespace() 0 6 2
A getControllerNameWithoutNamespace() 0 3 1
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
7
use Rawilk\LaravelModules\Support\Stub;
8
use Rawilk\LaravelModules\Traits\ModuleCommands;
9
10
class ControllerMakeCommand extends GeneratorCommand
11
{
12
    use ModuleCommands;
13
14
    /** @var string */
15
    protected $argumentName = 'controller';
16
17
    /** @var string */
18
    protected $signature = 'module:make-controller
19
                            {controller : The name of the controller class}
20
                            {module? : The name of the module to create the controller for}
21
                            {--p|plain : Generate a plain controller}
22
                            {--base_class= : Override the default base controller class (from config)}';
23
24
    /** @var string */
25
    protected $description = 'Generate a new controller for the specified module.';
26
27
    protected function getDefaultNamespace(): string
28
    {
29
        /** @var \Rawilk\LaravelModules\Contracts\Repository $module */
30
        $module = $this->laravel['modules'];
31
32
        return $module->config('paths.generator.controller.namespace') ?: $module->config('paths.generator.controller.path', 'Http/Controllers');
33
    }
34
35
    protected function getDestinationFilePath(): string
36
    {
37
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
38
39
        $controllerPath = GenerateConfigReader::read('controller');
40
41
        return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php';
42
    }
43
44
    protected function getTemplateContents(): string
45
    {
46
        /** @var \Rawilk\LaravelModules\Module $module */
47
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
48
49
        return (new Stub($this->getStubName(), [
50
            'MODULENAME'       => $module->getStudlyName(),
51
            'CONTROLLERNAME'   => $this->getControllerName(),
52
            'NAMESPACE'        => $module->getStudlyName(),
53
            'CLASS_NAMESPACE'  => $this->getClassNamespace($module),
54
            'CLASS'            => $this->getControllerNameWithoutNamespace(),
55
            'LOWER_NAME'       => $module->getLowerName(),
56
            'MODULE'           => $this->getModuleName(),
57
            'NAME'             => $this->getModuleName(),
58
            'STUDLY_NAME'      => $module->getStudlyName(),
59
            'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'),
60
            'BASE_CLASS'       => $this->getBaseClass('controller'),
61
            'BASE_CLASS_SHORT' => $this->getBaseClass('controller', true),
62
        ]))->render();
63
    }
64
65
    private function getControllerName(): string
66
    {
67
        $controller = Str::studly($this->argument('controller'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('controller') can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $controller = Str::studly(/** @scrutinizer ignore-type */ $this->argument('controller'));
Loading history...
68
69
        if (! Str::contains(strtolower($controller), 'controller')) {
70
            $controller .= 'Controller';
71
        }
72
73
        return $controller;
74
    }
75
76
    private function getControllerNameWithoutNamespace(): string
77
    {
78
        return class_basename($this->getControllerName());
79
    }
80
81
    private function getStubName(): string
82
    {
83
        if ($this->option('plain') === true) {
0 ignored issues
show
introduced by
The condition $this->option('plain') === true is always false.
Loading history...
84
            return '/controller-plain.stub';
85
        }
86
87
        return '/controller.stub';
88
    }
89
}
90