Passed
Pull Request — master (#5)
by Aleksandr
01:36
created

NetworksCollection::getCommand()   A

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 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sanchescom\WiFi\System\Mac;
6
7
use Sanchescom\WiFi\System\AbstractNetworksCollection;
8
use Sanchescom\WiFi\System\Separable;
9
10
/**
11
 * Class NetworksCollection.
12
 */
13
class NetworksCollection extends AbstractNetworksCollection
14
{
15
    use Separable;
16
17
    /**
18
     * @var int
19
     */
20
    const BSSID_KEY = 1;
21
22
    /**
23
     * @return string
24
     */
25
    protected function getCommand(): string
26
    {
27
        $utility = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
28
29
        return sprintf('%s -s && echo "%s" && %s --getinfo', $utility, $this->separator, $utility);
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    protected function getNetwork():? string
36
    {
37
        return Network::class;
38
    }
39
40
    /**
41
     * @param string $output
42
     *
43
     * @return array
44
     */
45
    public function extractingNetworks($output): array
46
    {
47
        list($networks, $current) = $this->explodeOutput($output);
48
49
        $currentBssid = extract_bssid($current, 0);
50
51
        $availableNetworks = $this->explodeAvailableNetworks($networks);
52
53
        array_shift($availableNetworks);
54
55
        array_walk($availableNetworks, function (&$networkData) use ($currentBssid) {
56
            $networkData = $this->extractingDataFromString($networkData);
57
58
            if (in_array($networkData[self::BSSID_KEY], $currentBssid)) {
59
                array_push($networkData, true);
60
            }
61
        });
62
63
        return $availableNetworks;
64
    }
65
66
    /**
67
     * @param string $networkData
68
     *
69
     * @return array
70
     */
71
    protected function extractingDataFromString(string $networkData): array
72
    {
73
        $extractedProperties = [];
74
75
        $pattern = '/(.*?)'.
76
        '(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})\s{1,}'.
77
        '([-+]?[0-9]*)\s{1,}'.
78
        '([^a-zA-Z]*)'.
79
        '(\w{1,})\s{1,}'.
80
        '([\w-]+)'.
81
        '(.*)/';
82
83
        preg_match_all(
84
            $pattern,
85
            $networkData,
86
            $extractedProperties
87
        );
88
89
        array_shift($extractedProperties);
90
91
        return trim_first($extractedProperties);
92
    }
93
}
94