Passed
Push — master ( d99be9...f7f3ea )
by Mokhlas
07:05
created

MultiAuthPrepare   F

Complexity

Total Complexity 65

Size/Duplication

Total Lines 1109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 573
dl 0
loc 1109
rs 3.2
c 0
b 0
f 0
wmc 65

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A installRouteFiles() 0 19 2
B installMiddleware() 0 57 6
B handle() 0 82 4
A getControllersPath() 0 3 1
A installModel() 0 62 3
F installView() 0 264 12
A getMigrationPath() 0 3 1
A getViewsFolderPath() 0 3 1
A installConfigs() 0 37 1
A getRouteServicesPath() 0 3 1
A getAppFolderPath() 0 3 1
A installRouteMaps() 0 18 1
B installControllers() 0 110 3
A getRoutesFolderPath() 0 3 1
A getMiddlewarePath() 0 3 1
A insert() 0 7 2
A installMigration() 0 39 1
A executeProcess() 0 21 5
A getNameInput() 0 3 1
A getConfigsFolderPath() 0 3 1
A isAlreadySetup() 0 9 2
A getParsedNameInput() 0 3 1
A installPrologueAlert() 0 8 2
A getHttpPath() 0 3 1
A installRequests() 0 40 3
A writeMigration() 0 6 1
A installUnauthenticated() 0 25 3
A insertIntoFile() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like MultiAuthPrepare often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MultiAuthPrepare, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace iMokhles\MultiAuthBackPack\Command;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\Console\Migrations\BaseCommand;
7
use Illuminate\Database\Migrations\MigrationCreator;
8
use Illuminate\Support\Composer;
9
use Symfony\Component\Process\Exception\ProcessFailedException;
10
use Symfony\Component\Process\Process;
11
12
class MultiAuthPrepare extends BaseCommand
13
{
14
    /**
15
     * @var
16
     */
17
    protected $progressBar;
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'make:multi-backpack {name} {--admin_theme= : chose the theme you want';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'create multi authentication guards with backpack panel';
31
32
    /**
33
     * The migration creator instance.
34
     *
35
     * @var \Illuminate\Database\Migrations\MigrationCreator
36
     */
37
    protected $creator;
38
39
    /**
40
     * The Composer instance.
41
     *
42
     * @var \Illuminate\Support\Composer
43
     */
44
    protected $composer;
45
46
    /**
47
     * Create a new migration install command instance.
48
     *
49
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
50
     * @param  \Illuminate\Support\Composer  $composer
51
     */
52
    public function __construct(MigrationCreator $creator, Composer $composer)
53
    {
54
        parent::__construct();
55
        $this->creator = $creator;
56
        $this->composer = $composer;
57
    }
58
59
    /**
60
     * Execute the console command.
61
     *
62
     * @return boolean
63
     */
64
    public function handle()
65
    {
66
67
        $this->progressBar = $this->output->createProgressBar(14);
68
        $this->progressBar->start();
69
70
        $this->line(" Preparing For MultiAuth. Please wait...");
71
        $this->progressBar->advance();
72
73
        $name = ucfirst($this->getParsedNameInput());
74
75
        $admin_theme = $this->option('admin_theme');
76
        if (is_null($admin_theme)) {
77
            $admin_theme = 'adminlte';
78
        }
79
80
        if (file_exists(__DIR__ . '/../Backpack/Views/'.$theme_name)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $theme_name seems to be never defined.
Loading history...
81
            if ($this->isAlreadySetup() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
82
83
                $this->line(" installing migrations...");
84
                $this->installMigration();
85
                $this->progressBar->advance();
86
87
                $this->line(" installing models...");
88
                $this->installModel();
89
                $this->progressBar->advance();
90
91
                $this->line(" installing route maps...");
92
                $this->installRouteMaps();
93
                $this->progressBar->advance();
94
95
                $this->line(" installing route files...");
96
                $this->installRouteFiles();
97
                $this->progressBar->advance();
98
99
                $this->line(" installing controllers...");
100
                $this->installControllers();
101
                $this->progressBar->advance();
102
103
                $this->line(" installing requests...");
104
                $this->installRequests();
105
                $this->progressBar->advance();
106
107
                $this->line(" installing configs...");
108
                $this->installConfigs();
109
                $this->progressBar->advance();
110
111
                $this->line(" installing middleware...");
112
                $this->installMiddleware();
113
                $this->progressBar->advance();
114
115
                $this->line(" installing unauthenticated function...");
116
                $this->installUnauthenticated();
117
                $this->progressBar->advance();
118
119
                $this->line(" installing views...");
120
                $this->installView($admin_theme);
0 ignored issues
show
Bug introduced by
It seems like $admin_theme can also be of type string[]; however, parameter $theme_name of iMokhles\MultiAuthBackPa...hPrepare::installView() 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

120
                $this->installView(/** @scrutinizer ignore-type */ $admin_theme);
Loading history...
121
                $this->progressBar->advance();
122
123
                $this->line(" installing prologue alert...");
124
                $this->installPrologueAlert();
125
                $this->progressBar->advance();
126
127
                $this->line(" dump autoload...");
128
                $this->composer->dumpAutoloads();
129
                $this->progressBar->advance();
130
131
                $this->progressBar->finish();
132
                $this->info(" finished ".$name." setup with Backpack panel.");
133
            } else {
134
                $this->progressBar->finish();
135
                $this->line(" failed. already setup for: ".$name);
136
                $this->progressBar->advance();
137
            }
138
        } else {
139
            $this->progressBar->finish();
140
            $this->line(" failed: ".$theme_name." theme not found");
141
            $this->progressBar->advance();
142
        }
143
144
145
        return true;
146
    }
147
148
    /**
149
     * Publish Prologue Alert
150
     *
151
     * @return boolean
152
     */
153
    public function installPrologueAlert() {
154
        $alertsConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."prologue/alerts.php";
155
        if (!file_exists($alertsConfigFile)) {
156
            $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"',
157
                'publishing config for notifications - prologue/alerts');
0 ignored issues
show
Bug introduced by
'publishing config for n...ions - prologue/alerts' of type string is incompatible with the type boolean expected by parameter $beforeNotice of iMokhles\MultiAuthBackPa...epare::executeProcess(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
                /** @scrutinizer ignore-type */ 'publishing config for notifications - prologue/alerts');
Loading history...
158
        }
159
160
        return true;
161
    }
