Passed
Push — master ( 4fc3f1...05af06 )
by Brian
02:29
created

InstallCommand::handle()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

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