Completed
Push — master ( 855909...5f683c )
by Mr
01:20
created

Config::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\Exception;
6
use RouterOS\Interfaces\ConfigInterface;
7
8
/**
9
 * Class Config
10
 * @package RouterOS
11
 * @since 0.1
12
 */
13
class Config implements ConfigInterface
14
{
15
    /**
16
     * Array of parameters (with defaults)
17
     * @var array
18
     */
19
    private $_parameters = [
20
        'legacy' => Client::LEGACY,
21
        'ssl' => Client::SSL,
22
        'timeout' => Client::TIMEOUT,
23
        'attempts' => Client::ATTEMPTS,
24
        'delay' => Client::ATTEMPTS_DELAY
25
    ];
26
27
    /**
28
     * Check if key in array
29
     *
30
     * @param   string $key
31
     * @param   array $array
32
     * @throws  Exception
33
     */
34
    private function keyAllowed(string $key, array $array)
35
    {
36
        // Check if parameter in list of allowed parameters
37
        if (!array_key_exists($key, $array)) {
38
            throw new Exception("Requested parameter '$key' not found in allowed list [" . implode(',',
39
                    array_keys($array)) . ']');
40
        }
41
    }
42
43
    /**
44
     * Set parameter into array
45
     *
46
     * @param   string $name
47
     * @param   mixed $value
48
     * @return  ConfigInterface
49
     */
50
    public function set(string $name, $value): ConfigInterface
51
    {
52
        try {
53
54
            // Check of key in array
55
            $this->keyAllowed($name, self::ALLOWED);
56
57
            $whatType = \gettype($value);
58
            $isType = self::ALLOWED[$name];
59
60
            // Check what type has this value
61
            if ($whatType !== $isType) {
62
                throw new Exception("Parameter '$name' has wrong type '$whatType'' but should be '$isType''");
63
            }
64
65
        } catch (Exception $e) {
66
            // __construct
67
        }
68
69
        // Save value to array
70
        $this->_parameters[$name] = $value;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Return port number (get from defaults if port is not set by user)
77
     *
78
     * @param   string $parameter
79
     * @return  bool|int
80
     */
81
    private function getPort(string $parameter)
82
    {
83
        // If client need port number and port is not set
84
        if ($parameter === 'port' && !isset($this->_parameters['port'])) {
85
            // then use default with or without ssl encryption
86
            return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
87
                ? Client::PORT_SSL
88
                : Client::PORT;
89
        }
90
        return null;
91
    }
92
93
    /**
94
     * Return parameter of current config by name
95
     *
96
     * @param   string $parameter
97
     * @return  mixed
98
     */
99
    public function get(string $parameter)
100
    {
101
        try {
102
            // Check of key in array
103
            $this->keyAllowed($parameter, self::ALLOWED);
104
105
        } catch (Exception $e) {
106
            // __construct
107
        }
108
109
        return $this->getPort($parameter) ?? $this->_parameters[$parameter];
110
    }
111
112
    /**
113
     * Return array with all parameters of configuration
114
     *
115
     * @return  array
116
     */
117
    public function getParameters(): array
118
    {
119
        return $this->_parameters;
120
    }
121
}
122