Passed
Pull Request — master (#5)
by Aleksandr
02:53
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 AbstractNetwork
31
     */
32
    abstract protected function getNetwork(): AbstractNetwork;
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 $this->getNetwork()->createFromArray($network);
74
        }, $networks);
75
    }
76
77
    /**
78
     * @param string $networksString
79
     *
80
     * @return array
81
     */
82
    protected function explodeAvailableNetworks(string $networksString): array
83
    {
84
        return explode("\n", trim($networksString));
85
    }
86
}
87