InstallCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 105
ccs 42
cts 42
cp 1
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A placeholders() 0 11 1
A hydrateStubs() 0 24 2
A handle() 0 23 6
A removeDarkClasses() 0 4 2
1
<?php
2
3
namespace Bmatovu\MultiAuth\Console;
4
5
use Bmatovu\MultiAuth\Console\Traits\InstallsApiStack;
6
use Bmatovu\MultiAuth\Console\Traits\InstallsBladeStack;
7
use Bmatovu\MultiAuth\Console\Traits\InstallsInertiaReactStack;
8
use Bmatovu\MultiAuth\Console\Traits\InstallsInertiaVueStack;
9
use Illuminate\Console\Command;
10
use Illuminate\Filesystem\Filesystem;
11
use Illuminate\Support\Str;
12
use Illuminate\Support\Facades\Log;
13
use Symfony\Component\Finder\Finder;
14
15
class InstallCommand extends Command
16
{
17
    use InstallsApiStack, InstallsBladeStack, InstallsInertiaReactStack, InstallsInertiaVueStack;
18
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'multi-auth:install
25
                            {guard=admin : Name of the guard (user area).}
26
                            {--stack= : The development stack that should be installed (blade,react,vue,api)}
27
                            {--dark : Indicate that dark mode support should be installed}
28
                            {--pest : Indicate that Pest should be installed}';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Install the MultiAuth controllers and resources';
36
37
    /**
38
     * The available stacks.
39
     *
40
     * @var array<int, string>
41
     */
42
    protected $stacks = ['blade', 'react', 'vue', 'api'];
43
44
    /**
45
     * Execute the console command.
46
     */
47 5
    public function handle(): mixed
48
    {
49 5
        $stack = $this->option('stack');
50
51 5
        if (!$stack) {
52 1
            $stack = $this->choice('What is your stack?', $this->stacks, 0);
53
        }
54
55 5
        $this->hydrateStubs(__DIR__ . '/../../stubs', $this->placeholders($this->argument('guard')));
56
57 5
        if ('vue' === $stack) {
58 1
            $this->installInertiaVueStack();
59 4
        } elseif ('react' === $stack) {
60 1
            $this->installInertiaReactStack();
61 3
        } elseif ('api' === $stack) {
62 1
            $this->installApiStack();
63 2
        } elseif ('blade' === $stack) {
64 1
            $this->installBladeStack();
65
        } else {
66 1
            $this->error('Invalid stack. Supported stacks are [blade], [react], [vue], and [api].');
67
        }
68
69 5
        return 1;
70
    }
71
72 5
    protected function placeholders(string $guard): array
73
    {
74 5
        return [
75 5
            '{{pluralCamel}}' => Str::plural(Str::camel($guard)),
76 5
            '{{pluralSlug}}' => Str::plural(Str::slug($guard)),
77 5
            '{{pluralSnake}}' => Str::plural(Str::snake($guard)),
78 5
            '{{pluralClass}}' => Str::plural(Str::studly($guard)),
79 5
            '{{singularCamel}}' => Str::singular(Str::camel($guard)),
80 5
            '{{singularSlug}}' => Str::singular(Str::slug($guard)),
81 5
            '{{singularSnake}}' => Str::singular(Str::snake($guard)),
82 5
            '{{singularClass}}' => Str::singular(Str::studly($guard)),
83 5
        ];
84
    }
85
86 5
    protected function hydrateStubs(string $dirPath, array $placeholders): void
87
    {
88 5
        $fs = new Filesystem();
89
90 5
        $fs->deleteDirectory(\dirname($dirPath) . '/.stubs');
91
92 5
        $rdi = new \RecursiveDirectoryIterator($dirPath, \FilesystemIterator::SKIP_DOTS);
93
94 5
        $rii = new \RecursiveIteratorIterator($rdi);
95
96 5
        foreach ($rii as $splFileInfo) {
97 5
            $newPath = \dirname($dirPath) . '/.stubs' . str_replace($dirPath, '', $splFileInfo->getPath());
98
99 5
            $newPath = strtr($newPath, $placeholders);
100
101 5
            $fs->ensureDirectoryExists($newPath);
102
103 5
            $fileName = $splFileInfo->getFilename();
104
105 5
            $newFilePath = $newPath . '/' . strtr($fileName, $placeholders);
106
107 5
            $fileContent = file_get_contents($splFileInfo->getPath() . '/' . $fileName);
108
109 5
            file_put_contents($newFilePath, strtr($fileContent, $placeholders));
110
        }
111
    }
112
113
    /**
114
     * Remove Tailwind dark classes from the given files.
115
     */
116 3
    protected function removeDarkClasses(Finder $finder): void
117
    {
118 3
        foreach ($finder as $file) {
119 3
            file_put_contents($file->getPathname(), preg_replace('/\sdark:[^\s"\']+/', '', $file->getContents()));
120
        }
121
    }
122
}
123