162
163
    /**
164
     * Install Migration.
165
     *
166
     * @return boolean
167
     */
168
    public function installMigration()
169
    {
170
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
171
        $name = ucfirst($this->getParsedNameInput());
172
        $namePlural = str_plural($name);
173
174
175
176
        $modelTableContent = file_get_contents(__DIR__ . '/../Migration/modelTable.stub');
177
        $modelTableContentNew = str_replace([
178
            '{{$namePlural}}',
179
            '{{$nameSmallPlural}}',
180
        ], [
181
            $namePlural,
182
            $nameSmallPlural
183
        ], $modelTableContent);
184
185
186
        $modelResetPasswordTableContent = file_get_contents(__DIR__ . '/../Migration/passwordResetsTable.stub');
187
        $modelResetPasswordTableContentNew = str_replace([
188
            '{{$namePlural}}',
189
            '{{$nameSmallPlural}}',
190
        ], [
191
            $namePlural,
192
            $nameSmallPlural
193
        ], $modelResetPasswordTableContent);
194
195
196
        $migrationName = date('Y_m_d_His') . '_'.'create_' . str_plural(snake_case($name)) .'_table.php';
197
        $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName;
198
        file_put_contents($migrationModelPath, $modelTableContentNew);
199
200
        $migrationResetName = date('Y_m_d_His') . '_'
201
            .'create_' . str_plural(snake_case($name))
202
            .'_password_resets_table.php';
203
        $migrationResetModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationResetName;
204
        file_put_contents($migrationResetModelPath, $modelResetPasswordTableContentNew);
205
206
        return true;
207
208
    }
209
210
211
    /**
212
     * Install Model.
213
     *
214
     * @return boolean
215
     */
216
    public function installModel()
217
    {
218
        $nameSmall = snake_case($this->getParsedNameInput());
219
        $name = ucfirst($this->getParsedNameInput());
220
221
222
        $arrayToChange = [
223
            '{{$name}}',
224
        ];
225
226
        $newChanges = [
227
            $name,
228
        ];
229
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
230
        array_push($arrayToChange, '{{$nameSmallPlural}}');
231
        array_push($arrayToChange, '{{$nameSmall}}');
232
        array_push($newChanges, $nameSmallPlural);
233
        array_push($newChanges, $nameSmall);
234
235
        $modelContent = file_get_contents(__DIR__ . '/../Backpack/Model/model.stub');
236
        $modelContentNew = str_replace($arrayToChange, $newChanges, $modelContent);
237
238
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Models";
239
        if (!file_exists($createFolder)) {
240
            mkdir($createFolder);
241
        }
242
243
        $modelPath = $createFolder.DIRECTORY_SEPARATOR.$name.".php";
244
        file_put_contents($modelPath, $modelContentNew);
245
246
247
248
        $resetNotificationContent = file_get_contents(__DIR__ . '/../Notification/resetPasswordNotification.stub');
249
        $resetNotificationContentNew = str_replace([
250
            '{{$name}}',
251
            '{{$nameSmall}}',
252
        ], [
253
            $name,
254
            $nameSmall
255
        ], $resetNotificationContent);
256
257
        $verifyEmailContent = file_get_contents(__DIR__ . '/../Notification/verifyEmailNotification.stub');
258
        $verifyEmailContentNew = str_replace([
259
            '{{$name}}',
260
            '{{$nameSmall}}',
261
        ], [
262
            $name,
263
            $nameSmall
264
        ], $verifyEmailContent);
265
266
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Notifications";
267
        if (!file_exists($createFolder)) {
268
            mkdir($createFolder);
269
        }
270
271
        $resetNotificationPath = $createFolder.DIRECTORY_SEPARATOR.$name."ResetPasswordNotification.php";
272
        file_put_contents($resetNotificationPath, $resetNotificationContentNew);
273
274
        $verifyEmailPath = $createFolder.DIRECTORY_SEPARATOR.$name."VerifyEmailNotification.php";
275
        file_put_contents($verifyEmailPath, $verifyEmailContentNew);
276
277
        return true;
278
279
    }
