Completed
Push — master ( 19d62a...28ed8a )
by Mokhlas
01:16
created

MultiAuthPrepare   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 823
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 50
lcom 1
cbo 1
dl 0
loc 823
rs 8
c 0
b 0
f 0

28 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B installMigration() 0 41 1
B installModel() 0 36 2
A handle() 0 19 1
A installPrologueAlert() 0 9 2
B installView() 0 120 6
A installRouteMaps() 0 19 1
A installRouteFiles() 0 20 2
B installRequests() 0 42 3
B installControllers() 0 101 3
B installConfigs() 0 39 1
B installUnauthenticated() 0 27 3
B installMiddleware() 0 36 3
B executeProcess() 0 23 5
A getParsedNameInput() 0 4 1
A getNameInput() 0 4 1
A writeMigration() 0 7 1
A getMigrationPath() 0 4 1
A getRouteServicesPath() 0 4 1
A getAppFolderPath() 0 4 1
A getRoutesFolderPath() 0 4 1
A getConfigsFolderPath() 0 4 1
A getViewsFolderPath() 0 4 1
A getControllersPath() 0 4 1
A getHttpPath() 0 4 1
A getMiddlewarePath() 0 4 1
A insertIntoFile() 0 5 2
A insert() 0 8 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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\MultiAuthCommand\Command;
4
5
use Illuminate\Database\Console\Migrations\BaseCommand;
6
use Illuminate\Database\Migrations\MigrationCreator;
7
use Illuminate\Support\Composer;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
use Symfony\Component\Process\Process;
10
11
class MultiAuthPrepare extends BaseCommand
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'make:multi-auth {name}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create MultiAuth for your project';
26
27
    /**
28
     * The migration creator instance.
29
     *
30
     * @var \Illuminate\Database\Migrations\MigrationCreator
31
     */
32
    protected $creator;
33
34
    /**
35
     * The Composer instance.
36
     *
37
     * @var \Illuminate\Support\Composer
38
     */
39
    protected $composer;
40
41
    /**
42
     * Create a new migration install command instance.
43
     *
44
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
45
     * @param  \Illuminate\Support\Composer  $composer
46
     */
47
    public function __construct(MigrationCreator $creator, Composer $composer)
48
    {
49
        parent::__construct();
50
        $this->creator = $creator;
51
        $this->composer = $composer;
52
    }
53
54
    /**
55
     * Execute the console command.
56
     *
57
     * @return boolean
58
     */
59
    public function handle()
60
    {
61
62
        $this->info("### Preparing For MultiAuth. Please wait...");
63
        $this->installMigration();
64
        $this->installModel();
65
        $this->installRouteMaps();
66
        $this->installRouteFiles();
67
        $this->installControllers();
68
        $this->installRequests();
69
        $this->installConfigs();
70
        $this->installMiddleware();
71
        $this->installView();
72
        $this->installPrologueAlert();
73
        $this->composer->dumpAutoloads();
74
        $this->info("### Finished MultiAuth.");
75
76
        return true;
77
    }
78
79
    /**
80
     * Publish Prologue Alert
81
     *
82
     * @return boolean
83
     */
84
    public function installPrologueAlert() {
85
        $alertsConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."prologue/alerts.php";
86
        if (!file_exists($alertsConfigFile)) {
87
            $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"',
88
                'publishing config for notifications - prologue/alerts');
89
        }
90
91
        return true;
92
    }
93
94
    /**
95
     * Install Migration.
96
     *
97
     * @return boolean
98
     */
99
    public function installMigration()
