Passed
Push — 1.2 ( ac7648...d64a15 )
by Quentin
05:40
created

ModuleMake::createModels()   D

Complexity

Conditions 11
Paths 384

Size

Total Lines 74
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 42
c 0
b 0
f 0
nc 384
nop 2
dl 0
loc 74
rs 4.1833

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Config\Repository as Config;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Composer;
10
use Illuminate\Support\Str;
11
12
class ModuleMake extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'twill:module {moduleName}
20
        {--B|hasBlocks}
21
        {--T|hasTranslation}
22
        {--S|hasSlug}
23
        {--M|hasMedias}
24
        {--F|hasFiles}
25
        {--P|hasPosition}
26
        {--R|hasRevisions}
27
        {--all}';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new Twill Module';
35
36
    /**
37
     * @var Filesystem
38
     */
39
    protected $files;
40
41
    /**
42
     * @var Composer
43
     */
44
    protected $composer;
45
46
    /**
47
     * @var string[]
48
     */
49
    protected $modelTraits;
50
51
    /**
52
     * @var string[]
53
     */
54
    protected $repositoryTraits;
55
56
    /**
57
     * @var Config
58
     */
59
    protected $config;
60
61
    /**
62
     * @param Filesystem $files
63
     * @param Composer $composer
64
     * @param Config $config
65
     */
66
    public function __construct(Filesystem $files, Composer $composer, Config $config)
67
    {
68
        parent::__construct();
69
70
        $this->files = $files;
71
        $this->composer = $composer;
72
        $this->config = $config;
73
74
        $this->blockable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property blockable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
        $this->translatable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property translatable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
        $this->sluggable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property sluggable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
        $this->mediable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property mediable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
        $this->fileable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property fileable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
        $this->sortable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property sortable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
        $this->revisionable = false;
0 ignored issues
show
Bug Best Practice introduced by
The property revisionable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
82
        $this->defaultsAnswserToNo = false;
0 ignored issues
show
Bug Best Practice introduced by
The property defaultsAnswserToNo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
84
        $this->modelTraits = ['HasBlocks', 'HasTranslation', 'HasSlug', 'HasMedias', 'HasFiles', 'HasRevisions', 'HasPosition'];
85
        $this->repositoryTraits = ['HandleBlocks', 'HandleTranslations', 'HandleSlugs', 'HandleMedias', 'HandleFiles', 'HandleRevisions'];
86
    }
87
88
    /**
89
     * Executes the console command.
90
     *
91
     * @return mixed
92
     */
93
    public function handle()
