Passed
Pull Request — master (#5)
by Aleksandr
02:11
created

AbstractNetworks::setNetworks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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