Network::connect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
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\Linux;
6
7
use Sanchescom\WiFi\System\AbstractNetwork;
8
9
/**
10
 * Class Network.
11
 */
12
class Network extends AbstractNetwork
13
{
14
    /** @var string */
15
    protected const POSITIVE_CONNECTION_FLAG = 'yes';
16
17
    /**
18
     * @param string $password
19
     * @param string $device
20
     *
21
     * @throws \Exception
22
     */
23
    public function connect(string $password, string $device): void
24
    {
25
        $format = 'LANG=C nmcli -w 10 device wifi connect "%s" password "%s" ifname "%s"';
26
27
        $this->getCommand()->execute(sprintf($format, $this->ssid, $password, $device));
28
    }
29
30
    /**
31
     * @param string $device
32
     *
33
     * @throws \Exception
34
     */
35
    public function disconnect(string $device): void
36
    {
37
        $this->getCommand()->execute(sprintf('LANG=C nmcli device disconnect %s', $device));
38
    }
39
40
    /**
41
     * @param array $network
42
     *
43
     * @return \Sanchescom\WiFi\System\Linux\Network
44
     */
45
    public function createFromArray(array $network): AbstractNetwork
46
    {
47
        $this->ssid = $network[1];
48
        $this->bssid = $network[2];
49
        $this->channel = (int) $network[4];
50
        $this->security = $network[7];
51
        $this->securityFlags = $network[8].' '.$network[9];
52
        $this->dbm = $network[6];
53
        $this->quality = to_quality((int) $network[6]);
54
        $this->frequency = (int) $network[5];
55
        $this->connected = ($network[0] == self::POSITIVE_CONNECTION_FLAG);
56
57
        return $this;
58
    }
59
}
60