94
    {
95
        $moduleName = Str::plural(lcfirst($this->argument('moduleName')));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('moduleName') can also be of type string[]; however, parameter $str of lcfirst() does only seem to accept 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

95
        $moduleName = Str::plural(lcfirst(/** @scrutinizer ignore-type */ $this->argument('moduleName')));
Loading history...
96
97
        $enabledOptions = Collection::make($this->options())->only([
98
            'hasBlocks',
99
            'hasTranslation',
100
            'hasSlug',
101
            'hasMedias',
102
            'hasFiles',
103
            'hasPosition',
104
            'hasRevisions',
105
        ])->filter(function ($enabled) {
106
            return $enabled;
107
        });
108
109
        if (count($enabledOptions) > 0) {
110
            $this->defaultsAnswserToNo = true;
0 ignored issues
show
Bug Best Practice introduced by
The property defaultsAnswserToNo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
        }
112
113
        $this->blockable = $this->checkOption('hasBlocks');
0 ignored issues
show
Bug Best Practice introduced by
The property blockable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        $this->translatable = $this->checkOption('hasTranslation');
0 ignored issues
show
Bug Best Practice introduced by
The property translatable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
115
        $this->sluggable = $this->checkOption('hasSlug');
0 ignored issues
show
Bug Best Practice introduced by
The property sluggable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
116
        $this->mediable = $this->checkOption('hasMedias');
0 ignored issues
show
Bug Best Practice introduced by
The property mediable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
        $this->fileable = $this->checkOption('hasFiles');
0 ignored issues
show
Bug Best Practice introduced by
The property fileable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
118
        $this->sortable = $this->checkOption('hasPosition');
0 ignored issues
show
Bug Best Practice introduced by
The property sortable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
        $this->revisionable = $this->checkOption('hasRevisions');
0 ignored issues
show
Bug Best Practice introduced by
The property revisionable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
120
121
        $activeTraits = [
122
            $this->blockable,
123
            $this->translatable,
124
            $this->sluggable,
125
            $this->mediable,
126
            $this->fileable,
127
            $this->revisionable,
128
            $this->sortable,
129
        ];
130
131
        $modelName = Str::studly(Str::singular($moduleName));
132
133
        $this->createMigration($moduleName);
134
        $this->createModels($modelName, $activeTraits);
135
        $this->createRepository($modelName, $activeTraits);
136
        $this->createController($moduleName, $modelName);
137
        $this->createRequest($modelName);
138
        $this->createViews($moduleName);
139
140
        $this->info("Add Route::module('{$moduleName}'); to your admin routes file.");
141
        $this->info("Setup a new CMS menu item in config/twill-navigation.php:");
142
143
        $navTitle = Str::studly($moduleName);
144
        $this->info("
145
            '{$moduleName}' => [
146
                'title' => '{$navTitle}',
147
                'module' => true
148
            ]
149
        ");
150
151
        $this->info("Migrate your database.\n");
152
153
        $this->info("Enjoy.");
154
155
        $this->composer->dumpAutoloads();
156
    }
157
158
    /**
159
     * Creates a new module database migration file.
160
     *
161
     * @param string $moduleName
162
     * @return void
163
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
164
     */
165
    private function createMigration($moduleName = 'items')
166
    {
167
        $table = Str::snake($moduleName);
168
        $tableClassName = Str::studly($table);
169
170
        $className = "Create{$tableClassName}Tables";
0 ignored issues
show
Unused Code introduced by
The assignment to $className is dead and can be removed.
Loading history...
171
172
        $migrationName = 'create_' . $table . '_tables';
173
174
        if (!count(glob(database_path('migrations/*' . $migrationName . '.php')))) {
0 ignored issues
show
Bug introduced by
It seems like glob(database_path('migr...igrationName . '.php')) can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, 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

174
        if (!count(/** @scrutinizer ignore-type */ glob(database_path('migrations/*' . $migrationName . '.php')))) {
Loading history...
175
            $migrationPath = $this->laravel->databasePath() . '/migrations';
176
177
            $fullPath = $this->laravel['migration.creator']->create($migrationName, $migrationPath);
178
179
            $stub = str_replace(
180
                ['{{table}}', '{{singularTableName}}', '{{tableClassName}}'],
181
                [$table, Str::singular($table), $tableClassName],
182
                $this->files->get(__DIR__ . '/stubs/migration.stub')
183
            );
184
185
            if ($this->translatable) {
186
                $stub = preg_replace('/{{!hasTranslation}}[\s\S]+?{{\/!hasTranslation}}/', '', $stub);
187
            } else {
188
                $stub = str_replace([
189
                    '{{!hasTranslation}}',
190
                    '{{/!hasTranslation}}',
191
                ], '', $stub);
192
            }
193
194
            $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable);
195
            $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable);
196
            $stub = $this->renderStubForOption($stub, 'hasRevisions', $this->revisionable);
197
            $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable);
198
199
            $stub = preg_replace('/\}\);[\s\S]+?Schema::create/', "});\n\n        Schema::create", $stub);
200
201
            twill_put_stub($fullPath, $stub);
202
203
            $this->info("Migration created successfully! Add some fields!");
204
        }
205
    }
206
207
    /**
208
     * Creates new model class files for the given model name and traits.
209
     *
210
     * @param string $modelName
211
     * @param array $activeTraits
212
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
213
     */
214
    private function createModels($modelName = 'Item', $activeTraits = [])
215
    {
216
        make_twill_directory('Models');
217
218
        if ($this->translatable) {
219
            make_twill_directory('Models/Translations');
220
221
            $modelTranslationClassName = $modelName . 'Translation';
222
223
            $stub = str_replace('{{modelTranslationClassName}}', $modelTranslationClassName, $this->files->get(__DIR__ . '/stubs/model_translation.stub'));
224
225
            twill_put_stub(twill_path('Models/Translations/' . $modelTranslationClassName . '.php'), $stub);
226
        }
227
228
        if ($this->sluggable) {
229
            make_twill_directory('Models/Slugs');
230
231
            $modelSlugClassName = $modelName . 'Slug';
232
233
            $stub = str_replace(['{{modelSlugClassName}}', '{{modelName}}'], [$modelSlugClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_slug.stub'));
234
235
            twill_put_stub(twill_path('Models/Slugs/' . $modelSlugClassName . '.php'), $stub);
236
        }
237
238
        if ($this->revisionable) {
239
            make_twill_directory('Models/Revisions');
240
241
            $modelRevisionClassName = $modelName . 'Revision';
242
243
            $stub = str_replace(['{{modelRevisionClassName}}', '{{modelName}}'], [$modelRevisionClassName, Str::snake($modelName)], $this->files->get(__DIR__ . '/stubs/model_revision.stub'));
244
245
            twill_put_stub(twill_path('Models/Revisions/' . $modelRevisionClassName . '.php'), $stub);
246
        }
247
248
        $modelClassName = $modelName;
249
250
        $activeModelTraits = [];
251
252
        foreach ($activeTraits as $index => $traitIsActive) {
253
            if ($traitIsActive) {
254
                !isset($this->modelTraits[$index]) ?: $activeModelTraits[] = $this->modelTraits[$index];
255
            }
256
        }
257
258
        $activeModelTraitsString = empty($activeModelTraits) ? '' : 'use ' . rtrim(implode(', ', $activeModelTraits), ', ') . ';';
259
260
        $activeModelTraitsImports = empty($activeModelTraits) ? '' : "use A17\Twill\Models\Behaviors\\" . implode(";\nuse A17\Twill\Models\Behaviors\\", $activeModelTraits) . ";";
261
262
        $activeModelImplements = $this->sortable ? 'implements Sortable' : '';
263
264
        if ($this->sortable) {
265
            $activeModelTraitsImports .= "\nuse A17\Twill\Models\Behaviors\Sortable;";
266
        }
267
268
        $stub = str_replace([
269
            '{{modelClassName}}',
270
            '{{modelTraits}}',
271
            '{{modelImports}}',
272
            '{{modelImplements}}',
273
        ], [
274
            $modelClassName,
275
            $activeModelTraitsString,
276
            $activeModelTraitsImports,
277
            $activeModelImplements,
278
        ], $this->files->get(__DIR__ . '/stubs/model.stub'));
279
280
        $stub = $this->renderStubForOption($stub, 'hasTranslation', $this->translatable);
281
        $stub = $this->renderStubForOption($stub, 'hasSlug', $this->sluggable);
282
        $stub = $this->renderStubForOption($stub, 'hasMedias', $this->mediable);
283
        $stub = $this->renderStubForOption($stub, 'hasPosition', $this->sortable);
284
285
        twill_put_stub(twill_path('Models/' . $modelClassName . '.php'), $stub);
286
287
        $this->info("Models created successfully! Fill your fillables!");
288
    }
289
290
    private function renderStubForOption($stub, $option, $enabled)
291
    {
292
        if ($enabled) {
293
            $stub = str_replace([
294
                '{{' . $option . '}}',
295
                '{{/' . $option . '}}',
296
            ], '', $stub);
297
        } else {
298
            $stub = preg_replace('/{{' . $option . '}}[\s\S]+?{{\/' . $option . '}}/', '', $stub);
299
        }
300
301
        return $stub;
302
    }
303
304
    /**
305
     * Creates new repository class file for the given model name.
306
     *
307
     * @param string $modelName
308
     * @param array $activeTraits
309
     * @return void
310
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
311
     */
312
    private function createRepository($modelName = 'Item', $activeTraits = [])
313
    {
314
        make_twill_directory('Repositories');
315
316
        $repositoryClassName = $modelName . 'Repository';
317
318
        $activeRepositoryTraits = [];
319
320
        foreach ($activeTraits as $index => $traitIsActive) {
321
            if ($traitIsActive) {
322
                !isset($this->repositoryTraits[$index]) ?: $activeRepositoryTraits[] = $this->repositoryTraits[$index];
323
            }
324
        }
325
326
        $activeRepositoryTraitsString = empty($activeRepositoryTraits) ? '' : 'use ' . (empty($activeRepositoryTraits) ? "" : rtrim(implode(', ', $activeRepositoryTraits), ', ') . ';');
327
328
        $activeRepositoryTraitsImports = empty($activeRepositoryTraits) ? '' : "use A17\Twill\Repositories\Behaviors\\" . implode(";\nuse A17\Twill\Repositories\Behaviors\\", $activeRepositoryTraits) . ";";
329
330
        $stub = str_replace(['{{repositoryClassName}}', '{{modelName}}', '{{repositoryTraits}}', '{{repositoryImports}}'], [$repositoryClassName, $modelName, $activeRepositoryTraitsString, $activeRepositoryTraitsImports], $this->files->get(__DIR__ . '/stubs/repository.stub'));
331
332
        twill_put_stub(twill_path('Repositories/' . $repositoryClassName . '.php'), $stub);
333
334
        $this->info("Repository created successfully! Control all the things!");
335
    }
336
337
    /**
338
     * Create a new controller class file for the given module name and model name.
339
     *
340
     * @param string $moduleName
341
     * @param string $modelName
342
     * @return void
343
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
344
     */
345
    private function createController($moduleName = 'items', $modelName = 'Item')
346
    {
347
        make_twill_directory('Http/Controllers/Admin');
348
349
        $controllerClassName = $modelName . 'Controller';
350
351
        $stub = str_replace(
352
            ['{{moduleName}}', '{{controllerClassName}}'],
353
            [$moduleName, $controllerClassName],
354
            $this->files->get(__DIR__ . '/stubs/controller.stub')
355
        );
356
357
        twill_put_stub(twill_path('Http/Controllers/Admin/' . $controllerClassName . '.php'), $stub);
358
359
        $this->info("Controller created successfully! Define your index/browser/form endpoints options!");
360
    }
361
362
    /**
363
     * Creates a new request class file for the given model name.
364
     *
365
     * @param string $modelName
366
     * @return void
367
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
368
     */
369
    private function createRequest($modelName = 'Item')
370
    {
371
        make_twill_directory('Http/Requests/Admin');
372
373
        $requestClassName = $modelName . 'Request';
374
375
        $stub = str_replace('{{requestClassName}}', $requestClassName, $this->files->get(__DIR__ . '/stubs/request.stub'));
376
377
        twill_put_stub(twill_path('Http/Requests/Admin/' . $requestClassName . '.php'), $stub);
378
379
        $this->info("Form request created successfully! Add some validation rules!");
380
    }
381
382
    /**
383
     * Creates appropriate module Blade view files.
384
     *
385
     * @param string $moduleName
386
     * @return void
387
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
388
     */
389
    private function createViews($moduleName = 'items')
390
    {
391
        $viewsPath = $this->config->get('view.paths')[0] . '/admin/' . $moduleName;
392
393
        make_twill_directory($viewsPath);
394
395
        $formView = $this->translatable ? 'form_translatable' : 'form';
396
397
        twill_put_stub($viewsPath . '/form.blade.php', $this->files->get(__DIR__ . '/stubs/' . $formView . '.blade.stub'));
398
399
        $this->info("Form view created successfully! Include your form fields using @formField directives!");
400
    }
401
402
    private function checkOption($option)
403
    {
404
        if ($this->option($option) || $this->option('all')) {
405
            return true;
406
        }
407
408
        $questions = [
409
            'hasBlocks' => 'Do you need to use the block editor on this module?',
410
            'hasTranslation' => 'Do you need to translate content on this module?',
411
            'hasSlug' => 'Do you need to generate slugs on this module?',
412
            'hasMedias' => 'Do you need to attach images on this module?',
413
            'hasFiles' => 'Do you need to attach files on this module?',
414
            'hasPosition' => 'Do you need to manage the position of records on this module?',
415
            'hasRevisions' => 'Do you need to enable revisions on this module?',
416
        ];
417
418
        return 'yes' === $this->choice($questions[$option], ['no', 'yes'], $this->defaultsAnswserToNo ? 0 : 1);
419
    }
420
}
421