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

AbstractNetworksCollection::getBySsid()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sanchescom\WiFi\System;
6
7
use Exception;
8
9
/**
10
 * Class AbstractNetworksCollection.
11
 */
12
abstract class AbstractNetworksCollection
13
{
14
    /**
15
     * @var AbstractNetwork[]
16
     */
17
    protected $networks;
18
19
    /** @var Command */
20
    protected $command;
21
22
    /**
23
     * @param string $output
24
     *
25
     * @return array
26
     */
27
    abstract protected function extractingNetworks(string $output): array;
28
29
    /**
30
     * @return string
31
     */
32
    abstract protected function getNetwork():? string;
33
34
    /**
35
     * @return string
36
     */
37
    abstract protected function getCommand(): string;
38
39
    /**
40
     * AbstractNetworksCollection constructor.
41
     *
42
     * @param Command $command
43
     */
44
    public function __construct(Command $command)
45
    {
46
        $this->command = $command;
47
    }
48
49
    /**
50
     * @throws Exception
51
     *
52
     * @return Collection
53
     */
54
    public function scan(): Collection
55
    {
56
        $output = $this->command->execute($this->getCommand());
57
58
        $this->setNetworks(
59
            $this->extractingNetworks($output)
60
        );
61
62
        return new Collection($this->networks);
63
    }
64
65
    /**
66
     * @param array $networks
67
     *
68
     * @return void
69
     */
70
    protected function setNetworks(array $networks): void
71
    {
72
        $this->networks = array_map(function (array $network) {
73
            return call_user_func_array([
74
                $this->getNetwork(),
75
                'createFromArray',
76
            ], [
77
                $network,
78
                $this->command,
79
            ]);
80
        }, $networks);
81
    }
82
83
    /**
84
     * @param string $networksString
85
     *
86
     * @return array
87
     */
88
    protected function explodeAvailableNetworks(string $networksString): array
89
    {
90
        return explode("\n", trim($networksString));
91
    }
92
}
93