1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RouterOS; |
4
|
|
|
|
5
|
|
|
use RouterOS\Exceptions\ConfigException; |
6
|
|
|
use RouterOS\Helpers\ArrayHelper; |
7
|
|
|
use RouterOS\Helpers\TypeHelper; |
8
|
|
|
use RouterOS\Interfaces\ConfigInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Config with array of parameters |
12
|
|
|
* |
13
|
|
|
* @package RouterOS |
14
|
|
|
* @since 0.1 |
15
|
|
|
*/ |
16
|
|
|
class Config implements ConfigInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Array of parameters (with some default values) |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $_parameters = [ |
24
|
|
|
'legacy' => Client::LEGACY, |
25
|
|
|
'ssl' => Client::SSL, |
26
|
|
|
'timeout' => Client::TIMEOUT, |
27
|
|
|
'attempts' => Client::ATTEMPTS, |
28
|
|
|
'delay' => Client::ATTEMPTS_DELAY |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Config constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $parameters List of parameters which can be set on object creation stage |
35
|
|
|
* @throws ConfigException |
36
|
|
|
* @since 0.6 |
37
|
|
|
*/ |
38
|
21 |
|
public function __construct(array $parameters = []) |
39
|
|
|
{ |
40
|
21 |
|
foreach ($parameters as $key => $value) { |
41
|
11 |
|
$this->set($key, $value); |
42
|
|
|
} |
43
|
21 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set parameter into array |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* @param mixed $value |
50
|
|
|
* @return \RouterOS\Config |
51
|
|
|
* @throws ConfigException |
52
|
|
|
*/ |
53
|
15 |
|
public function set(string $name, $value): Config |
54
|
|
|
{ |
55
|
|
|
// Check of key in array |
56
|
15 |
|
if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { |
57
|
1 |
|
throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Check what type has this value |
61
|
14 |
|
if (TypeHelper::checkIfTypeMismatch(\gettype($value), self::ALLOWED[$name])) { |
62
|
1 |
|
throw new ConfigException("Parameter '$name' has wrong type '" . \gettype($value) . "' but should be '" . self::ALLOWED[$name] . "'"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Save value to array |
66
|
13 |
|
$this->_parameters[$name] = $value; |
67
|
|
|
|
68
|
13 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return port number (get from defaults if port is not set by user) |
73
|
|
|
* |
74
|
|
|
* @param string $parameter |
75
|
|
|
* @return bool|int |
76
|
|
|
*/ |
77
|
10 |
|
private function getPort(string $parameter) |
78
|
|
|
{ |
79
|
|
|
// If client need port number and port is not set |
80
|
10 |
|
if ($parameter === 'port' && !isset($this->_parameters['port']) && null !== $this->_parameters['port']) { |
81
|
|
|
// then use default with or without ssl encryption |
82
|
|
|
return (isset($this->_parameters['ssl']) && $this->_parameters['ssl']) |
83
|
|
|
? Client::PORT_SSL |
84
|
|
|
: Client::PORT; |
85
|
|
|
} |
86
|
10 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Remove parameter from array by name |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* @return \RouterOS\Config |
94
|
|
|
* @throws \RouterOS\Exceptions\ConfigException |
95
|
|
|
*/ |
96
|
2 |
View Code Duplication |
public function delete(string $name): Config |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
// Check of key in array |
99
|
2 |
|
if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { |
100
|
1 |
|
throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Save value to array |
104
|
1 |
|
unset($this->_parameters[$name]); |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return parameter of current config by name |
111
|
|
|
* |
112
|
|
|
* @param string $name |
113
|
|
|
* @return mixed |
114
|
|
|
* @throws \RouterOS\Exceptions\ConfigException |
115
|
|
|
*/ |
116
|
11 |
View Code Duplication |
public function get(string $name) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
// Check of key in array |
119
|
11 |
|
if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { |
120
|
1 |
|
throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); |
121
|
|
|
} |
122
|
|
|
|
123
|
10 |
|
return $this->getPort($name) ?? $this->_parameters[$name]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return array with all parameters of configuration |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
15 |
|
public function getParameters(): array |
132
|
|
|
{ |
133
|
15 |
|
return $this->_parameters; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.