Completed
Push — master ( 52e6bb...f12d15 )
by Mr
02:17
created

Config   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 127
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A keyAllowed() 0 7 2
A keyType() 0 6 2
A set() 0 13 1
A getPort() 0 11 5
A delete() 0 10 1
A get() 0 7 1
A getParameters() 0 4 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
 * @package RouterOS
11
 * @since 0.1
12
 */
13
class Config implements ConfigInterface
14
{
15
    /**
16
     * Array of parameters (with some default values)
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 list of allowed parameters
29
     *
30
     * @param   string $key
31
     * @param   array $array
32
     * @throws  ConfigException
33
     */
34 7
    private function keyAllowed(string $key, array $array)
35
    {
36 7
        if (!array_key_exists($key, $array)) {
37 3
            throw new ConfigException("Requested parameter '$key' not found in allowed list [" . implode(',',
38 3
                    array_keys($array)) . ']');
39
        }
40 4
    }
41
42
    /**
43
     * Compare data types of some value
44
     *
45
     * @param   string $name Name of value
46
     * @param   mixed $whatType What type has value
47
     * @param   mixed $isType What type should be
48
     * @throws  ConfigException
49
     */
50 3
    private function keyType(string $name, $whatType, $isType)
51
    {
52 3
        if ($whatType !== $isType) {
53 1
            throw new ConfigException("Parameter '$name' has wrong type '$whatType'' but should be '$isType''");
54
        }
55 2
    }
56
57
    /**
58
     * Set parameter into array
59
     *
60
     * @param   string $name
61
     * @param   mixed $value
62
     * @return  ConfigInterface
63
     * @throws  ConfigException
64
     */
65 4
    public function set(string $name, $value): ConfigInterface
66
    {
67
        // Check of key in array
68 4
        $this->keyAllowed($name, self::ALLOWED);
69
70
        // Check what type has this value
71 3
        $this->keyType($name, \gettype($value), self::ALLOWED[$name]);
72
73
        // Save value to array
74 2
        $this->_parameters[$name] = $value;
75
76 2
        return $this;
77
    }
78
79
    /**
80
     * Return port number (get from defaults if port is not set by user)
81
     *
82
     * @param   string $parameter
83
     * @return  bool|int
84
     */
85 1
    private function getPort(string $parameter)
86
    {
87
        // If client need port number and port is not set
88 1
        if ($parameter === 'port' && !isset($this->_parameters['port'])) {
89
            // then use default with or without ssl encryption
90 1
            return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
91 1
                ? Client::PORT_SSL
92 1
                : Client::PORT;
93
        }
94 1
        return null;
95
    }
96
97
    /**
98
     * Remove parameter from array by name
99
     *
100
     * @param   string $parameter
101
     * @return  ConfigInterface
102
     * @throws  ConfigException
103
     */
104 3
    public function delete(string $parameter): ConfigInterface
105
    {
106
        // Check of key in array
107 3
        $this->keyAllowed($parameter, self::ALLOWED);
108
109
        // Save value to array
110 2
        unset($this->_parameters[$parameter]);
111
112 2
        return $this;
113
    }
114
115
    /**
116
     * Return parameter of current config by name
117
     *
118
     * @param   string $parameter
119
     * @return  mixed
120
     * @throws  ConfigException
121
     */
122 2
    public function get(string $parameter)
123
    {
124
        // Check of key in array
125 2
        $this->keyAllowed($parameter, self::ALLOWED);
126
127 1
        return $this->getPort($parameter) ?? $this->_parameters[$parameter];
128
    }
129
130
    /**
131
     * Return array with all parameters of configuration
132
     *
133
     * @return  array
134
     */
135 3
    public function getParameters(): array
136
    {
137 3
        return $this->_parameters;
138
    }
139
}
140