Cray::createViews()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace JunaidQadirB\Cray\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use JunaidQadirB\Cray\Console\Contracts\GeneratorCommand;
7
8
class Cray extends GeneratorCommand
9
{
10
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'cray';
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'cray
23
    {name : Model name. Controller, factory, migration, views will be based on this name.}
24
    {--views-dir= : Place views in a sub-directory under the views directory. It can be any nested directory structure}
25
    {--controller-dir= : Place controller in a sub-directory under the Http/Controllers directory. It can be any nested directory structure}
26
    {--stubs-dir= : Specify a custom stubs directory}
27
    {--no-views : Do not create view files for the model}
28
    {--no-migration : Do not create a migration for the model}
29
    {--no-factory : Do not create a factory for the model}
30
    ';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Scaffold a nearly complete boilerplate CRUD pages for the specified Model';
38
39
    /**
40
     * Create a new command instance.
41
     *
42
     */
43
    /*    public function __construct()
44
        {
45
            parent::__construct();
46
        }*/
47
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return mixed
52
     */
53
    public function handle()
54
    {
55
        /**
56
         * Steps
57
         * - Generate Model
58
         * - Generate Factory
59
         * - Generate Migration
60
         * - Generate Controller
61
         * - Generate Requests
62
         * - Generate Views
63
         *
64
         */
65
        $this->type = 'Model';
66
67
        if (!$this->option('no-factory')) {
68
            $this->createFactory();
69
        }
70
71
        if (!$this->option('no-migration')) {
72
            $this->createMigration();
73
        }
74
        $this->createController();
75
76
        if (!$this->option('no-views')) {
77
            $this->createViews();
78
        }
79
80
        $this->type = 'Request';
81
82
        $this->createRequest('Store');
83
84
        $this->createRequest('Update');
85
86
87
    }
88
89
    /**
90
     * Create a model factory for the model.
91
     *
92
     * @return void
93
     */
94 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...
95
    {
96
        $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...
97
98
        $this->call('cray:factory', [
99
            'name' => "{$factory}Factory",
100
            '--model' => $this->argument('name'),
101
        ]);
102
    }
103
104
    /**
105
     * Create a migration file for the model.
106
     *
107
     * @return void
108
     */
109 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...
110
    {
111
        $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...
112
113
        $this->call('cray:migration', [
114
            'name' => "create_{$table}_table",
115
            '--create' => $table,
116
        ]);
117
    }
118
119
    /**
120
     * Create a controller for the model.
121
     *
122
     * @return void
123
     */
124
    protected function createController()
125
    {
126
127
        $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...
128
129
        $modelName = $this->qualifyClass($this->getNameInput());
130
131
        $args = [
132
            'name' => "{$controller}Controller",
133
            '--model' => $modelName,
134
        ];
135
136
        $viewsDir = $this->option('views-dir');
137
        if ($viewsDir) {
138
            $args['--views-dir'] = $viewsDir;
139
        }
140
141
        $controllerDir = $this->option('controller-dir');
142
        if ($controllerDir) {
143
            $args['--controller-dir'] = $controllerDir;
144
        }
145
146
        $this->call('cray:controller', $args);
147
    }
148
149
    protected function createViews()
150
    {
151
        $name = $this->argument('name');
152
        $args = [
153
            'name' => $name,
154
            '--all' => true,
155
        ];
156
157
        $dir = $this->option('views-dir');
158
        if ($dir) {
159
            $args['--dir'] = $dir;
160
        }
161
162
        $stub = $this->option('stubs-dir');
163
        if ($stub) {
164
            $args['--stubs'] = $stub;
165
        }
166
        $this->call('cray:view', $args);
167
168
169
    }
170
171
    /**
172
     * Create a controller for the model.
173
     *
174
     * @param string $requestType
175
     *
176
     * @return void
177
     */
178
    protected function createRequest($requestType = 'Store')
179
    {
180
        $model = 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...
181
        $name = "{$model}{$requestType}Request";
182
        $this->call('cray:request', [
183
            'name' => $name,
184
            '--model' => $model,
185
            '--type' => Str::slug($requestType),
186
        ]);
187
    }
188
189
    /**
190
     * Get the stub file for the generator.
191
     *
192
     * @return string
193
     */
194
    protected function getStub()
195
    {
196
        return config('cray.stubs_dir') . '/' . Str::slug($this->type) . '.stub';
197
    }
198
199
    /**
200
     * Get the default namespace for the class.
201
     *
202
     * @param string $rootNamespace
203
     *
204
     * @return string
205
     */
206
    protected function getDefaultNamespace($rootNamespace)
207
    {
208
        switch ($this->type) {
209
            case 'Request':
210
                return $rootNamespace . '\Http\Requests';
211
        }
212
213
        return $rootNamespace;
214
    }
215
}
216