280
281
    /**
282
     * Install View.
283
     *
284
     * @param string $theme_name
285
     * @return bool
286
     */
287
    public function installView($theme_name = 'adminlte')
288
    {
289
        if (file_exists(__DIR__ . '/../Backpack/Views/'.$theme_name)) {
290
            $nameSmall = snake_case($this->getParsedNameInput());
291
            $name = ucfirst($this->getParsedNameInput());
292
293
            // layouts
294
            $appBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/layouts/layout.blade.stub');
295
            $appGuestBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/layouts/layout_guest.blade.stub');
296
297
            // home
298
            $homeBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/home.blade.stub');
299
300
            // auth
301
            $loginBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/login.blade.stub');
302
            $registerBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/register.blade.stub');
303
            $verifyEmailBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/verify.blade.stub');
304
305
            // auth/passwords
306
            $resetBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/passwords/reset.blade.stub');
307
            $emailBlade = file_get_contents(__DIR__ . '/../Backpack/Views/'.$theme_name.'/auth/passwords/email.blade.stub');
308
309
            // auth/account
310
            $update_infoBlade = file_get_contents(__DIR__
311
                . '/../Backpack/Views/'.$theme_name.'/auth/account/update_info.blade.stub');
312
            $change_passwordBlade = file_get_contents(__DIR__
313
                . '/../Backpack/Views/'.$theme_name.'/auth/account/change_password.blade.stub');
314
            $sidemenuBlade = file_get_contents(__DIR__
315
                . '/../Backpack/Views/'.$theme_name.'/auth/account/sidemenu.blade.stub');
316
317
            // inc
318
            $main_headerBlade = file_get_contents(__DIR__
319
                . '/../Backpack/Views/'.$theme_name.'/inc/main_header.blade.stub');
320
            $sidebarBlade = file_get_contents(__DIR__
321
                . '/../Backpack/Views/'.$theme_name.'/inc/sidebar.blade.stub');
322
            $alertsBlade = file_get_contents(__DIR__
323
                . '/../Backpack/Views/'.$theme_name.'/inc/alerts.blade.stub');
324
            $footerBlade = file_get_contents(__DIR__
325
                . '/../Backpack/Views/'.$theme_name.'/inc/footer.blade.stub');
326
            $footerGuestBlade = file_get_contents(__DIR__
327
                . '/../Backpack/Views/'.$theme_name.'/inc/footer_guest.blade.stub');
328
            $headBlade = file_get_contents(__DIR__
329
                . '/../Backpack/Views/'.$theme_name.'/inc/head.blade.stub');
330
            $scriptsBlade = file_get_contents(__DIR__
331
                . '/../Backpack/Views/'.$theme_name.'/inc/scripts.blade.stub');
332
333
            if ($theme_name == 'adminlte') {
334
                $menuBlade = file_get_contents(__DIR__
335
                    . '/../Backpack/Views/'.$theme_name.'/inc/menu.blade.stub');
336
                $sidebar_user_panelBlade = file_get_contents(__DIR__
337
                    . '/../Backpack/Views/'.$theme_name.'/inc/sidebar_user_panel.blade.stub');
338
                // style
339
                $authCSS = file_get_contents(__DIR__
340
                    . '/../Backpack/Views/'.$theme_name.'/style/backpack_auth_css.blade.stub');
341
            } else {
342
                $account_infoBlade = file_get_contents(__DIR__
343
                    . '/../Backpack/Views/'.$theme_name.'/auth/account/account_info.blade.stub');
344
                $sidebar_user_panelBlade = file_get_contents(__DIR__
345
                    . '/../Backpack/Views/'.$theme_name.'/inc/user_menu.blade.stub');
346
                $breadcrumbBlade = file_get_contents(__DIR__
347
                    . '/../Backpack/Views/'.$theme_name.'/auth/account/breadcrumb.blade.stub');
348
                $notifications_menuBlade = file_get_contents(__DIR__
349
                    . '/../Backpack/Views/'.$theme_name.'/auth/account/notifications_menu.blade.stub');
350
            }
351
352
353
354
355
            $createFolder = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall";
356
            if (!file_exists($createFolder)) {
357
                mkdir($createFolder);
358
            }
359
360
            $createFolderLayouts = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
361
                ."$nameSmall"
362
                .DIRECTORY_SEPARATOR."layouts";
363
            if (!file_exists($createFolderLayouts)) {
364
                mkdir($createFolderLayouts);
365
            }
366
367
            $createFolderStyle = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
368
                ."$nameSmall"
369
                .DIRECTORY_SEPARATOR."style";
370
            if (!file_exists($createFolderStyle)) {
371
                mkdir($createFolderStyle);
372
            }
373
374
            $createFolderInc = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
375
                ."$nameSmall"
376
                .DIRECTORY_SEPARATOR."inc";
377
            if (!file_exists($createFolderInc)) {
378
                mkdir($createFolderInc);
379
            }
380
381
            $createFolderAuth = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall"
382
                .DIRECTORY_SEPARATOR."auth";
383
            if (!file_exists($createFolderAuth)) {
384
                mkdir($createFolderAuth);
385
            }
386
387
            $createFolderAuthPasswords = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
388
                "$nameSmall".DIRECTORY_SEPARATOR
389
                ."auth".DIRECTORY_SEPARATOR."passwords";
390
            if (!file_exists($createFolderAuthPasswords)) {
391
                mkdir($createFolderAuthPasswords);
392
            }
393
394
            $createFolderAuthAccount = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
395
                "$nameSmall".DIRECTORY_SEPARATOR
396
                ."auth".DIRECTORY_SEPARATOR."account";
397
            if (!file_exists($createFolderAuthAccount)) {
398
                mkdir($createFolderAuthAccount);
399
            }
400
401
            $headBladeNew = str_replace([
402
                '{{$nameSmall}}',
403
            ], [
404
                $nameSmall
405
            ], $headBlade);
406
407
            $appBladeNew = str_replace([
408
                '{{$nameSmall}}',
409
            ], [
410
                $nameSmall
411
            ], $appBlade);
412
413
            $appGuestBladeNew = str_replace([
414
                '{{$nameSmall}}',
415
            ], [
416
                $nameSmall
417
            ], $appGuestBlade);
418
419
            $verifyEmailBladeNew = str_replace([
420
                '{{$nameSmall}}',
421
            ], [
422
                $nameSmall
423
            ], $verifyEmailBlade);
424
425
            $homeBladeNew = str_replace([
426
                '{{$nameSmall}}',
427
                '{{$name}}',
428
429
            ], [
430
                $nameSmall
431
            ], $homeBlade);
432
433
            $loginBladeNew = str_replace([
434
                '{{$nameSmall}}',
435
            ], [
436
                $nameSmall
437
            ], $loginBlade);
438
439
            $registerBladeNew = str_replace([
440
                '{{$nameSmall}}',
441
            ], [
442
                $nameSmall
443
            ], $registerBlade);
444
445
            $emailBladeNew = str_replace([
446
                '{{$nameSmall}}',
447
            ], [
448
                $nameSmall
449
            ], $emailBlade);
450
451
            $resetBladeNew = str_replace([
452
                '{{$nameSmall}}',
453
            ], [
454
                $nameSmall
455
            ], $resetBlade);
456
457
            $update_infoBladeNew = str_replace([
458
                '{{$nameSmall}}',
459
                '{{$name}}',
460
            ], [
461
                $nameSmall,
462
                $name
463
            ], $update_infoBlade);
464
465
            $change_passwordBladeNew = str_replace([
466
                '{{$nameSmall}}',
467
            ], [
468
                $nameSmall,
469
            ], $change_passwordBlade);
470
471
            if ($theme_name != 'adminlte') {
472
                $account_infoBladeNew = str_replace([
473
                    '{{$nameSmall}}',
474
                ], [
475
                    $nameSmall,
476
                ], $account_infoBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $account_infoBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
477
            }
478
479
480
481
            $sidemenuBladeNew = str_replace([
482
                '{{$nameSmall}}',
483
            ], [
484
                $nameSmall
485
            ], $sidemenuBlade);
486
487
            $main_headerBladeNew = str_replace([
488
                '{{$nameSmall}}',
489
            ], [
490
                $nameSmall
491
            ], $main_headerBlade);
492
493
            $menuBladeNew = str_replace([
494
                '{{$nameSmall}}',
495
            ], [
496
                $nameSmall
497
            ], $menuBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $menuBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
498
499
            $sidebarBladeNew = str_replace([
500
                '{{$nameSmall}}',
501
            ], [
502
                $nameSmall
503
            ], $sidebarBlade);
504
505
            $sidebar_user_panelBladeNew = str_replace([
506
                '{{$nameSmall}}',
507
            ], [
508
                $nameSmall
509
            ], $sidebar_user_panelBlade);
510
511
512
            file_put_contents($createFolderLayouts.'/layout.blade.php', $appBladeNew);
513
            file_put_contents($createFolderLayouts.'/layout_guest.blade.php', $appGuestBladeNew);
514
            file_put_contents($createFolder.'/home.blade.php', $homeBladeNew);
515
516
517
            file_put_contents($createFolderInc.'/main_header.blade.php', $main_headerBladeNew);
518
            file_put_contents($createFolderInc.'/sidebar.blade.php', $sidebarBladeNew);
519
520
            file_put_contents($createFolderInc.'/alerts.blade.php', $alertsBlade);
521
            file_put_contents($createFolderInc.'/footer.blade.php', $footerBlade);
522
            file_put_contents($createFolderInc.'/footer_guest.blade.php', $footerGuestBlade);
523
            file_put_contents($createFolderInc.'/head.blade.php', $headBladeNew);
524
            file_put_contents($createFolderInc.'/scripts.blade.php', $scriptsBlade);
525
526
            file_put_contents($createFolderAuth.'/login.blade.php', $loginBladeNew);
527
            file_put_contents($createFolderAuth.'/verify.blade.php', $verifyEmailBladeNew);
528
            file_put_contents($createFolderAuth.'/register.blade.php', $registerBladeNew);
529
            file_put_contents($createFolderAuthPasswords.'/email.blade.php', $emailBladeNew);
530
            file_put_contents($createFolderAuthPasswords.'/reset.blade.php', $resetBladeNew);
531
532
            file_put_contents($createFolderAuthAccount.'/sidemenu.blade.php', $sidemenuBladeNew);
533
            file_put_contents($createFolderAuthAccount.'/update_info.blade.php', $update_infoBladeNew);
534
            file_put_contents($createFolderAuthAccount.'/change_password.blade.php', $change_passwordBladeNew);
535
536
            if ($theme_name == 'adminlte') {
537
                file_put_contents($createFolderStyle.'/backpack_auth_css.blade.php', $authCSS);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $authCSS does not seem to be defined for all execution paths leading up to this point.
Loading history...
538
                file_put_contents($createFolderInc.'/menu.blade.php', $menuBladeNew);
539
                file_put_contents($createFolderInc.'/sidebar_user_panel.blade.php', $sidebar_user_panelBladeNew);
540
            } else {
541
                file_put_contents($createFolderAuthAccount.'/account_info.blade.php', $account_infoBladeNew);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $account_infoBladeNew does not seem to be defined for all execution paths leading up to this point.
Loading history...
542
                file_put_contents($createFolderInc.'/user_menu.blade.php', $sidebar_user_panelBladeNew);
543
                file_put_contents($createFolderInc.'/breadcrumb.blade.php', $breadcrumbBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $breadcrumbBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
544
                file_put_contents($createFolderInc.'/notifications_menu.blade.php', $notifications_menuBlade);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $notifications_menuBlade does not seem to be defined for all execution paths leading up to this point.
Loading history...
545
            }
546
547
548
            return true;
549
        }
550
        return false;
551
552
    }
553
554
    /**
555
     * Install RouteMaps.
556
     *
557
     * @return boolean
558
     */
559
560
    public function installRouteMaps()
561
    {
562
        $nameSmall = snake_case($this->getParsedNameInput());
563
        $name = ucfirst($this->getParsedNameInput());
564
        $mapCallFunction = file_get_contents(__DIR__ . '/../Route/mapRoute.stub');
565
        $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction);
566
        $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true);
567
        $mapFunction = file_get_contents(__DIR__ . '/../Route/mapRouteFunction.stub');
568
        $mapFunctionNew = str_replace([
569
            '{{$name}}',
570
            '{{$nameSmall}}'
571
        ], [
572
            "$name",
573
            "$nameSmall"
574
        ], $mapFunction);
575
        $this->insert($this->getRouteServicesPath(), '        //
576
    }', $mapFunctionNew, true);
577
        return true;
578
    }
579
580
    public function isAlreadySetup() {
581
        $name = ucfirst($this->getParsedNameInput());
582
583
        $routeServicesContent = file_get_contents($this->getRouteServicesPath());
584
585
        if (str_contains($routeServicesContent,'$this->map'.$name.'Routes();')) {
586
            return true;
587
        }
588
        return false;
589
    }
590
591
    /**
592
     * Install RouteFile.
593
     *
594
     * @return boolean
595
     */
596
597
    public function installRouteFiles()
598
    {
599
        $nameSmall = snake_case($this->getParsedNameInput());
600
        $name = ucfirst($this->getParsedNameInput());
601
        $createFolder = $this->getRoutesFolderPath().DIRECTORY_SEPARATOR.$nameSmall;
602
        if (!file_exists($createFolder)) {
603
            mkdir($createFolder);
604
        }
605
        $routeFileContent = file_get_contents(__DIR__ . '/../Route/routeFile.stub');
606
        $routeFileContentNew = str_replace([
607
            '{{$name}}',
608
            '{{$nameSmall}}'
609
        ], [
610
            "$name",
611
            "$nameSmall"
612
        ], $routeFileContent);
613
        $routeFile = $createFolder.DIRECTORY_SEPARATOR.$nameSmall.".php";
614
        file_put_contents($routeFile, $routeFileContentNew);
615
        return true;
616
    }
617
618
    /**
619
     * Install Requests.
620
     *
621
     * @return boolean
622
     */
623
624
    public function installRequests()
625
    {
626
        $nameSmall = snake_case($this->getParsedNameInput());
627
        $name = ucfirst($this->getParsedNameInput());
628
629
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
630
        if (!file_exists($nameFolder)) {
631
            mkdir($nameFolder);
632
        }
633
634
        $requestsFolder = $nameFolder.DIRECTORY_SEPARATOR."Requests";
635
        if (!file_exists($requestsFolder)) {
636
            mkdir($requestsFolder);
637
        }
638
        $accountInfoContent = file_get_contents(__DIR__ . '/../Request/AccountInfoRequest.stub');
639
        $changePasswordContent = file_get_contents(__DIR__ . '/../Request/ChangePasswordRequest.stub');
640
641
        $accountInfoContentNew = str_replace([
642
            '{{$name}}',
643
            '{{$nameSmall}}'
644
        ], [
645
            "$name",
646
            "$nameSmall"
647
        ], $accountInfoContent);
648
649
        $changePasswordContentNew = str_replace([
650
            '{{$name}}',
651
            '{{$nameSmall}}'
652
        ], [
653
            "$name",
654
            "$nameSmall"
655
        ], $changePasswordContent);
656
657
        $accountInfoFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}AccountInfoRequest.php";
658
        $changePasswordFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}ChangePasswordRequest.php";
