Completed
Push — master ( bda266...ce7873 )
by Aleksandr
15s queued 10s
created

AbstractNetwork::getFrequencyGenerator()   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
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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
    /**
13
     * @var string
14
     */
15
    public $bssid;
16
17
    /**
18
     * @var string
19
     */
20
    public $ssid;
21
22
    /**
23
     * @var int
24
     */
25
    public $channel;
26
27
    /**
28
     * @var float
29
     */
30
    public $quality;
31
32
    /**
33
     * @var float
34
     */
35
    public $dbm;
36
37
    /**
38
     * @var string
39
     */
40
    public $security;
41
42
    /**
43
     * @var string
44
     */
45
    public $securityFlags;
46
47
    /**
48
     * @var int
49
     */
50
    public $frequency;
51
52
    /**
53
     * @var bool
54
     */
55
    public $connected;
56
57
    /**
58
     * @var array
59
     */
60
    protected static $securityTypes = [
61
        'WPA2',
62
        'WPA',
63
        'WEP',
64
    ];
65
66
    /**
67
     * @param string $password
68
     * @param string $device
69
     */
70
    abstract public function connect(string $password, string $device): void;
71
72
    /**
73
     * @param string $device
74
     */
75
    abstract public function disconnect(string $device): void;
76
77
    /**
78
     * @param array $network
79
     *
80
     * @return AbstractNetwork
81
     */
82
    abstract public static function createFromArray(array $network): self;
83
84
    /**
85
     * @return string
86
     */
87
    public function getSecurityType(): string
88
    {
89
        $securityType = 'Unknown';
90
91
        foreach (self::$securityTypes as $securityType) {
92
            if (strpos($this->security, $securityType) !== false) {
93
                break;
94
            }
95
        }
96
97
        return $securityType;
98
    }
99
100
    /**
101
     * @return int
102
     */
103
    public function getFrequency(): int
104
    {
105
        return $this->getFrequencyGenerator()->getFrequencyForChannel($this->channel);
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function __toString(): string
112
    {
113
        return implode('|', [
114
            $this->bssid,
115
            $this->ssid,
116
            $this->quality,
117
            $this->dbm,
118
            $this->security,
119
            $this->securityFlags,
120
            $this->frequency,
121
            var_export($this->connected, true),
122
        ]);
123
    }
124
125
    /**
126
     * @return FrequencyGenerator
127
     */
128
    protected function getFrequencyGenerator(): FrequencyGenerator
129
    {
130
        return new FrequencyGenerator();
131
    }
132
133
    /**
134
     * @return float
135
     */
136
    protected function dBmToQuality(): float
137
    {
138
        return 2 * ($this->dbm + 100);
139
    }
140
141
    /**
142
     * @return float
143
     */
144
    protected function qualityToDBm(): float
145
    {
146
        return ($this->quality / 2) - 100;
147
    }
148
}
149