Issues (27)

src/Console/Commands/ModelMakeCommand.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Larafast\Fastapi\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use Larafast\Fastapi\Console\Contracts\GeneratorCommand;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class ModelMakeCommand extends GeneratorCommand
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'fastApi:model';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Create a new Eloquent model class';
24
25
    /**
26
     * The type of class being generated.
27
     *
28
     * @var string
29
     */
30
    protected $type = 'Model';
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return void
36
     */
37
    public function handle()
38
    {
39
        if (parent::handle() === false && !$this->option('force')) {
40
            return;
41
        }
42
        if ($this->option('all')) {
43
            $this->input->setOption('factory', true);
44
            $this->input->setOption('migration', true);
45
            $this->input->setOption('controller', true);
46
            $this->input->setOption('resource', true);
47
        }
48
49
        if ($this->option('factory')) {
50
            $this->createFactory();
51
        }
52
53
        if ($this->option('migration')) {
54
            $this->createMigration();
55
        }
56
57
        if ($this->option('controller') || $this->option('resource')) {
58
            $this->createController();
59
        }
60
    }
61
62
    /**
63
     * Create a model factory for the model.
64
     *
65
     * @return void
66
     */
67
    protected function createFactory()
68
    {
69
        $factory = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|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

69
        $factory = Str::studly(class_basename(/** @scrutinizer ignore-type */ $this->argument('name')));
Loading history...
70
71
        $this->call('fastApi:factory', [
72
            'name' => "{$factory}Factory",
73
            '--model' => $this->argument('name'),
74
        ]);
75
    }
76
77
    /**
78
     * Create a migration file for the model.
79
     *
80
     * @return void
81
     */
82
    protected function createMigration()
83
    {
84
        $table = Str::plural(Str::snake(class_basename($this->argument('name'))));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|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

84
        $table = Str::plural(Str::snake(class_basename(/** @scrutinizer ignore-type */ $this->argument('name'))));
Loading history...
85
86
        $this->call('fastApi:migration', [
87
            'name' => "create_{$table}_table",
88
            '--create' => $table,
89
        ]);
90
    }
91
92
    /**
93
     * Create a controller for the model.
94
     *
95
     * @return void
96
     */
97
    protected function createController()
98
    {
99
        $controller = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|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

99
        $controller = Str::studly(class_basename(/** @scrutinizer ignore-type */ $this->argument('name')));
Loading history...
100
101
        $modelName = $this->qualifyClass($this->getNameInput());
102
103
        $this->call('fastApi:controller', [
104
            'name' => "{$controller}Controller",
105
            '--model' => $this->option('resource') ? $modelName : null,
106
        ]);
107
    }
108
109
    /**
110
     * Get the stub file for the generator.
111
     *
112
     * @return string
113
     */
114
    protected function getStub()
115
    {
116
        if ($this->option('pivot')) {
117
            return config('fastApi.stubs_dir').'/pivot.model.stub';
118
        }
119
120
        return config('fastApi.stubs_dir').'/model.stub';
121
    }
122
123
    /**
124
     * Get the default namespace for the class.
125
     *
126
     * @param string $rootNamespace
127
     *
128
     * @return string
129
     */
130
    protected function getDefaultNamespace($rootNamespace)
131
    {
132
        return $rootNamespace;
133
    }
134
135
    /**
136
     * Get the console command options.
137
     *
138
     * @return array
139
     */
140
    protected function getOptions()
141
    {
142
        return [
143
            [
144
                'all',
145
                'a',
146
                InputOption::VALUE_NONE,
147
                'Generate a migration, factory, and resource controller for the model',
148
            ],
149
150
            ['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'],
151
152
            ['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'],
153
154
            ['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists.'],
155
156
            ['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model.'],
157
158
            [
159
                'pivot',
160
                'p',
161
                InputOption::VALUE_NONE,
162
                'Indicates if the generated model should be a custom intermediate table model.',
163
            ],
164
165
            [
166
                'resource',
167
                'r',
168
                InputOption::VALUE_NONE,
169
                'Indicates if the generated controller should be a resource controller.',
170
            ],
171
        ];
172
    }
173
}
174