Completed
Pull Request — master (#40)
by Mr
06:35
created

Config::getPort()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.1666

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6.1666
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
use function gettype;
10
11
/**
12
 * Class Config with array of parameters
13
 *
14
 * @package RouterOS
15
 * @since   0.1
16
 */
17
class Config implements ConfigInterface
18
{
19
    /**
20
     * By default legacy login on RouterOS pre-6.43 is not supported
21
     */
22
    public const LEGACY = false;
23
24
    /**
25
     * Default port number
26
     */
27
    public const PORT = 8728;
28
29
    /**
30
     * Default ssl port number
31
     */
32
    public const PORT_SSL = 8729;
33
34
    /**
35
     * Do not use SSL by default
36
     */
37
    public const SSL = false;
38
39
    /**
40
     * Max timeout for answer from router
41
     */
42
    public const TIMEOUT = 10;
43
44
    /**
45
     * Count of reconnect attempts
46
     */
47
    public const ATTEMPTS = 10;
48
49
    /**
50
     * Delay between attempts in seconds
51
     */
52
    public const ATTEMPTS_DELAY = 1;
53
54
    /**
55
     * Delay between attempts in seconds
56
     */
57
    public const SSH_PORT = 22;
58
59
    /**
60
     * List of allowed parameters of config
61
     */
62
    public const ALLOWED = [
63
        'host'     => 'string',  // Address of Mikrotik RouterOS
64
        'user'     => 'string',  // Username
65
        'pass'     => 'string',  // Password
66
        'port'     => 'integer', // RouterOS API port number for access (if not set use default or default with SSL if SSL enabled)
67
        'ssl'      => 'boolean', // Enable ssl support (if port is not set this parameter must change default port to ssl port)
68
        'legacy'   => 'boolean', // Support of legacy login scheme (true - pre 6.43, false - post 6.43)
69
        'timeout'  => 'integer', // Max timeout for answer from RouterOS
70
        'attempts' => 'integer', // Count of attempts to establish TCP session
71
        'delay'    => 'integer', // Delay between attempts in seconds
72
        'ssh_port' => 'integer', // Number of SSH port
73
    ];
74
75
    /**
76
     * Array of parameters (with some default values)
77
     *
78
     * @var array
79
     */
80
    private $_parameters = [
81
        'legacy'   => self::LEGACY,
82
        'ssl'      => self::SSL,
83
        'timeout'  => self::TIMEOUT,
84
        'attempts' => self::ATTEMPTS,
85
        'delay'    => self::ATTEMPTS_DELAY,
86
        'ssh_port' => self::SSH_PORT,
87
    ];
88
89
    /**
90
     * Config constructor.
91
     *
92
     * @param array $parameters List of parameters which can be set on object creation stage
93
     *
94
     * @throws \RouterOS\Exceptions\ConfigException
95
     * @since  0.6
96
     */
97 9
    public function __construct(array $parameters = [])
98
    {
99 9
        foreach ($parameters as $key => $value) {
100 7
            $this->set($key, $value);
101
        }
102 9
    }
103
104
    /**
105
     * @inheritDoc
106
     *
107
     * @throws \RouterOS\Exceptions\ConfigException when name of configuration key is invalid or not allowed
108
     */
109 9
    public function set(string $name, $value): ConfigInterface
110
    {
111
        // Check of key in array
112 9
        if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) {
113
            throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']');
114
        }
115
116
        // Check what type has this value
117 9
        if (TypeHelper::checkIfTypeMismatch(gettype($value), self::ALLOWED[$name])) {
118
            throw new ConfigException("Parameter '$name' has wrong type '" . gettype($value) . "' but should be '" . self::ALLOWED[$name] . "'");
119
        }
120
121
        // Save value to array
122 9
        $this->_parameters[$name] = $value;
123
124 9
        return $this;
125
    }
126
127
    /**
128
     * Return port number (get from defaults if port is not set by user)
129
     *
130
     * @param string $parameter
131
     *
132
     * @return bool|int
133
     */
134 8
    private function getPort(string $parameter)
135
    {
136
        // If client need port number and port is not set
137 8
        if ($parameter === 'port' && (!isset($this->_parameters['port']) || null === $this->_parameters['port'])) {
138
            // then use default with or without ssl encryption
139 5
            return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
140
                ? self::PORT_SSL
141 5
                : self::PORT;
142
        }
143 8
        return null;
144
    }
145
146
    /**
147
     * @inheritDoc
148
     *
149
     * @throws \RouterOS\Exceptions\ConfigException when parameter is not allowed
150
     */
151
    public function delete(string $name): ConfigInterface
152
    {
153
        // Check of key in array
154
        if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) {
155
            throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']');
156
        }
157
158
        // Save value to array
159
        unset($this->_parameters[$name]);
160
161
        return $this;
162
    }
163
164
    /**
165
     * @inheritDoc
166
     *
167
     * @throws \RouterOS\Exceptions\ConfigException when parameter is not allowed
168
     */
169 8
    public function get(string $name)
170
    {
171
        // Check of key in array
172 8
        if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) {
173
            throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']');
174
        }
175
176 8
        return $this->getPort($name) ?? $this->_parameters[$name];
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182 9
    public function getParameters(): array
183
    {
184 9
        return $this->_parameters;
185
    }
186
}
187