100
    {
101
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
102
        $name = ucfirst($this->getParsedNameInput());
103
        $namePlural = str_plural($name);
104
105
106
107
        $modelTableContent = file_get_contents(__DIR__ . '/../Migration/modelTable.stub');
108
        $modelTableContentNew = str_replace([
109
            '{{$namePlural}}',
110
            '{{$nameSmallPlural}}',
111
        ], [
112
            $namePlural,
113
            $nameSmallPlural
114
        ], $modelTableContent);
115
116
117
        $modelResetPasswordTableContent = file_get_contents(__DIR__ . '/../Migration/passwordResetsTable.stub');
118
        $modelResetPasswordTableContentNew = str_replace([
119
            '{{$namePlural}}',
120
            '{{$nameSmallPlural}}',
121
        ], [
122
            $namePlural,
123
            $nameSmallPlural
124
        ], $modelResetPasswordTableContent);
125
126
127
        $migrationName = date('Y_m_d_His') . '_'.'create_' . str_plural(snake_case($name)) .'_table.php';
128
        $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName;
129
        file_put_contents($migrationModelPath, $modelTableContentNew);
130
131
        $migrationResetName = date('Y_m_d_His') . '_'
132
            .'create_' . str_plural(snake_case($name))
133
            .'_password_resets_table.php';
134
        $migrationResetModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationResetName;
135
        file_put_contents($migrationResetModelPath, $modelResetPasswordTableContentNew);
136
137
        return true;
138
139
    }
140
141
142
    /**
143
     * Install Model.
144
     *
145
     * @return boolean
146
     */
147
    public function installModel()
148
    {
149
        $nameSmall = snake_case($this->getParsedNameInput());
150
        $name = ucfirst($this->getParsedNameInput());
151
152
153
        $modelContent = file_get_contents(__DIR__ . '/../Model/model.stub');
154
        $modelContentNew = str_replace([
155
            '{{$name}}',
156
        ], [
157
            $name,
158
        ], $modelContent);
159
        $modelPath = $this->getAppFolderPath().DIRECTORY_SEPARATOR.$name.".php";
160
        file_put_contents($modelPath, $modelContentNew);
161
162
163
        $resetNotificationContent = file_get_contents(__DIR__ . '/../Notification/resetPasswordNotification.stub');
164
        $resetNotificationContentNew = str_replace([
165
            '{{$name}}',
166
            '{{$nameSmall}}',
167
        ], [
168
            $name,
169
            $nameSmall
170
        ], $resetNotificationContent);
171
172
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Notifications";
173
        if (!file_exists($createFolder)) {
174
            mkdir($createFolder);
175
        }
176
177
        $resetNotificationPath = $createFolder.DIRECTORY_SEPARATOR.$name."ResetPasswordNotification.php";
178
        file_put_contents($resetNotificationPath, $resetNotificationContentNew);
179
180
        return true;
181
182
    }
183
184
    /**
185
     * Install View.
186
     *
187
     * @return boolean
188
     */
189
    public function installView()