659
660
        file_put_contents($accountInfoFile, $accountInfoContentNew);
661
        file_put_contents($changePasswordFile, $changePasswordContentNew);
662
663
        return true;
664
665
    }
666
    /**
667
     * Install Controller.
668
     *
669
     * @return boolean
670
     */
671
672
    public function installControllers()
673
    {
674
        $nameSmall = snake_case($this->getParsedNameInput());
675
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
676
        $name = ucfirst($this->getParsedNameInput());
677
678
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
679
        if (!file_exists($nameFolder)) {
680
            mkdir($nameFolder);
681
        }
682
683
        $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth";
684
        if (!file_exists($authFolder)) {
685
            mkdir($authFolder);
686
        }
687
688
        $controllerContent = file_get_contents(__DIR__ . '/../Controllers/Controller.stub');
689
        $homeControllerContent = file_get_contents(__DIR__ . '/../Controllers/HomeController.stub');
690
        $loginControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/LoginController.stub');
691
        $forgotControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ForgotPasswordController.stub');
692
        $registerControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/RegisterController.stub');
693
        $resetControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ResetPasswordController.stub');
694
        $myAccountControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/MyAccountController.stub');
695
        $verificationControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/VerificationController.stub');
696
697
        $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent);
698
699
        $homeFileContentNew = str_replace([
700
            '{{$name}}',
701
            '{{$nameSmall}}'
702
        ], [
703
            "$name",
704
            "$nameSmall"
705
        ], $homeControllerContent);
