ModelMakeCommand::createMigration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace JunaidQadirB\Cray\Console\Commands;
4
5
6
use Illuminate\Support\Str;
7
use JunaidQadirB\Cray\Console\Contracts\GeneratorCommand;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class ModelMakeCommand extends GeneratorCommand
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'cray:model';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create a new Eloquent model class';
25
26
    /**
27
     * The type of class being generated.
28
     *
29
     * @var string
30
     */
31
    protected $type = 'Model';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return void
37
     */
38
    public function handle()
39
    {
40
        if (parent::handle() === false && !$this->option('force')) {
41
            return;
42
        }
43
        if ($this->option('all')) {
44
            $this->input->setOption('factory', true);
45
            $this->input->setOption('migration', true);
46
            $this->input->setOption('controller', true);
47
            $this->input->setOption('resource', true);
48
        }
49
50
        if ($this->option('factory')) {
51
            $this->createFactory();
52
        }
53
54
        if ($this->option('migration')) {
55
            $this->createMigration();
56
        }
57
58
        if ($this->option('controller') || $this->option('resource')) {
59
            $this->createController();
60
        }
61
    }
62
63
    /**
64
     * Create a model factory for the model.
65
     *
66
     * @return void
67
     */
68 View Code Duplication
    protected function createFactory()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $factory = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, class_basename() does only seem to accept string|object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
71
72
        $this->call('cray:factory', [
73
            'name' => "{$factory}Factory",
74
            '--model' => $this->argument('name'),
75
        ]);
76
    }
77
78
    /**
79
     * Create a migration file for the model.
80
     *
81
     * @return void
82
     */
83 View Code Duplication
    protected function createMigration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $table = Str::plural(Str::snake(class_basename($this->argument('name'))));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, class_basename() does only seem to accept string|object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
87
        $this->call('cray:migration', [
88
            'name' => "create_{$table}_table",
89
            '--create' => $table,
90
        ]);
91
    }
92
93
    /**
94
     * Create a controller for the model.
95
     *
96
     * @return void
97
     */
98
    protected function createController()
99
    {
100
        $controller = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, class_basename() does only seem to accept string|object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

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