190
    {
191
        $nameSmall = snake_case($this->getParsedNameInput());
192
193
        $appBlade = file_get_contents(__DIR__ . '/../Views/layouts/app.blade.stub');
194
        $welcomeBlade = file_get_contents(__DIR__ . '/../Views/welcome.blade.stub');
195
        $homeBlade = file_get_contents(__DIR__ . '/../Views/home.blade.stub');
196
        $loginBlade = file_get_contents(__DIR__ . '/../Views/auth/login.blade.stub');
197
        $registerBlade = file_get_contents(__DIR__ . '/../Views/auth/register.blade.stub');
198
        $resetBlade = file_get_contents(__DIR__ . '/../Views/auth/passwords/reset.blade.stub');
199
        $emailBlade = file_get_contents(__DIR__ . '/../Views/auth/passwords/email.blade.stub');
200
201
        $update_infoBlade = file_get_contents(__DIR__
202
            . '/../Views/auth/account/update_info.blade.stub');
203
        $change_passwordBlade = file_get_contents(__DIR__
204
            . '/../Views/auth/account/change_password.blade.stub');
205
206
        $createFolder = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall";
207
        if (!file_exists($createFolder)) {
208
            mkdir($createFolder);
209
        }
210
211
        $createFolderLayouts = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
212
            ."$nameSmall"
213
            .DIRECTORY_SEPARATOR."layouts";
214
        if (!file_exists($createFolderLayouts)) {
215
            mkdir($createFolderLayouts);
216
        }
217
218
        $createFolderAuth = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall"
219
            .DIRECTORY_SEPARATOR."auth";
220
        if (!file_exists($createFolderAuth)) {
221
            mkdir($createFolderAuth);
222
        }
223
224
        $createFolderAuthPasswords = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
225
            "$nameSmall".DIRECTORY_SEPARATOR
226
            ."auth".DIRECTORY_SEPARATOR."passwords";
227
        if (!file_exists($createFolderAuthPasswords)) {
228
            mkdir($createFolderAuthPasswords);
229
        }
230
231
        $createFolderAuthAccount = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
232
            "$nameSmall".DIRECTORY_SEPARATOR
233
            ."auth".DIRECTORY_SEPARATOR."account";
234
        if (!file_exists($createFolderAuthAccount)) {
235
            mkdir($createFolderAuthAccount);
236
        }
237
238
        $appBladeNew = str_replace([
239
            '{{$nameSmall}}',
240
        ], [
241
            $nameSmall
242
        ], $appBlade);
243
244
        $welcomeBladeNew = str_replace([
245
            '{{$nameSmall}}',
246
        ], [
247
            $nameSmall
248
        ], $welcomeBlade);
249
250
        $homeBladeNew = str_replace([
251
            '{{$nameSmall}}',
252
        ], [
253
            $nameSmall
254
        ], $homeBlade);
255
256
        $loginBladeNew = str_replace([
257
            '{{$nameSmall}}',
258
        ], [
259
            $nameSmall
260
        ], $loginBlade);
261
262
        $registerBladeNew = str_replace([
263
            '{{$nameSmall}}',
264
        ], [
265
            $nameSmall
266
        ], $registerBlade);
267
268
        $emailBladeNew = str_replace([
269
            '{{$nameSmall}}',
270
        ], [
271
            $nameSmall
272
        ], $emailBlade);
273
274
        $resetBladeNew = str_replace([
275
            '{{$nameSmall}}',
276
        ], [
277
            $nameSmall
278
        ], $resetBlade);
279
280
        $update_infoBladeNew = str_replace([
281
            '{{$nameSmall}}',
282
            '{{$name}}',
283
        ], [
284
            $nameSmall
285
        ], $update_infoBlade);
286
287
        $change_passwordBladeNew = str_replace([
288
            '{{$nameSmall}}',
289
        ], [
290
            $nameSmall
291
        ], $change_passwordBlade);
292
293
294
295
        file_put_contents($createFolderLayouts.'/app.blade.php', $appBladeNew);
296
        file_put_contents($createFolder.'/welcome.blade.php', $welcomeBladeNew);
297
        file_put_contents($createFolder.'/home.blade.php', $homeBladeNew);
298
        file_put_contents($createFolderAuth.'/login.blade.php', $loginBladeNew);
299
        file_put_contents($createFolderAuth.'/register.blade.php', $registerBladeNew);
300
        file_put_contents($createFolderAuthPasswords.'/email.blade.php', $emailBladeNew);
301
        file_put_contents($createFolderAuthPasswords.'/reset.blade.php', $resetBladeNew);
302
303
        file_put_contents($createFolderAuthAccount.'/update_info.blade.php', $update_infoBladeNew);
304
        file_put_contents($createFolderAuthAccount.'/change_password.blade.php', $change_passwordBladeNew);
305
306
        return true;
307
308
    }
309
310
    /**
311
     * Install RouteMaps.
312
     *
313
     * @return boolean
314
     */
315
316
    public function installRouteMaps()
317
    {
318
        $nameSmall = snake_case($this->getParsedNameInput());
319
        $name = ucfirst($this->getParsedNameInput());
320
        $mapCallFunction = file_get_contents(__DIR__ . '/../Route/mapRoute.stub');
321
        $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction);
