Completed
Push — master ( f081a5...127bd9 )
by Aleksandr
14s queued 11s
created

WiFi::setPhpOperationSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sanchescom\WiFi;
6
7
use Sanchescom\WiFi\Contracts\CommandInterface;
8
use Sanchescom\WiFi\Exceptions\UnknownSystemException;
9
use Sanchescom\WiFi\System\AbstractNetworks;
10
use Sanchescom\WiFi\System\Collection;
11
use Sanchescom\WiFi\System\Command;
12
use Sanchescom\WiFi\System\Linux\Networks as LinuxNetworks;
13
use Sanchescom\WiFi\System\Mac\Networks as MacNetworks;
14
use Sanchescom\WiFi\System\Windows\Networks as WindowsNetworks;
15
16
/**
17
 * Class WiFi.
18
 */
19
class WiFi
20
{
21
    /** @var string */
22
    const OS_LINUX = 'LINUX';
23
24
    /** @var string */
25
    const OS_OSX = 'DAR';
26
27
    /** @var string */
28
    const OS_WIN = 'WIN';
29
30
    /** @var string */
31
    protected static $commandClass = Command::class;
32
33
    /** @var string */
34
    protected static $phpOperationSystem = PHP_OS;
35
36
    /** @var array */
37
    protected static $systems = [
38
        self::OS_LINUX => LinuxNetworks::class,
39
        self::OS_OSX   => MacNetworks::class,
40
        self::OS_WIN   => WindowsNetworks::class,
41
    ];
42
43
    /**
44
     * @param string $commandClass
45
     */
46
    public static function setCommandClass(string $commandClass): void
47
    {
48
        self::$commandClass = $commandClass;
49
    }
50
51
    /**
52
     * @param string $phpOperationSystem
53
     */
54
    public static function setPhpOperationSystem(string $phpOperationSystem): void
55
    {
56
        self::$phpOperationSystem = $phpOperationSystem;
57
    }
58
59
    /**
60
     * @return \Sanchescom\WiFi\System\Collection
61
     */
62
    public static function scan(): Collection
63
    {
64
        return (new static())->getSystemInstance()->scan();
65
    }
66
67
    /**
68
     * Getting instance on network collections depended on operation system.
69
     *
70
     * @throws \Sanchescom\WiFi\Exceptions\UnknownSystemException
71
     *
72
     * @return \Sanchescom\WiFi\System\AbstractNetworks
73
     */
74
    protected function getSystemInstance(): AbstractNetworks
75
    {
76
        $operationSystem = $this->getOperationSystem();
77
78
        if (!array_key_exists($operationSystem, static::$systems)) {
79
            throw new UnknownSystemException();
80
        }
81
82
        return new static::$systems[$operationSystem]($this->getCommandInstance());
83
    }
84
85
    /**
86
     * @return \Sanchescom\WiFi\Contracts\CommandInterface
87
     */
88
    protected function getCommandInstance(): CommandInterface
89
    {
90
        return new static::$commandClass();
91
    }
92
93
    /**
94
     * Getting prefix from operation system name.
95
     *
96
     * @return string
97
     */
98
    protected function getOperationSystem()
99
    {
100
        return strtoupper(substr(self::$phpOperationSystem, 0, 3));
101
    }
102
}
103