Launcher::open()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\launcher;
6
7
use drupol\phposinfo\Enum\FamilyName;
8
use drupol\phposinfo\OsInfo;
9
use Exception;
10
use Symfony\Component\Process\Exception\ProcessFailedException;
11
use Symfony\Component\Process\Process;
12
13
/**
14
 * Class Launcher.
15
 */
16
final class Launcher
17
{
18
    /**
19
     * Open a resource on your operating system.
20
     *
21
     * @param string ...$resources
22
     *   The resource. (URL, filepath, etc etc)
23
     *
24
     * @throws Exception
25
     */
26
    public static function open(string ...$resources): void
27
    {
28
        $baseCommand = self::getCommand();
29
30
        foreach ($resources as $resource) {
31
            $process = new Process([
32
                $baseCommand,
33
                $resource,
34
            ]);
35
36
            $process->run();
37
38
            if (!$process->isSuccessful()) {
39
                throw new ProcessFailedException($process);
40
            }
41
        }
42
    }
43
44
    /**
45
     * Get the command to run.
46
     *
47
     * @throws Exception
48
     *
49
     * @return string
50
     */
51
    private static function getCommand(): string
52
    {
53
        switch (OsInfo::family()) {
54
            case FamilyName::BSD:
55
            case FamilyName::LINUX:
56
                $command = 'xdg-open';
57
58
                break;
59
            case FamilyName::DARWIN:
60
                $command = 'open';
61
62
                break;
63
            case FamilyName::WINDOWS:
64
                $command = 'start';
65
66
                break;
67
68
            default:
69
                throw new Exception('Unable to find the operating system.');
70
        }
71
72
        return $command;
73
    }
74
}
75