322
        $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true);
323
        $mapFunction = file_get_contents(__DIR__ . '/../Route/mapRouteFunction.stub');
324
        $mapFunctionNew = str_replace([
325
            '{{$name}}',
326
            '{{$nameSmall}}'
327
        ], [
328
            "$name",
329
            "$nameSmall"
330
        ], $mapFunction);
331
        $this->insert($this->getRouteServicesPath(), '        //
332
    }', $mapFunctionNew, true);
333
        return true;
334
    }
335
336
    /**
337
     * Install RouteFile.
338
     *
339
     * @return boolean
340
     */
341
342
    public function installRouteFiles()
343
    {
344
        $nameSmall = snake_case($this->getParsedNameInput());
345
        $name = ucfirst($this->getParsedNameInput());
346
        $createFolder = $this->getRoutesFolderPath().DIRECTORY_SEPARATOR.$nameSmall;
347
        if (!file_exists($createFolder)) {
348
            mkdir($createFolder);
349
        }
350
        $routeFileContent = file_get_contents(__DIR__ . '/../Route/routeFile.stub');
351
        $routeFileContentNew = str_replace([
352
            '{{$name}}',
353
            '{{$nameSmall}}'
354
        ], [
355
            "$name",
356
            "$nameSmall"
357
        ], $routeFileContent);
358
        $routeFile = $createFolder.DIRECTORY_SEPARATOR.$nameSmall.".php";
359
        file_put_contents($routeFile, $routeFileContentNew);
360
        return true;
361
    }
362
363
    /**
364
     * Install Requests.
365
     *
366
     * @return boolean
367
     */
368
369
    public function installRequests()
370
    {
371
        $nameSmall = snake_case($this->getParsedNameInput());
372
        $name = ucfirst($this->getParsedNameInput());
373
374
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
375
        if (!file_exists($nameFolder)) {
376
            mkdir($nameFolder);
377
        }
378
379
        $requestsFolder = $nameFolder.DIRECTORY_SEPARATOR."Requests";
380
        if (!file_exists($requestsFolder)) {
381
            mkdir($requestsFolder);
382
        }
383
        $accountInfoContent = file_get_contents(__DIR__ . '/../Request/AccountInfoRequest.stub');
384
        $changePasswordContent = file_get_contents(__DIR__ . '/../Request/ChangePasswordRequest.stub');
385
386
        $accountInfoContentNew = str_replace([
387
            '{{$name}}',
388
            '{{$nameSmall}}'
389
        ], [
390
            "$name",
391
            "$nameSmall"
392
        ], $accountInfoContent);
393
394
        $changePasswordContentNew = str_replace([
395
            '{{$name}}',
396
            '{{$nameSmall}}'
397
        ], [
398
            "$name",
399
            "$nameSmall"
400
        ], $changePasswordContent);
401
402
        $accountInfoFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}AccountInfoRequest.php";
403
        $changePasswordFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}ChangePasswordRequest.php";
404
405
        file_put_contents($accountInfoFile, $accountInfoContentNew);
406
        file_put_contents($changePasswordFile, $changePasswordContentNew);
407
408
        return true;
409
410
    }
411
    /**
412
     * Install Controller.
413
     *
414
     * @return boolean
415
     */
416
417
    public function installControllers()
418
    {
419
        $nameSmall = snake_case($this->getParsedNameInput());
420
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
421
        $name = ucfirst($this->getParsedNameInput());
422
423
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
424
        if (!file_exists($nameFolder)) {
425
            mkdir($nameFolder);
426
        }
427
428
        $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth";
429
        if (!file_exists($authFolder)) {
430
            mkdir($authFolder);
431
        }
432
433
        $controllerContent = file_get_contents(__DIR__ . '/../Controllers/Controller.stub');
434
        $homeControllerContent = file_get_contents(__DIR__ . '/../Controllers/HomeController.stub');
435
        $loginControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/LoginController.stub');
436
        $forgotControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ForgotPasswordController.stub');
437
        $registerControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/RegisterController.stub');
438
        $resetControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ResetPasswordController.stub');
439
        $myAccountControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/MyAccountController.stub');
440
441
        $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent);
