Passed
Push — master ( 56df63...3e77af )
by Brian
02:31
created

InstallCommand::handle()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 20
nop 0
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace Bmatovu\MultiAuth\Console;
4
5
use Bmatovu\MultiAuth\Console\Traits\InstallsBladeStack;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Str;
9
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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