MagicModelCommand::alreadyExists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 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:model')]
11
class MagicModelCommand extends GeneratorCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'magic:model';
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:model';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create a new magic model class';
37
38
    /**
39
     * The type of class being generated.
40
     *
41
     * @var string
42
     */
43
    protected $type = 'Model';
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($name)) {
54
            $this->components->error("This model already exists");
55
            return;
56
        }
57
58
        $ary = explode("\\", $name);
59
        $model = $ary[count($ary) - 1];
60
        $modelVariable = lcfirst($model);
61
        $modelUnderScore = Str::snake($modelVariable);
62
63
        $this->callSilently("make:migration", ['name' => "create_{$modelUnderScore}s_table"]);
64
        $this->components->info("Migration scaffolding for {$model} model generated successfully.");
65
66
        $this->callSilently("magic:contract", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
67
        $this->components->info("Magic contract scaffolding for {$model} model generated successfully.");
68
69
        $this->callSilently("magic:repository", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
70
        $this->components->info("Magic repository scaffolding for {$model} model generated successfully.");
71
72
        $this->callSilently("magic:service", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
73
        $this->components->info("Magic service scaffolding for {$model} model generated successfully.");
74
75
        $this->callSilently("magic:controller", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
76
        $this->components->info("Magic controller scaffolding for {$model} model generated successfully.");
77
78
        $this->callSilently("magic:createRequest", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
79
        $this->components->info("Magic create request scaffolding for {$model} model generated successfully.");
80
81
        $this->callSilently("magic:updateRequest", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
82
        $this->components->info("Magic update request scaffolding for {$model} model generated successfully.");
83
84
        $this->callSilently("magic:deleteRequest", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
85
        $this->components->info("Magic delete request scaffolding for {$model} model generated successfully.");
86
87
        $this->callSilently("magic:readRequest", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
88
        $this->components->info("Magic read request scaffolding for {$model} model generated successfully.");
89
90
        $this->callSilently("magic:api", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
91
        $this->components->info("Magic api route scaffolding for {$model} model generated successfully.");
92
93
        $this->callSilently("magic:test", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
94
        $this->components->info("Magic test scaffolding for {$model} model generated successfully.");
95
96
        $this->callSilently("magic:factory", ['name' => "{$model}", '--variable' => $modelVariable, '--underscore' => $modelUnderScore]);
97
        $this->components->info("Magic factory scaffolding for {$model} model generated successfully.");
98
99
        $stub = str_replace(
100
            ['DummyModel', '{{ model }}'], class_basename($model), parent::buildClass($name)
101
        );
102
103
        $stub = str_replace(
104
            ['DummyModelVariable', '{{ modelVariable }}'], trim($modelVariable, '\\'), $stub
105
        );
106
107
        return str_replace(
108
            ['DummyModelUnderScore', '{{ modelUnderScore }}'], trim($modelUnderScore, '\\'), $stub
109
        );
110
    }
111
112
    /**
113
     * Determine if the class already exists.
114
     *
115
     * @param  string  $rawName
116
     * @return bool
117
     */
118
    protected function alreadyExists($rawName)
119
    {
120
        return class_exists($rawName) ||
121
               $this->files->exists($this->getPath($this->qualifyClass($rawName)));
122
    }
123
124
    /**
125
     * Get the stub file for the generator.
126
     *
127
     * @return string
128
     */
129
    protected function getStub()
130
    {
131
        return __DIR__.'/stubs/model.stub';
132
    }
133
134
    /**
135
     * Resolve the fully-qualified path to the stub.
136
     *
137
     * @param  string  $stub
138
     * @return string
139
     */
140
    protected function resolveStubPath($stub)
141
    {
142
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
143
                        ? $customPath
144
                        : __DIR__.$stub;
145
    }
146
147
    /**
148
     * Get the default namespace for the class.
149
     *
150
     * @param  string  $rootNamespace
151
     * @return string
152
     */
153
    protected function getDefaultNamespace($rootNamespace)
154
    {
155
        return $rootNamespace.'\Models';
156
    }
157
158
    /**
159
     * Get the console command options.
160
     *
161
     * @return array
162
     */
163
    protected function getOptions()
164
    {
165
        return [
166
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the model already exists'],
167
        ];
168
    }
169
}
170