Passed
Push — master ( f7fd98...6717a3 )
by Aleksandr
01:32
created

NetworksCollection::isStartNetworkDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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