Completed
Push — master ( e929d6...be85d1 )
by Mr
04:49
created

Config::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
10
/**
11
 * Class Config with array of parameters
12
 *
13
 * @package RouterOS
14
 * @since   0.1
15
 */
16
class Config implements ConfigInterface
17
{
18
    /**
19
     * Array of parameters (with some default values)
20
     *
21
     * @var array
22
     */
23
    private $_parameters = [
24
        'legacy'   => Client::LEGACY,
25
        'ssl'      => Client::SSL,
26
        'timeout'  => Client::TIMEOUT,
27
        'attempts' => Client::ATTEMPTS,
28
        'delay'    => Client::ATTEMPTS_DELAY
29
    ];
30
31
    /**
32
     * Config constructor.
33
     *
34
     * @param array $parameters List of parameters which can be set on object creation stage
35
     *
36
     * @throws \RouterOS\Exceptions\ConfigException
37
     * @since  0.6
38
     */
39 28
    public function __construct(array $parameters = [])
40
    {
41 28
        foreach ($parameters as $key => $value) {
42 17
            $this->set($key, $value);
43
        }
44 28
    }
45
46
    /**
47
     * Set parameter into array
48
     *
49
     * @param string $name
50
     * @param mixed  $value
51
     *
52
     * @return \RouterOS\Config
53
     * @throws \RouterOS\Exceptions\ConfigException
54
     */
55 23
    public function set(string $name, $value): Config
56
    {
57
        // Check of key in array
58 23
        if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) {
59 1
            throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']');
60
        }
61
62
        // Check what type has this value
63 22
        if (TypeHelper::checkIfTypeMismatch(\gettype($value), self::ALLOWED[$name])) {
64 1
            throw new ConfigException("Parameter '$name' has wrong type '" . \gettype($value) . "' but should be '" . self::ALLOWED[$name] . "'");
65
        }
66
67
        // Save value to array
68 21
        $this->_parameters[$name] = $value;
69
70 21
        return $this;
71
    }
72
73
    /**
74
     * Return port number (get from defaults if port is not set by user)
75
     *
76
     * @param string $parameter
77
     *
78
     * @return bool|int
79
     */
80 17
    private function getPort(string $parameter)
81
    {
82
        // If client need port number and port is not set
83 17
        if ($parameter === 'port' && (!isset($this->_parameters['port']) || null === $this->_parameters['port'])) {
84
            // then use default with or without ssl encryption
85 14
            return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
86 1
                ? Client::PORT_SSL
87 14
                : Client::PORT;
88
        }
89 17
        return null;
90
    }
91
92
    /**
93
     * Remove parameter from array by name
94
     *
95
     * @param string $name
96
     *
97
     * @return \RouterOS\Config
98
     * @throws \RouterOS\Exceptions\ConfigException
99
     */
100 3 View Code Duplication
    public function delete(string $name): Config
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
    {
102
        // Check of key in array
103 3
        if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) {
104 1
            throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']');
105
        }
106
107
        // Save value to array
108 2
        unset($this->_parameters[$name]);
109
110 2
        return $this;
111
    }
112
113
    /**
114
     * Return parameter of current config by name
115
     *
116
     * @param string $name
117
     *
118
     * @return mixed
119
     * @throws \RouterOS\Exceptions\ConfigException
120
     */
121 18 View Code Duplication
    public function get(string $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
122
    {
123
        // Check of key in array
124 18
        if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) {
125 1
            throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']');
126
        }
127
128 17
        return $this->getPort($name) ?? $this->_parameters[$name];
129
    }
130
131
    /**
132
     * Return array with all parameters of configuration
133
     *
134
     * @return array
135
     */
136 22
    public function getParameters(): array
137
    {
138 22
        return $this->_parameters;
139
    }
140
}
141