442
443
        $homeFileContentNew = str_replace([
444
            '{{$name}}',
445
            '{{$nameSmall}}'
446
        ], [
447
            "$name",
448
            "$nameSmall"
449
        ], $homeControllerContent);
450
451
        $loginFileContentNew = str_replace([
452
            '{{$name}}',
453
            '{{$nameSmall}}'
454
        ], [
455
            "$name",
456
            "$nameSmall"
457
        ], $loginControllerContent);
458
459
        $forgotFileContentNew = str_replace([
460
            '{{$name}}',
461
            '{{$nameSmall}}',
462
            '{{$nameSmallPlural}}'
463
        ], [
464
            "$name",
465
            "$nameSmall",
466
            "$nameSmallPlural"
467
        ], $forgotControllerContent);
468
469
        $registerFileContentNew = str_replace([
470
            '{{$name}}',
471
            '{{$nameSmall}}',
472
            '{{$nameSmallPlural}}'
473
        ], [
474
            "$name",
475
            "$nameSmall",
476
            "$nameSmallPlural"
477
        ], $registerControllerContent);
478
479
        $resetFileContentNew = str_replace([
480
            '{{$name}}',
481
            '{{$nameSmall}}',
482
            '{{$nameSmallPlural}}'
483
        ], [
484
            "$name",
485
            "$nameSmall",
486
            "$nameSmallPlural"
487
        ], $resetControllerContent);
488
489
        $myAccountFileContentNew = str_replace([
490
            '{{$name}}',
491
            '{{$nameSmall}}'
492
        ], [
493
            "$name",
494
            "$nameSmall"
495
        ], $myAccountControllerContent);
496
497
        $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php";
498
        $homeFile = $nameFolder.DIRECTORY_SEPARATOR."HomeController.php";
499
        $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php";
500
        $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php";
501
        $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php";
502
        $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php";
503
504
        $myAccountFile = $authFolder.DIRECTORY_SEPARATOR."{$name}AccountController.php";
505
506
507
        file_put_contents($controllerFile, $controllerFileContentNew);
508
        file_put_contents($homeFile, $homeFileContentNew);
509
        file_put_contents($loginFile, $loginFileContentNew);
510
        file_put_contents($forgotFile, $forgotFileContentNew);
511
        file_put_contents($registerFile, $registerFileContentNew);
512
        file_put_contents($resetFile, $resetFileContentNew);
513
        file_put_contents($myAccountFile, $myAccountFileContentNew);
514
515
        return true;
516
517
    }
518
519
    /**
520
     * Install Configs.
521
     *
522
     * @return boolean
523
     */
524
525
    public function installConfigs()
526
    {
527
        $nameSmall = snake_case($this->getParsedNameInput());
528
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
529
        $name = ucfirst($this->getParsedNameInput());
530
531
        $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php";
532
533
        $guardContent = file_get_contents(__DIR__ . '/../Config/guard.stub');
534
        $passwordContent = file_get_contents(__DIR__ . '/../Config/password.stub');
535
        $providerContent = file_get_contents(__DIR__ . '/../Config/provider.stub');
536
537
        $guardFileContentNew = str_replace([
538
            '{{$nameSmall}}',
539
            '{{$nameSmallPlural}}'
540
        ], [
541
            "$nameSmall",
542
            "$nameSmallPlural"
543
        ], $guardContent);
544
545
        $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent);
546
547
        $providerFileContentNew = str_replace([
548
            '{{$name}}',
549
            '{{$nameSmallPlural}}'
550
        ], [
551
            "$name",
552
            "$nameSmallPlural"
553
        ], $providerContent);
