Passed
Pull Request — master (#5)
by Aleksandr
02:53
created

AbstractNetwork::dBmToQuality()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sanchescom\WiFi\System;
6
7
/**
8
 * Class AbstractNetwork.
9
 */
10
abstract class AbstractNetwork
11
{
12
    /** @var string */
13
    const WPA2_SECURITY = 'WPA2';
14
15
    /** @var string */
16
    const WPA_SECURITY = 'WPA';
17
18
    /** @var string */
19
    const WEP_SECURITY = 'WEP';
20
21
    /** @var string */
22
    const UNKNOWN_SECURITY = 'Unknown';
23
24
    /** @var string */
25
    public $bssid;
26
27
    /** @var string */
28
    public $ssid;
29
30
    /** @var int */
31
    public $channel;
32
33
    /** @var float */
34
    public $quality;
35
36
    /** @var float */
37
    public $dbm;
38
39
    /** @var string */
40
    public $security;
41
42
    /** @var string */
43
    public $securityFlags;
44
45
    /** @var int */
46
    public $frequency;
47
48
    /** @var bool */
49
    public $connected;
50
51
    /** @var array */
52
    protected static $securityTypes = [
53
        self::WPA2_SECURITY,
54
        self::WPA_SECURITY,
55
        self::WEP_SECURITY,
56
    ];
57
58
    /** @var Command */
59
    protected $command;
60
61
    /**
62
     * AbstractNetwork constructor.
63
     *
64
     * @param Command $command
65
     */
66
    public function __construct(Command $command)
67
    {
68
        $this->command = $command;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getSecurityType(): string
75
    {
76
        $securityType = self::UNKNOWN_SECURITY;
77
78
        foreach (self::$securityTypes as $securityType) {
79
            if (strpos($this->security, $securityType) !== false) {
80
                break;
81
            }
82
        }
83
84
        return $securityType;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function __toString(): string
91
    {
92
        return implode('|', [
93
            $this->bssid,
94
            $this->ssid,
95
            $this->quality,
96
            $this->dbm,
97
            $this->security,
98
            $this->securityFlags,
99
            $this->frequency,
100
            var_export($this->connected, true),
101
        ]);
102
    }
103
104
    /**
105
     * @param string $password
106
     * @param string $device
107
     */
108
    abstract public function connect(string $password, string $device): void;
109
110
    /**
111
     * @param string $device
112
     */
113
    abstract public function disconnect(string $device): void;
114
115
    /**
116
     * @param array $network
117
     *
118
     * @return AbstractNetwork
119
     */
120
    abstract public function createFromArray(array $network): self;
121
}
122