Completed
Push — master ( de6ffa...f677f6 )
by Aleksandr
12s queued 11s
created

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