Networks::checkNetworkConnection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sanchescom\WiFi\System\Windows;
6
7
use Sanchescom\WiFi\System\AbstractNetwork;
8
use Sanchescom\WiFi\System\AbstractNetworks;
9
use Sanchescom\WiFi\System\Separable;
10
11
/**
12
 * Class Networks.
13
 * {@inheritdoc}
14
 */
15
class Networks extends AbstractNetworks
16
{
17
    use Separable;
18
19
    /** @var int */
20
    const BSSID_KEY = 4;
21
22
    /** @var int */
23
    const EXTRACT_BSSID_KEY = 1;
24
25
    /** @var int */
26
    const ZERO_KEY = 0;
27
28
    /** @var int */
29
    const NETWORK_DESCRIPTION_ROWS_AMOUNT = 11;
30
31
    /** @var int */
32
    const NETWORK_DESCRIPTION_BLOCK_STEP = 1;
33
34
    /**
35
     * @return string
36
     */
37
    protected function getCommand(): string
38
    {
39
        return glue_commands(
40
            'chcp 65001',
41
            'netsh wlan show networks mode=Bssid',
42
            'echo '.$this->separator,
43
            'netsh wlan show interfaces'
44
        );
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function getNetwork(): AbstractNetwork
51
    {
52
        return new Network($this->command);
53
    }
54
55
    /**
56
     * @param string $output
57
     *
58
     * @return array
59
     */
60
    protected function extractingNetworks(string $output): array
61
    {
62
        list($networks, $current) = $this->explodeOutput($output);
63
64
        $currentBssid = extract_bssid($current, self::EXTRACT_BSSID_KEY);
65
66
        $availableNetworks = $this->explodeAvailableNetworks($networks);
67
68
        $countAvailableNetworks = count($availableNetworks);
69
70
        $groupedNetworks = [];
71
72
        for ($i = 10, $j = 5, $k = 0; $countAvailableNetworks >= $j; $i--, $j++) {
73
            if ($this->isStartNetworkDescription($i)) {
74
                $this->checkNetworkConnection($groupedNetworks, $currentBssid, $k);
75
76
                list($i, $k) = $this->nextNetwork($k);
77
            } else {
78
                $groupedNetworks[$k][] = extract_after($availableNetworks[$j]);
79
            }
80
        }
81
82
        return $groupedNetworks;
83
    }
84
85
    /**
86
     * Checking which network currently connected and set a flag.
87
     *
88
     * @param array $groupedNetworks
89
     * @param array $currentBssid
90
     * @param int   $networkBlockIndex
91
     */
92
    private function checkNetworkConnection(array &$groupedNetworks, array $currentBssid, int $networkBlockIndex): void
93
    {
94
        if (in_array($groupedNetworks[$networkBlockIndex][self::BSSID_KEY], $currentBssid)) {
95
            $groupedNetworks[$networkBlockIndex][] = true;
96
        }
97
    }
98
99
    /**
100
     * Checking that iterable row is start of description.
101
     *
102
     * @param int $firstRowIndex
103
     *
104
     * @return bool
105
     */
106
    private function isStartNetworkDescription(int $firstRowIndex): bool
107
    {
108
        return $firstRowIndex == self::ZERO_KEY;
109
    }
110
111
    /**
112
     * Setting vars to state for new iteration of processing network description.
113
     *
114
     * @param int $nextRowIndex
115
     *
116
     * @return array
117
     */
118
    private function nextNetwork(int $nextRowIndex): array
119
    {
120
        return [self::NETWORK_DESCRIPTION_ROWS_AMOUNT, $nextRowIndex + self::NETWORK_DESCRIPTION_BLOCK_STEP];
121
    }
122
}
123