|
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
|
|
|
* Set parameter into array |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $name |
|
31
|
|
|
* @param mixed $value |
|
32
|
|
|
* @return ConfigInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
public function set(string $name, $value): ConfigInterface |
|
35
|
|
|
{ |
|
36
|
|
|
try { |
|
37
|
|
|
|
|
38
|
|
|
// Check if parameter in list of allowed parameters |
|
39
|
|
View Code Duplication |
if (!array_key_exists($name, self::ALLOWED)) { |
|
|
|
|
|
|
40
|
|
|
throw new Exception("Requested parameter \"$name\" not found in allowed list [" . implode(',', |
|
41
|
|
|
array_keys(self::ALLOWED)) . ']'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Get type of current variable |
|
45
|
|
|
$whatType = \gettype($value); |
|
46
|
|
|
// Get allowed type of parameter |
|
47
|
|
|
$type = self::ALLOWED[$name]; |
|
48
|
|
|
$isType = 'is_' . $type; |
|
49
|
|
|
|
|
50
|
|
|
// Check what type has this value |
|
51
|
|
|
if (!$isType($value)) { |
|
52
|
|
|
throw new Exception("Parameter \"$name\" has wrong type \"$whatType\" but should be \"$type\""); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
} catch (Exception $e) { |
|
56
|
|
|
// __construct |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Save value to array |
|
60
|
|
|
$this->_parameters[$name] = $value; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return parameter of current config by name |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $parameter |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public function get(string $parameter) |
|
72
|
|
|
{ |
|
73
|
|
|
try { |
|
74
|
|
|
// Check if parameter in list of allowed parameters |
|
75
|
|
View Code Duplication |
if (!array_key_exists($parameter, self::ALLOWED)) { |
|
|
|
|
|
|
76
|
|
|
throw new Exception("Requested parameter \"$parameter\" is not found in allowed list [" . implode(',', |
|
77
|
|
|
array_keys(self::ALLOWED)) . ']'); |
|
78
|
|
|
} |
|
79
|
|
|
} catch (Exception $e) { |
|
80
|
|
|
// __construct |
|
81
|
|
|
} |
|
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
|
|
|
|
|
91
|
|
|
return $this->_parameters[$parameter]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Return array with all parameters of configuration |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getParameters(): array |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->_parameters; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
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.