MagicDeleteRequestCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildClass() 0 22 2
A getOptions() 0 6 1
A resolveStubPath() 0 5 2
A alreadyExists() 0 4 2
A getPath() 0 6 1
A getStub() 0 3 1
A getDefaultNamespace() 0 3 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:deleteRequest')]
11
class MagicDeleteRequestCommand extends GeneratorCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'magic:deleteRequest';
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:deleteRequest';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create a new magic request class';
37
38
    /**
39
     * The type of class being generated.
40
     *
41
     * @var string
42
     */
43
    protected $type = 'DeleteRequest';
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 DeleteRequest already exists");
55
            return;
56
        }
57
58
        $model = $name;
59
        $modelVariable = $this->option('variable');
60
        $modelUnderScore = $this->option('underscore');
61
        $name = "{$name}DeleteRequest";
62
63
        $stub = str_replace(
64
            ['DummyModel', '{{ model }}'], class_basename($model), parent::buildClass($name)
65
        );
66
67
        $stub = str_replace(
68
            ['DummyModelVariable', '{{ modelVariable }}'], trim($modelVariable, '\\'), $stub
69
        );
70
71
        return str_replace(
72
            ['DummyModelUnderScore', '{{ modelUnderScore }}'], trim($modelUnderScore, '\\'), $stub
73
        );
74
75
    }
76
77
    /**
78
     * Determine if the class already exists.
79
     *
80
     * @param  string  $rawName
81
     * @return bool
82
     */
83
    protected function alreadyExists($rawName)
84
    {
85
        return class_exists($rawName) ||
86
               $this->files->exists($this->getPath($this->qualifyClass($rawName)));
87
    }
88
89
    /**
90
     * Get the stub file for the generator.
91
     *
92
     * @return string
93
     */
94
    protected function getStub()
95
    {
96
        return __DIR__.'/stubs/deleteRequest.stub';
97
    }
98
99
    /**
100
     * Resolve the fully-qualified path to the stub.
101
     *
102
     * @param  string  $stub
103
     * @return string
104
     */
105
    protected function resolveStubPath($stub)
106
    {
107
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
108
                        ? $customPath
109
                        : __DIR__.$stub;
110
    }
111
112
    /**
113
     * Get the default namespace for the class.
114
     *
115
     * @param  string  $rootNamespace
116
     * @return string
117
     */
118
    protected function getDefaultNamespace($rootNamespace)
119
    {
120
        return 'App\Http\Requests';
121
    }
122
123
    /**
124
     * Get the console command options.
125
     *
126
     * @return array
127
     */
128
    protected function getOptions()
129
    {
130
        return [
131
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the request already exists'],
132
            ['variable', 'var', InputOption::VALUE_REQUIRED, 'Create a model variable value for this request class'],
133
            ['underscore', 'u', InputOption::VALUE_REQUIRED, 'Create a model underscore value for this request class'],
134
        ];
135
    }
136
137
    /**
138
     * Get the destination class path.
139
     *
140
     * @param  string  $name
141
     * @return string
142
     */
143
    protected function getPath($name)
144
    {
145
        $name = "{$name}DeleteRequest";
146
        $name = Str::replaceFirst($this->rootNamespace(), '', $name);
147
148
        return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
149
    }
150
}
151