shouldGeneratePermissionsMigration()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Savannabits\JetstreamInertiaGenerator;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class JetstreamInertiaGenerator extends Command
11
{
12
    protected $name = "jig:generate";
13
    protected $description ="Scaffold a whole CRUD module";
14
    protected $files;
15
16
    public function handle(Filesystem $files) {
17
        $this->files = $files;
18
19
        $tableNameArgument = $this->argument('table_name');
20
        $modelOption = $this->option('model-name');
21
        $controllerOption = $this->option('controller-name');
22
        $repositoryOption = $this->option('repository-name');
23
        $policyOption = $this->option('policy-name');
24
        $exportOption = $this->option('with-export');
25
        $withoutBulkOptions = $this->option('without-bulk');
26
        $force = $this->option('force');
27
        $template = $this->option('template');
28
29
        $this->call('jig:generate:model', [
30
            'table_name' => $tableNameArgument,
31
            'class_name' => $modelOption,
32
            '--force' => $force,
33
            '--template' => $template,
34
        ]);
35
36
        /*$this->call('jig:generate:factory', [
37
            'table_name' => $tableNameArgument,
38
            '--model-name' => $modelOption,
39
            '--seed' => $this->option('seed'),
40
        ]);*/
41
42
        $this->call('jig:generate:request:index', [
43
            'table_name' => $tableNameArgument,
44
            '--model-name' => $modelOption,
45
            '--force' => $force,
46
            '--template' => $template,
47
        ]);
48
49
        $this->call('jig:generate:request:store', [
50
            'table_name' => $tableNameArgument,
51
            '--model-name' => $modelOption,
52
            '--force' => $force,
53
            '--template' => $template,
54
        ]);
55
56
        $this->call('jig:generate:request:update', [
57
            'table_name' => $tableNameArgument,
58
            '--model-name' => $modelOption,
59
            '--force' => $force,
60
            '--template' => $template,
61
        ]);
62
63
        $this->call('jig:generate:request:destroy', [
64
            'table_name' => $tableNameArgument,
65
            '--model-name' => $modelOption,
66
            '--force' => $force,
67
        ]);
68
69
        $this->call('jig:generate:repository', [
70
            'table_name' => $tableNameArgument,
71
            'class_name' => $repositoryOption,
72
            '--model-name' => $modelOption,
73
            '--force' => $force,
74
            '--with-export' => $exportOption,
75
            '--without-bulk' => $withoutBulkOptions,
76
            '--template' => $template,
77
        ]);
78
79
        $this->call('jig:generate:api:controller', [
80
            'table_name' => $tableNameArgument,
81
            'class_name' => $controllerOption,
82
            '--model-name' => $modelOption,
83
            '--force' => $force,
84
            '--with-export' => $exportOption,
85
            '--without-bulk' => $withoutBulkOptions,
86
            '--template' => $template,
87
        ]);
88
        $this->call('jig:generate:controller', [
89
            'table_name' => $tableNameArgument,
90
            'class_name' => $controllerOption,
91
            '--model-name' => $modelOption,
92
            '--force' => $force,
93
            '--with-export' => $exportOption,
94
            '--without-bulk' => $withoutBulkOptions,
95
            '--template' => $template,
96
        ]);
97
98
99
         $this->call('jig:generate:routes', [
100
            'table_name' => $tableNameArgument,
101
            '--model-name' => $modelOption,
102
            '--controller-name' => $controllerOption,
103
            '--with-export' => $exportOption,
104
            '--without-bulk' => $withoutBulkOptions,
105
             '--template' => $template,
106
        ]);
107
108
109
        $this->call('jig:generate:api:routes', [
110
            'table_name' => $tableNameArgument,
111
            '--model-name' => $modelOption,
112
            '--controller-name' => $controllerOption,
113
            '--with-export' => $exportOption,
114
            '--without-bulk' => $withoutBulkOptions,
115
            '--template' => $template,
116
        ]);
117
118
        $this->call('jig:generate:index', [
119
            'table_name' => $tableNameArgument,
120
            '--model-name' => $modelOption,
121
            '--force' => $force,
122
            '--with-export' => $exportOption,
123
            '--without-bulk' => $withoutBulkOptions,
124
            '--template' => $template,
125
        ]);
126
127
        $this->call('jig:generate:form', [
128
            'table_name' => $tableNameArgument,
129
            '--model-name' => $modelOption,
130
            '--force' => $force,
131
            '--template' => $template,
132
        ]);
133
134
        /*
135
        $this->call('jig:generate:lang', [
136
            'table_name' => $tableNameArgument,
137
            '--model-name' => $modelOption,
138
            '--with-export' => $exportOption,
139
        ]);
140
141
142
        if($exportOption){
143
            $this->call('jig:generate:export', [
144
                'table_name' => $tableNameArgument,
145
                '--force' => $force,
146
            ]);
147
        }
148
149
        */
150
        $this->call('jig:generate:policy', [
151
            'table_name' => $tableNameArgument,
152
            'class_name' => $policyOption,
153
            '--model-name' => $modelOption,
154
            '--force' => $force,
155
            '--with-export' => false,
156
            '--without-bulk' => false,
157
            '--template' => $template,
158
        ]);
159
160
        if ($this->shouldGeneratePermissionsMigration()) {
161
            $this->call('jig:generate:permissions', [
162
                'table_name' => $tableNameArgument,
163
                '--model-name' => $modelOption,
164
                '--force' => $force,
165
                '--without-bulk' => $withoutBulkOptions,
166
            ]);
167
168
            if ($this->option('no-interaction') || $this->confirm('Do you want to attach generated permissions to the default role now?', true)) {
169
                $this->call('migrate');
170
            }
171
        }
172
173
        $this->info('Generating whole admin CRUD module finished');
174
    }
175
176
    protected function getArguments() {
177
        return [
178
            ['table_name', InputArgument::REQUIRED, 'Name of the existing table'],
179
        ];
180
    }
181
182
    protected function getOptions() {
183
        return [
184
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom model name'],
185
            ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Specify custom model name'],
186
            ['controller-name', 'c', InputOption::VALUE_OPTIONAL, 'Specify custom controller name'],
187
            ['repository-name', 'r', InputOption::VALUE_OPTIONAL, 'Specify custom repository class name'],
188
            ['policy-name', 'p', InputOption::VALUE_OPTIONAL, 'Specify custom Policy class name'],
189
            ['seed', 's', InputOption::VALUE_NONE, 'Seeds the table with fake data'],
190
            ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating admin'],
191
            ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'],
192
            ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'],
193
        ];
194
    }
195
196
    protected function shouldGeneratePermissionsMigration() {
197
        if (class_exists('\Savannabits\JetstreamInertiaGenerator\JetstreamInertiaGenerator')) {
198
            return true;
199
        }
200
201
        return false;
202
    }
203
}
204