Passed
Push — master ( 4cbafb...c0ea2b )
by Caen
06:14 queued 02:29
created

HerdInstallCommand::getDriverTargetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\RealtimeCompiler\Console\Commands;
6
7
use Hyde\Hyde;
8
use Hyde\Console\Concerns\Command;
9
use Illuminate\Support\Facades\File;
10
use Exception;
11
12
/**
13
 * @experimental This command is experimental and may be unstable. Report issues at GitHub!
14
 *
15
 * @see https://github.com/hydephp/realtime-compiler/pull/30
16
 */
17
class HerdInstallCommand extends Command
18
{
19
    /** @var string */
20
    protected $signature = 'herd:install {--force : Overwrite any existing file}';
21
22
    /** @var string */
23
    protected $description = '[Experimental] Install the HydePHP Valet driver for Laravel Herd';
24
25
    public function safeHandle(): int
26
    {
27
        $driverTargetPath = $this->getDriverTargetPath();
28
        $driverSourcePath = Hyde::vendorPath('resources/stubs/HydeValetDriver.php', 'realtime-compiler');
0 ignored issues
show
Bug introduced by
The method vendorPath() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

28
        /** @scrutinizer ignore-call */ 
29
        $driverSourcePath = Hyde::vendorPath('resources/stubs/HydeValetDriver.php', 'realtime-compiler');
Loading history...
29
30
        if (! is_dir(dirname($driverTargetPath))) {
31
            $this->error('Herd Valet drivers directory not found. Is Herd installed?');
32
33
            return Command::FAILURE;
34
        }
35
36
        if (file_exists($driverTargetPath) && ! $this->option('force')) {
37
            if (! $this->confirm('The HydePHP Valet driver for Herd already exists. Do you want to overwrite it?', true)) {
38
                $this->info('Installation cancelled.');
39
40
                return Command::SUCCESS;
41
            }
42
        }
43
44
        try {
45
            File::copy($driverSourcePath, $driverTargetPath);
46
            $this->info('HydePHP Valet driver for Herd successfully installed!');
47
            $this->line('Driver path: '.$driverTargetPath);
48
49
            return Command::SUCCESS;
50
        } catch (Exception $exception) {
51
            $this->error('Failed to install the HydePHP Valet driver: '.$exception->getMessage());
52
53
            return Command::FAILURE;
54
        }
55
    }
56
57
    private function getDriverTargetPath(): string
58
    {
59
        return match (PHP_OS_FAMILY) {
60
            'Darwin' => $_SERVER['HOME'].'/Library/Application Support/Herd/config/valet/Drivers/HydeValetDriver.php',
61
            'Windows' => $_SERVER['USERPROFILE'].'\.config\herd\config\valet\Drivers\HydeValetDriver.php',
62
            default => throw new Exception('Herd is not yet supported on Linux.'),
63
        };
64
    }
65
}
66