Completed
Push — master ( 55cea2...4dcdcf )
by Mr
04:08
created

Config::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\ConfigException;
6
use RouterOS\Interfaces\ConfigInterface;
7
8
/**
9
 * Class Config with array of parameters
10
 *
11
 * @package RouterOS
12
 * @since   0.1
13
 */
14
class Config implements ConfigInterface
15
{
16
    /**
17
     * Array of parameters (with some default values)
18
     *
19
     * @var array
20
     */
21
    private $_parameters = [
22
        'legacy'   => Client::LEGACY,
23
        'ssl'      => Client::SSL,
24
        'timeout'  => Client::TIMEOUT,
25
        'attempts' => Client::ATTEMPTS,
26
        'delay'    => Client::ATTEMPTS_DELAY
27
    ];
28
29
    /**
30
     * Config constructor.
31
     *
32
     * @param   array $parameters List of parameters which can be set on object creation stage
33
     * @throws  ConfigException
34
     * @since   0.6
35
     */
36 14
    public function __construct(array $parameters = [])
37
    {
38 14
        foreach ($parameters as $key => $value) {
39
            $this->set($key, $value);
40
        }
41 14
    }
42
43
    /**
44
     * Check if key in list of parameters
45
     *
46
     * @param   string $key
47
     * @param   array  $array
48
     * @throws  ConfigException
49
     */
50 12
    private function exceptionIfKeyNotExist(string $key, array $array)
51
    {
52 12
        if (!array_key_exists($key, $array)) {
53 3
            throw new ConfigException("Requested parameter '$key' not found in list [" . implode(',', array_keys($array)) . ']');
54
        }
55 9
    }
56
57
    /**
58
     * Compare data types of some value
59
     *
60
     * @param   string $name     Name of value
61
     * @param   mixed  $whatType What type has value
62
     * @param   mixed  $isType   What type should be
63
     * @throws  ConfigException
64
     */
65 8
    private function exceptionIfTypeMismatch(string $name, $whatType, $isType)
66
    {
67 8
        if ($whatType !== $isType) {
68 1
            throw new ConfigException("Parameter '$name' has wrong type '$whatType' but should be '$isType'");
69
        }
70 7
    }
71
72
    /**
73
     * Set parameter into array
74
     *
75
     * @param   string $name
76
     * @param   mixed  $value
77
     * @return  \RouterOS\Config
78
     * @throws  ConfigException
79
     */
80 9
    public function set(string $name, $value): Config
81
    {
82
        // Check of key in array
83 9
        $this->exceptionIfKeyNotExist($name, self::ALLOWED);
84
85
        // Check what type has this value
86 8
        $this->exceptionIfTypeMismatch($name, \gettype($value), self::ALLOWED[$name]);
87
88
        // Save value to array
89 7
        $this->_parameters[$name] = $value;
90
91 7
        return $this;
92
    }
93
94
    /**
95
     * Return port number (get from defaults if port is not set by user)
96
     *
97
     * @param   string $parameter
98
     * @return  bool|int
99
     */
100 6
    private function getPort(string $parameter)
101
    {
102
        // If client need port number and port is not set
103 6
        if ($parameter === 'port' && !isset($this->_parameters['port'])) {
104
            // then use default with or without ssl encryption
105 4
            return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
106 1
                ? Client::PORT_SSL
107 4
                : Client::PORT;
108
        }
109 6
        return null;
110
    }
111
112
    /**
113
     * Remove parameter from array by name
114
     *
115
     * @param   string $parameter
116
     * @return  \RouterOS\Config
117
     * @throws  ConfigException
118
     */
119 3
    public function delete(string $parameter): Config
120
    {
121
        // Check of key in array
122 3
        $this->exceptionIfKeyNotExist($parameter, self::ALLOWED);
123
124
        // Save value to array
125 2
        unset($this->_parameters[$parameter]);
126
127 2
        return $this;
128
    }
129
130
    /**
131
     * Return parameter of current config by name
132
     *
133
     * @param   string $parameter
134
     * @return  mixed
135
     * @throws  ConfigException
136
     */
137 7
    public function get(string $parameter)
138
    {
139
        // Check of key in array
140 7
        $this->exceptionIfKeyNotExist($parameter, self::ALLOWED);
141
142 6
        return $this->getPort($parameter) ?? $this->_parameters[$parameter];
143
    }
144
145
    /**
146
     * Return array with all parameters of configuration
147
     *
148
     * @return  array
149
     */
150 8
    public function getParameters(): array
151
    {
152 8
        return $this->_parameters;
153
    }
154
}
155