706
707
        $loginFileContentNew = str_replace([
708
            '{{$name}}',
709
            '{{$nameSmall}}'
710
        ], [
711
            "$name",
712
            "$nameSmall"
713
        ], $loginControllerContent);
714
715
        $forgotFileContentNew = str_replace([
716
            '{{$name}}',
717
            '{{$nameSmall}}',
718
            '{{$nameSmallPlural}}'
719
        ], [
720
            "$name",
721
            "$nameSmall",
722
            "$nameSmallPlural"
723
        ], $forgotControllerContent);
724
725
        $registerFileContentNew = str_replace([
726
            '{{$name}}',
727
            '{{$nameSmall}}',
728
            '{{$nameSmallPlural}}'
729
        ], [
730
            "$name",
731
            "$nameSmall",
732
            "$nameSmallPlural"
733
        ], $registerControllerContent);
734
735
        $resetFileContentNew = str_replace([
736
            '{{$name}}',
737
            '{{$nameSmall}}',
738
            '{{$nameSmallPlural}}'
739
        ], [
740
            "$name",
741
            "$nameSmall",
742
            "$nameSmallPlural"
743
        ], $resetControllerContent);
744
745
        $myAccountFileContentNew = str_replace([
746
            '{{$name}}',
747
            '{{$nameSmall}}'
748
        ], [
749
            "$name",
750
            "$nameSmall"
751
        ], $myAccountControllerContent);
