MagicControllerCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 32
c 1
b 0
f 0
dl 0
loc 138
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getStub() 0 3 1
A buildClass() 0 21 2
A getDefaultNamespace() 0 3 1
A getPath() 0 6 1
A alreadyExists() 0 4 2
A resolveStubPath() 0 5 2
A getOptions() 0 6 1
1
<?php
2
3
namespace Ikechukwukalu\Magicmake\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Input\InputOption;
9
10
#[AsCommand(name: 'magic:controller')]
11
class MagicControllerCommand extends GeneratorCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'magic:controller';
19
20
    /**
21
     * The name of the console command.
22
     *
23
     * This name is used to identify the command during lazy loading.
24
     *
25
     * @var string|null
26
     *
27
     * @deprecated
28
     */
29
    protected static $defaultName = 'magic:controller';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create a new magic controller class';
37
38
    /**
39
     * The type of class being generated.
40
     *
41
     * @var string
42
     */
43
    protected $type = 'Controller';
44
45
    /**
46
     * Build the class with the given name.
47
     *
48
     * @param  string  $name
49
     * @return string
50
     */
51
    protected function buildClass($name)
52
    {
53
        if ($this->alreadyExists(str_replace('.php', '', $this->getPath($name)))) {
54
            $this->components->error("This Controller already exists");
55
            return;
56
        }
57
58
        $model = $name;
59
        $modelVariable = $this->option('variable');
60
        $modelUnderScore = $this->option('underscore');
61
62
        $stub = str_replace(
63
            ['DummyModel', '{{ model }}'], class_basename($model), parent::buildClass($name)
64
        );
65
66
        $stub = str_replace(
67
            ['DummyModelVariable', '{{ modelVariable }}'], trim($modelVariable, '\\'), $stub
68
        );
69
70
        return str_replace(
71
            ['DummyModelUnderScore', '{{ modelUnderScore }}'], trim($modelUnderScore, '\\'), $stub
72
        );
73
74
    }
75
76
    /**
77
     * Determine if the class already exists.
78
     *
79
     * @param  string  $rawName
80
     * @return bool
81
     */
82
    protected function alreadyExists($rawName)
83
    {
84
        return class_exists($rawName) ||
85
               $this->files->exists($this->getPath($this->qualifyClass($rawName)));
86
    }
87
88
    /**
89
     * Get the stub file for the generator.
90
     *
91
     * @return string
92
     */
93
    protected function getStub()
94
    {
95
        return __DIR__.'/stubs/controller.stub';
96
    }
97
98
    /**
99
     * Resolve the fully-qualified path to the stub.
100
     *
101
     * @param  string  $stub
102
     * @return string
103
     */
104
    protected function resolveStubPath($stub)
105
    {
106
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
107
                        ? $customPath
108
                        : __DIR__.$stub;
109
    }
110
111
    /**
112
     * Get the default namespace for the class.
113
     *
114
     * @param  string  $rootNamespace
115
     * @return string
116
     */
117
    protected function getDefaultNamespace($rootNamespace)
118
    {
119
        return 'App\Http\Controllers';
120
    }
121
122
    /**
123
     * Get the console command options.
124
     *
125
     * @return array
126
     */
127
    protected function getOptions()
128
    {
129
        return [
130
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the controller already exists'],
131
            ['variable', 'var', InputOption::VALUE_REQUIRED, 'Create a model variable value for this controller class'],
132
            ['underscore', 'u', InputOption::VALUE_REQUIRED, 'Create a model underscore value for this controller class'],
133
        ];
134
    }
135
136
    /**
137
     * Get the destination class path.
138
     *
139
     * @param  string  $name
140
     * @return string
141
     */
142
    protected function getPath($name)
143
    {
144
        $name = "{$name}Controller";
145
        $name = Str::replaceFirst($this->rootNamespace(), '', $name);
146
147
        return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
148
    }
149
}
150