AbstractNetworks::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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