752
753
        $verificationControllerContentNew = str_replace([
754
            '{{$name}}',
755
            '{{$nameSmall}}'
756
        ], [
757
            "$name",
758
            "$nameSmall"
759
        ], $verificationControllerContent);
760
761
        $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php";
762
        $homeFile = $nameFolder.DIRECTORY_SEPARATOR."HomeController.php";
763
        $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php";
764
        $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php";
765
        $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php";
766
        $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php";
767
        $verificationFile = $authFolder.DIRECTORY_SEPARATOR."VerificationController.php";
768
769
        $myAccountFile = $authFolder.DIRECTORY_SEPARATOR."{$name}AccountController.php";
770
771
772
        file_put_contents($controllerFile, $controllerFileContentNew);
773
        file_put_contents($homeFile, $homeFileContentNew);
774
        file_put_contents($loginFile, $loginFileContentNew);
775
        file_put_contents($forgotFile, $forgotFileContentNew);
776
        file_put_contents($registerFile, $registerFileContentNew);
777
        file_put_contents($resetFile, $resetFileContentNew);
778
        file_put_contents($myAccountFile, $myAccountFileContentNew);
779
        file_put_contents($verificationFile, $verificationControllerContentNew);
780
781
        return true;
782
783
    }
784
785
    /**
786
     * Install Configs.
787
     *
788
     * @return boolean
789
     */
790
791
    public function installConfigs()
792
    {
793
        $nameSmall = snake_case($this->getParsedNameInput());
794
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
795
        $name = ucfirst($this->getParsedNameInput());
796
797
        $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php";
798
799
        $guardContent = file_get_contents(__DIR__ . '/../Config/guard.stub');
800
        $passwordContent = file_get_contents(__DIR__ . '/../Config/password.stub');
801
        $providerContent = file_get_contents(__DIR__ . '/../Config/provider.stub');
802
803
        $guardFileContentNew = str_replace([
804
            '{{$nameSmall}}',
805
            '{{$nameSmallPlural}}'
806
        ], [
807
            "$nameSmall",
808
            "$nameSmallPlural"
809
        ], $guardContent);
810
811
        $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent);
