Network::connect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sanchescom\WiFi\System\Windows;
6
7
use Sanchescom\WiFi\Contracts\FrequencyInterface;
8
use Sanchescom\WiFi\System\AbstractNetwork;
9
use Sanchescom\WiFi\System\Frequency;
10
11
/**
12
 * Class Network.
13
 */
14
class Network extends AbstractNetwork implements FrequencyInterface
15
{
16
    use Frequency;
17
18
    /**
19
     * @param string $password
20
     * @param string $device
21
     *
22
     * @throws \Exception
23
     */
24
    public function connect(string $password, string $device): void
25
    {
26
        $command = glue_commands(
27
            sprintf('netsh wlan add profile filename="%s"', $this->getProfileService()->create($password)),
28
            sprintf('netsh wlan connect interface="%s" ssid="%s" name="%s"', $device, $this->ssid, $this->ssid)
29
        );
30
31
        $this->getCommand()->execute($command);
32
33
        $this->getProfileService()->delete();
34
    }
35
36
    /**
37
     * @param string $device
38
     *
39
     * @throws \Exception
40
     */
41
    public function disconnect(string $device): void
42
    {
43
        $this->getCommand()->execute(sprintf('netsh wlan disconnect interface="%s"', $device));
44
    }
45
46
    /**
47
     * @param array $network
48
     *
49
     * @return \Sanchescom\WiFi\System\Windows\Network
50
     */
51
    public function createFromArray(array $network): AbstractNetwork
52
    {
53
        $this->ssid = $network[0];
54
        $this->bssid = $network[4];
55
        $this->channel = (int) $network[7];
56
        $this->security = $network[2];
57
        $this->securityFlags = $network[3];
58
        $this->quality = (int) $network[5];
59
        $this->frequency = $this->getFrequency();
60
        $this->dbm = to_dbm((int) $network[5]);
61
        $this->connected = isset($network[10]);
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return Profile
68
     */
69
    protected function getProfileService()
70
    {
71
        return new Profile($this->ssid, $this->getSecurityType());
72
    }
73
}
74