Passed
Pull Request — master (#5)
by Aleksandr
03:12
created

AbstractNetworksCollection::setNetworks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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