812
813
        $providerFileContentNew = str_replace([
814
            '{{$name}}',
815
            '{{$nameSmallPlural}}'
816
        ], [
817
            "$name",
818
            "$nameSmallPlural"
819
        ], $providerContent);
820
821
        $this->insert($authConfigFile, '    \'guards\' => [', $guardFileContentNew, true);
822
823
        $this->insert($authConfigFile, '    \'passwords\' => [', $passwordFileContentNew, true);
824
825
        $this->insert($authConfigFile, '    \'providers\' => [', $providerFileContentNew, true);
826
827
        return true;
828
829
    }
830
831
    /**
832
     * Install Unauthenticated Handler.
833
     *
834
     * @return boolean
835
     */
836
    public function installUnauthenticated()
837
    {
838
        $nameSmall = snake_case($this->getParsedNameInput());
839
        $exceptionHandlerFile = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Exceptions".DIRECTORY_SEPARATOR
840
            ."Handler.php";
841
        $exceptionHandlerFileContent = file_get_contents($exceptionHandlerFile);
842
        $exceptionHandlerFileContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerUnauthorized.stub');
843
844
845
        if (!str_contains($exceptionHandlerFileContent, 'MultiAuthUnAuthenticated')) {
846
            // replace old file
847
            $deleted = unlink($exceptionHandlerFile);
848
            if ($deleted) {
849
                file_put_contents($exceptionHandlerFile, $exceptionHandlerFileContentNew);
850
            }
851
        }
852
853
        $exceptionHandlerGuardContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerGuard.stub');
854
        $exceptionHandlerGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
855
            $exceptionHandlerGuardContentNew);
856
857
        $this->insert($exceptionHandlerFile, '        switch(array_get($exception->guards(), 0)) {',
858
            $exceptionHandlerGuardContentNew2, true);
859
860
        return true;
861
862
    }
863
864
    /**
865
     * Install Middleware.
866
     *
867
     * @return boolean
868
     */
869
870
    public function installMiddleware()
871
    {
872
        $nameSmall = snake_case($this->getParsedNameInput());
873
874
        $redirectIfMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."RedirectIfAuthenticated.php";
875
        $authenticateMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."Authenticate.php";
876
        $ensureEmailIsVerifiedMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."EnsureEmailIsVerified.php";
877
        $middlewareKernelFile = $this->getHttpPath().DIRECTORY_SEPARATOR."Kernel.php";
878
879
        $redirectIfMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
880
        $authenticateMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
881
882
        $ensureEmailIsVerifiedMiddlewareFileeContent = file_get_contents(__DIR__ . '/../Middleware/ensureEmailIsVerified.stub');
883
        $redirectIfMiddlewareContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectIf.stub');
884
        $authenticateMiddlewareContentNew = file_get_contents(__DIR__ . '/../Middleware/authenticate.stub');
885
886
        if (!file_exists($ensureEmailIsVerifiedMiddlewareFile)) {
887
            file_put_contents($ensureEmailIsVerifiedMiddlewareFile, $ensureEmailIsVerifiedMiddlewareFileeContent);
888
        }
889
890
        if (!str_contains($redirectIfMiddlewareFileContent, 'MultiAuthGuards')) {
891
            // replace old file
892
            $deleted = unlink($redirectIfMiddlewareFile);
893
            if ($deleted) {
894
                file_put_contents($redirectIfMiddlewareFile, $redirectIfMiddlewareContentNew);
895
            }
896
        }
897
898
        if (!str_contains($authenticateMiddlewareFileContent, 'MultiAuthGuards')) {
899
            // replace old file
900
            $deleted = unlink($authenticateMiddlewareFile);
901
            if ($deleted) {
902
                file_put_contents($authenticateMiddlewareFile, $authenticateMiddlewareContentNew);
903
            }
904
        }
905
906
        $redirectIfMiddlewareGroupContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddleware.stub');
907
        $redirectIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddlewareGuard.stub');
908
        $authenticateIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Middleware/authenticateIf.stub');
909
910
        $redirectIfMiddlewareGroupContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
911
            $redirectIfMiddlewareGroupContentNew);
912
        $redirectIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
913
            $redirectIfMiddlewareGuardContentNew);
914
        $authenticateIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
915
            $authenticateIfMiddlewareGuardContentNew);
916
917
        $this->insert($middlewareKernelFile, '    protected $middlewareGroups = [',
918
            $redirectIfMiddlewareGroupContentNew2, true);
919
920
        $this->insert($redirectIfMiddlewareFile, '        switch ($guard) {',
921
            $redirectIfMiddlewareGuardContentNew2, true);
922
923
        $this->insert($authenticateMiddlewareFile, '        // MultiAuthGuards',
924
            $authenticateIfMiddlewareGuardContentNew2, true);
925
926
        return true;
927
928
    }
929
930
    /**
931
     * Run a SSH command.
932
     *
933
     * @param string $command      The SSH command that needs to be run
934
     * @param bool   $beforeNotice Information for the user before the command is run
935
     * @param bool   $afterNotice  Information for the user after the command is run
936
     *
937
     * @return mixed Command-line output
938
     */
