Test Setup Failed
Push — master ( b2f5f7...e10ced )
by Mr
02:35
created

Config::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
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
     * @throws  Exception
50
     */
51
    public function set(string $name, $value): ConfigInterface
52
    {
53
        // Check of key in array
54
        $this->keyAllowed($name, self::ALLOWED);
55
56
        $whatType = \gettype($value);
57
        $isType = self::ALLOWED[$name];
58
59
        // Check what type has this value
60
        if ($whatType !== $isType) {
61
            throw new Exception("Parameter '$name' has wrong type '$whatType'' but should be '$isType''");
62
        }
63
64
        // Save value to array
65
        $this->_parameters[$name] = $value;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Return port number (get from defaults if port is not set by user)
72
     *
73
     * @param   string $parameter
74
     * @return  bool|int
75
     */
76
    private function getPort(string $parameter)
77
    {
78
        // If client need port number and port is not set
79
        if ($parameter === 'port' && !isset($this->_parameters['port'])) {
80
            // then use default with or without ssl encryption
81
            return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
82
                ? Client::PORT_SSL
83
                : Client::PORT;
84
        }
85
        return null;
86
    }
87
88
    /**
89
     * Remove parameter from array by name
90
     *
91
     * @param   string $parameter
92
     * @return  ConfigInterface
93
     * @throws  Exception
94
     */
95
    public function delete(string $parameter): ConfigInterface
96
    {
97
        // Check of key in array
98
        $this->keyAllowed($parameter, self::ALLOWED);
99
100
        // Save value to array
101
        unset($this->_parameters[$parameter]);
102
103
        return $this;
104
    }
105
106
    /**
107
     * Return parameter of current config by name
108
     *
109
     * @param   string $parameter
110
     * @return  mixed
111
     * @throws  Exception
112
     */
113
    public function get(string $parameter)
114
    {
115
        // Check of key in array
116
        $this->keyAllowed($parameter, self::ALLOWED);
117
118
        return $this->getPort($parameter) ?? $this->_parameters[$parameter];
119
    }
120
121
    /**
122
     * Return array with all parameters of configuration
123
     *
124
     * @return  array
125
     */
126
    public function getParameters(): array
127
    {
128
        return $this->_parameters;
129
    }
130
}
131