Completed
Push — master ( 20e4ee...bc728c )
by Mr
01:19
created

Config::set()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 3
nc 6
nop 2
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
     */
50
    public function set(string $name, $value): ConfigInterface
51
    {
52
        try {
53
54
            // Check of key in array
55
            $this->keyAllowed($name, self::ALLOWED);
56
57
            // Get type of current variable
58
            $whatType = \gettype($value);
59
            // Get allowed type of parameter
60
            $type = self::ALLOWED[$name];
61
            $isType = 'is_' . $type;
62
63
            // Check what type has this value
64
            if (!$isType($value)) {
65
                throw new Exception("Parameter \"$name\" has wrong type \"$whatType\" but should be \"$type\"");
66
            }
67
68
        } catch (Exception $e) {
69
            // __construct
70
        }
71
72
        // Save value to array
73
        $this->_parameters[$name] = $value;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Return parameter of current config by name
80
     *
81
     * @param   string $parameter
82
     * @return  mixed
83
     */
84
    public function get(string $parameter)
85
    {
86
        try {
87
            // Check of key in array
88
            $this->keyAllowed($parameter, self::ALLOWED);
89
90
        } catch (Exception $e) {
91
            // __construct
92
        }
93
94
        // If client need port number and port is not set
95
        if ($parameter === 'port' && !isset($this->_parameters['port'])) {
96
            // then use default with or without ssl encryption
97
            return (isset($this->_parameters['ssl']) && $this->_parameters['ssl'])
98
                ? Client::PORT_SSL
99
                : Client::PORT;
100
        }
101
102
        return $this->_parameters[$parameter];
103
    }
104
105
    /**
106
     * Return array with all parameters of configuration
107
     *
108
     * @return  array
109
     */
110
    public function getParameters(): array
111
    {
112
        return $this->_parameters;
113
    }
114
}
115