GenericConnectionSettings   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 149
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getClassName() 0 4 1
A getServerType() 0 4 1
A getHash() 0 4 1
A hasParameter() 0 4 2
A getParameter() 0 7 2
A setParameter() 0 8 2
A hasParameters() 0 4 1
A getParameters() 0 7 2
A setParameters() 0 5 1
A getRequiredKeys() 0 4 1
A validate() 0 12 3
1
<?php
2
3
namespace ConfigToken\ConnectionSettings\Types;
4
5
6
use ConfigToken\ConnectionSettings\ConnectionSettingsInterface;
7
use ConfigToken\ConnectionSettings\Exception\ConnectionSettingsException;
8
9
class GenericConnectionSettings implements ConnectionSettingsInterface
10
{
11
    protected $parameters;
12
13
    /**
14
     * @param array|null $parameters
15
     */
16
    public function __construct(array $parameters = null)
17
    {
18
        if (isset($parameters)) {
19
            $this->setParameters($parameters);
20
        }
21
    }
22
23
    /**
24
     * Get the implementation class name.
25
     *
26
     * @return string
27
     */
28
    public static function getClassName()
29
    {
30
        return get_called_class();
31
    }
32
33
    /**
34
     * Get the file server type identifier corresponding to the client's implementation.
35
     *
36
     * @return string|null If fallback class for all server types.
37
     */
38
    public static function getServerType()
39
    {
40
        return null;
41
    }
42
43
    /**
44
     * Get the unique hash of the connection parameters.
45
     *
46
     * @return string
47
     */
48
    public function getHash()
49
    {
50
        return md5(serialize($this->getParameters()));
51
    }
52
53
    /**
54
     * Check if the parameter with the given key was set.
55
     *
56
     * @param string $key The parameter key.
57
     * @return boolean
58
     */
59
    public function hasParameter($key)
60
    {
61
        return isset($this->parameters) && isset($this->parameters[$key]);
62
    }
63
64
    /**
65
     * Get the parameter with the given key.
66
     *
67
     * @param string $key The parameter key.
68
     * @param mixed $default The default value to return if the parameter was not set.
69
     * @return mixed|null
70
     */
71
    public function getParameter($key, $default=null)
72
    {
73
        if (!$this->hasParameter($key)) {
74
            return $default;
75
        }
76
        return $this->parameters[$key];
77
    }
78
79
    /**
80
     * Set the parameter with the given key.
81
     *
82
     * @param string $key The parameter key.
83
     * @param mixed $value The new value.
84
     * @return $this
85
     */
86
    public function setParameter($key, $value)
87
    {
88
        if (!isset($this->parameters)) {
89
            $this->parameters = array();
90
        }
91
        $this->parameters[$key] = $value;
92
        return $this;
93
    }
94
95
    /**
96
     * Check if the parameters array was set.
97
     *
98
     * @return boolean
99
     */
100
    public function hasParameters()
101
    {
102
        return isset($this->parameters);
103
    }
104
105
    /**
106
     * Get the parameters array.
107
     *
108
     * @return array
109
     */
110
    public function getParameters()
111
    {
112
        if (!$this->hasParameters()) {
113
            return array();
114
        }
115
        return $this->parameters;
116
    }
117
118
    /**
119
     * Set the parameters array.
120
     *
121
     * @param array $value The new array of parameters.
122
     * @return $this
123
     */
124
    public function setParameters($value)
125
    {
126
        $this->parameters = $value;
127
        return $this;
128
    }
129
130
    /**
131
     * Get the required keys.
132
     *
133
     * @return array
134
     */
135
    protected static function getRequiredKeys()
136
    {
137
        return array();
138
    }
139
140
    /**
141
     * Validate the connection parameters and throw appropriate exceptions.
142
     *
143
     * @throws ConnectionSettingsException
144
     */
145
    public function validate()
146
    {
147
        $parameters = $this->hasParameters() ? $this->getParameters() : array();
148
        $missingRequiredKeys = array_diff(static::getRequiredKeys(), array_keys($parameters));
149
        if (!empty($missingRequiredKeys)) {
150
            throw new ConnectionSettingsException(
151
                $this,
152
                'Missing the following required key(s): ["%s"]',
153
                implode('", "', $missingRequiredKeys)
154
            );
155
        }
156
    }
157
}