939
    public function executeProcess($command, $beforeNotice = false, $afterNotice = false)
940
    {
941
        if ($beforeNotice) {
942
            $this->info('### '.$beforeNotice);
0 ignored issues
show
Bug introduced by
Are you sure $beforeNotice of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

942
            $this->info('### './** @scrutinizer ignore-type */ $beforeNotice);
Loading history...
943
        } else {
944
            $this->info('### Running: '.$command);
945
        }
946
        $process = new Process($command);
947
        $process->run(function ($type, $buffer) {
948
            if (Process::ERR === $type) {
949
                echo '... > '.$buffer;
950
            } else {
951
                echo 'OUT > '.$buffer;
952
            }
953
        });
954
        // executes after the command finishes
955
        if (!$process->isSuccessful()) {
956
            throw new ProcessFailedException($process);
957
        }
958
        if ($afterNotice) {
959
            $this->info('### '.$afterNotice);
0 ignored issues
show
Bug introduced by
Are you sure $afterNotice of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

959
            $this->info('### './** @scrutinizer ignore-type */ $afterNotice);
Loading history...
960
        }
961
    }
962
963
    /**
964
     * Get the desired class name from the input.
965
     *
966
     * @return string
967
     */
968
    protected function getParsedNameInput()
969
    {
970
        return mb_strtolower(str_singular($this->getNameInput()));
971
    }
972
    /**
973
     * Get the desired class name from the input.
974
     *
975
     * @return string
976
     */
977
    protected function getNameInput()
978
    {
979
        return trim($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; however, parameter $str of trim() 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

979
        return trim(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
980
    }
981
982
    /**
983
     * Write the migration file to disk.
984
     *
985
     * @param  string  $name
986
     * @param  string  $table
987
     * @param  bool    $create
988
     * @return mixed
989
     */
990
    protected function writeMigration($name, $table, $create)
991
    {
992
        $file = pathinfo($this->creator->create(
993
            $name, $this->getMigrationPath(), $table, $create
994
        ), PATHINFO_FILENAME);
995
        $this->line("<info>Created Migration:</info> {$file}");
996
    }
997
998
    /**
999
     * Get migration path.
1000
     *
1001
     * @return string
1002
     */
1003
    protected function getMigrationPath()
1004
    {
1005
        return parent::getMigrationPath();
1006
    }
1007
1008
    /**
1009
     * Get Routes Provider Path.
1010
     *
1011
     * @return string
1012
     */
1013
    protected function getRouteServicesPath()
1014
    {
1015
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php';
1016
    }
1017
1018
    /**
1019
     * Get Routes Folder Path.
1020
     *
1021
     * @return string
1022
     */
1023
    protected function getAppFolderPath()
1024
    {
1025
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
1026
    }
1027
1028
    /**
1029
     * Get Routes Folder Path.
1030
     *
1031
     * @return string
1032
     */
1033
    protected function getRoutesFolderPath()
1034
    {
1035
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
1036
    }
1037
1038
    /**
1039
     * Get Config Folder Path.
1040
     *
1041
     * @return string
1042
     */
1043
    protected function getConfigsFolderPath()
1044
    {
1045
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config';
1046
    }
1047
1048
    /**
1049
     * Get Config Folder Path.
1050
     *
1051
     * @return string
1052
     */
1053
    protected function getViewsFolderPath()
1054
    {
1055
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
1056
    }
1057
1058
    /**
1059
     * Get Controllers Path.
1060
     *
1061
     * @return string
1062
     */
1063
    protected function getControllersPath()
1064
    {
1065
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
1066
    }
1067
1068
    /**
1069
     * Get Http Path.
1070
     *
1071
     * @return string
1072
     */
1073
    protected function getHttpPath()
1074
    {
1075
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
1076
    }
1077
1078
    /**
1079
     * Get Middleware Path.
1080
     *
1081
     * @return string
1082
     */
1083
    protected function getMiddlewarePath()
1084
    {
1085
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Middleware';
1086
    }
1087
1088
    /**
1089
     * insert text into file
1090
     *
1091
     * @param string $filePath
1092
     * @param string $insertMarker
1093
     * @param string $text
1094
     * @param boolean $after
1095
     *
1096
     * @return integer
1097
     */
1098
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
1099
        $contents = file_get_contents($filePath);
1100
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
1101
        return file_put_contents($filePath, $new_contents);
1102
    }
1103
1104
    /**
1105
     * insert text into file
1106
     *
1107
     * @param string $filePath
1108
     * @param string $keyword
1109
     * @param string $body
1110
     * @param boolean $after
1111
     *
1112
     * @return integer
1113
     */
1114
    public function insert($filePath, $keyword, $body, $after = true) {
1115
1116
        $contents = file_get_contents($filePath);
1117
        $new_contents = substr_replace($contents, PHP_EOL . $body,
1118
            ($after) ? strpos($contents, $keyword) + strlen($keyword) : strpos($contents, $keyword)
1119
            , 0);
1120
        return file_put_contents($filePath, $new_contents);
1121
    }
1122
}
1123