554
555
        $this->insert($authConfigFile, '    \'guards\' => [', $guardFileContentNew, true);
556
557
        $this->insert($authConfigFile, '    \'passwords\' => [', $passwordFileContentNew, true);
558
559
        $this->insert($authConfigFile, '    \'providers\' => [', $providerFileContentNew, true);
560
561
        return true;
562
563
    }
564
565
    /**
566
     * Install Unauthenticated Handler.
567
     *
568
     * @return boolean
569
     */
570
    public function installUnauthenticated()
571
    {
572
        $nameSmall = snake_case($this->getParsedNameInput());
573
        $exceptionHandlerFile = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Exceptions".DIRECTORY_SEPARATOR
574
            ."Handler.php";
575
        $exceptionHandlerFileContent = file_get_contents($exceptionHandlerFile);
576
        $exceptionHandlerFileContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerUnauthorized.stub');
577
578
579
        if (!str_contains($exceptionHandlerFileContent, 'MultiAuthUnAuthenticated')) {
580
            // replace old file
581
            $deleted = unlink($exceptionHandlerFile);
582
            if ($deleted) {
583
                file_put_contents($exceptionHandlerFile, $exceptionHandlerFileContentNew);
584
            }
585
        }
586
587
        $exceptionHandlerGuardContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerGuard.stub');
588
        $exceptionHandlerGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
589
            $exceptionHandlerGuardContentNew);
590
591
        $this->insert($exceptionHandlerFile, '        switch(array_get($exception->guards(), 0)) {',
592
            $exceptionHandlerGuardContentNew2, true);
593
594
        return true;
595
596
    }
597
598
    /**
599
     * Install Middleware.
600
     *
601
     * @return boolean
602
     */
603
604
    public function installMiddleware()
605
    {
606
        $nameSmall = snake_case($this->getParsedNameInput());
607
608
        $redirectIfMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."RedirectIfAuthenticated.php";
609
        $middlewareKernelFile = $this->getHttpPath().DIRECTORY_SEPARATOR."Kernel.php";
610
611
        $redirectIfMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
612
613
        $redirectIfMiddlewareContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectIf.stub');
614
615
        if (!str_contains($redirectIfMiddlewareFileContent, 'MultiAuthGuards')) {
616
            // replace old file
617
            $deleted = unlink($redirectIfMiddlewareFile);
618
            if ($deleted) {
619
                file_put_contents($redirectIfMiddlewareFile, $redirectIfMiddlewareContentNew);
620
            }
621
        }
622
623
        $redirectIfMiddlewareGroupContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddleware.stub');
624
        $redirectIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddlewareGuard.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
625
626
        $redirectIfMiddlewareGroupContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
627
            $redirectIfMiddlewareGroupContentNew);
628
        $redirectIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
629
            $redirectIfMiddlewareGuardContentNew);
630
631
        $this->insert($middlewareKernelFile, '    protected $middlewareGroups = [',
632
            $redirectIfMiddlewareGroupContentNew2, true);
633
634
        $this->insert($redirectIfMiddlewareFile, '        switch ($guard) {',
635
            $redirectIfMiddlewareGuardContentNew2, true);
636
637
        return true;
638
639
    }
640
641
    /**
642
     * Run a SSH command.
643
     *
644
     * @param string $command      The SSH command that needs to be run
645
     * @param bool   $beforeNotice Information for the user before the command is run
646
     * @param bool   $afterNotice  Information for the user after the command is run
647
     *
648
     * @return mixed Command-line output
649
     */
650
    public function executeProcess($command, $beforeNotice = false, $afterNotice = false)
651
    {
652
        if ($beforeNotice) {
653
            $this->info('### '.$beforeNotice);
654
        } else {
655
            $this->info('### Running: '.$command);
656
        }
657
        $process = new Process($command);
658
        $process->run(function ($type, $buffer) {
659
            if (Process::ERR === $type) {
660
                echo '... > '.$buffer;
661
            } else {
662
                echo 'OUT > '.$buffer;
663
            }
664
        });
665
        // executes after the command finishes
666
        if (!$process->isSuccessful()) {
667
            throw new ProcessFailedException($process);
668
        }
669
        if ($afterNotice) {
670
            $this->info('### '.$afterNotice);
671
        }
672
    }
