Completed
Push — master ( bda266...ce7873 )
by Aleksandr
15s queued 10s
created

FrequencyGenerator::setGeneratedFrequencies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Sanchescom\WiFi\System;
4
5
/**
6
 * Class FrequencyGenerator.
7
 */
8
class FrequencyGenerator
9
{
10
    /**
11
     * @var array
12
     */
13
    protected static $frequencies = [];
14
15
    /**
16
     * Array description:
17
     * <code>
18
     * $frequencySettings = [
19
     *      [
20
     *          2412, // frequency starts from
21
     *          1,    // channel starts from
22
     *          14,   // channel ends till
23
     *          5,    // frequency step
24
     *          1,    // frequency increasing
25
     *      ],
26
     * ];
27
     * </code>.
28
     *
29
     * @var array[int][int]int
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[int][int]int at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
30
     */
31
    protected $frequencySettings = [
32
        [2412, 1, 14, 5, 1],
33
        [5180, 36, 64, 10, 2],
34
        [5500, 100, 144, 10, 2],
35
        [5745, 149, 161, 10, 2],
36
        [5825, 165, 173, 20, 4],
37
    ];
38
39
    /**
40
     * @param int $channel
41
     *
42
     * @return mixed
43
     */
44
    public function getFrequencyForChannel(int $channel)
45
    {
46
        return $this->generateFrequencies()[$channel];
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    protected function generateFrequencies(): array
53
    {
54
        if (empty(self::$frequencies)) {
55
            foreach ($this->frequencySettings as $frequencySetting) {
56
                $this->setGeneratedFrequencies($frequencySetting);
57
            }
58
        }
59
60
        return self::$frequencies;
61
    }
62
63
    /**
64
     * @param array $frequencySetting
65
     */
66
    protected function setGeneratedFrequencies(array $frequencySetting): void
67
    {
68
        list(
69
            $frequencyStart,
70
            $channelStart,
71
            $channelEnd,
72
            $frequencyStep,
73
            $frequencyIncreasing
74
            ) = $frequencySetting;
75
76
        for ($i = $channelStart; $i <= $channelEnd; $i += $frequencyIncreasing) {
77
            self::$frequencies[$i] = $frequencyStart;
78
79
            $frequencyStart += $frequencyStep;
80
        }
81
    }
82
}
83