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

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