673
674
    /**
675
     * Get the desired class name from the input.
676
     *
677
     * @return string
678
     */
679
    protected function getParsedNameInput()
680
    {
681
        return mb_strtolower(str_singular($this->getNameInput()));
682
    }
683
    /**
684
     * Get the desired class name from the input.
685
     *
686
     * @return string
687
     */
688
    protected function getNameInput()
689
    {
690
        return trim($this->argument('name'));
691
    }
692
693
    /**
694
     * Write the migration file to disk.
695
     *
696
     * @param  string  $name
697
     * @param  string  $table
698
     * @param  bool    $create
699
     * @return mixed
700
     */
701
    protected function writeMigration($name, $table, $create)
702
    {
703
        $file = pathinfo($this->creator->create(
704
            $name, $this->getMigrationPath(), $table, $create
705
        ), PATHINFO_FILENAME);
706
        $this->line("<info>Created Migration:</info> {$file}");
707
    }
708
709
    /**
710
     * Get migration path (either specified by '--path' option or default location).
711
     *
712
     * @return string
713
     */
714
    protected function getMigrationPath()
715
    {
716
        return parent::getMigrationPath();
717
    }
718
719
    /**
720
     * Get Routes Provider Path.
721
     *
722
     * @return string
723
     */
724
    protected function getRouteServicesPath()
725
    {
726
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php';
727
    }
728
729
    /**
730
     * Get Routes Folder Path.
731
     *
732
     * @return string
733
     */
734
    protected function getAppFolderPath()
735
    {
736
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
737
    }
738
739
    /**
740
     * Get Routes Folder Path.
741
     *
742
     * @return string
743
     */
744
    protected function getRoutesFolderPath()
745
    {
746
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
747
    }
748
749
    /**
750
     * Get Config Folder Path.
751
     *
752
     * @return string
753
     */
754
    protected function getConfigsFolderPath()
755
    {
756
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config';
757
    }
758
759
    /**
760
     * Get Config Folder Path.
761
     *
762
     * @return string
763
     */
764
    protected function getViewsFolderPath()
765
    {
766
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
767
    }
768
769
    /**
770
     * Get Controllers Path.
771
     *
772
     * @return string
773
     */
774
    protected function getControllersPath()
775
    {
776
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
777
    }
778
779
    /**
780
     * Get Http Path.
781
     *
782
     * @return string
783
     */
784
    protected function getHttpPath()
785
    {
786
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
787
    }
788
789
    /**
790
     * Get Middleware Path.
791
     *
792
     * @return string
793
     */
794
    protected function getMiddlewarePath()
795
    {
796
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Middleware';
797
    }
798
799
    /**
800
     * insert text into file
801
     *
802
     * @param string $filePath
803
     * @param string $insertMarker
804
     * @param string $text
805
     * @param boolean $after
806
     *
807
     * @return integer
808
     */
809
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
810
        $contents = file_get_contents($filePath);
811
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
812
        return file_put_contents($filePath, $new_contents);
813
    }
814
815
    /**
816
     * insert text into file
817
     *
818
     * @param string $filePath
819
     * @param string $keyword
820
     * @param string $body
821
     * @param boolean $after
822
     *
823
     * @return integer
824
     */
825
    public function insert($filePath, $keyword, $body, $after = true) {
826
827
        $contents = file_get_contents($filePath);
828
        $new_contents = substr_replace($contents, PHP_EOL . $body,
829
            ($after) ? strpos($contents, $keyword) + strlen($keyword) : strpos($contents, $keyword)
830
            , 0);
831
        return file_put_contents($filePath, $new_contents);
832
    }
833
}
834