Passed
Push — master ( d50d64...3e68ba )
by Maike
02:16
created

Config::getCharset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MocOrm\Connection;
4
5
use Mockery\Exception;
0 ignored issues
show
Bug introduced by
The type Mockery\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class Config
8
{
9
    /**
10
     * @connectionString array of string This attribute is internal for save the connection string
11
     */
12
    private $_connectionString = [];
13
14
    /**
15
     * @username array of string This attribute is internal for save the connection usernames
16
     */
17
    private $_username = [];
18
19
    /**
20
     * @password array of string This attribute is internal for save the connection passwords
21
     */
22
    private $_password = [];
23
24
    /**
25
     * List of drivers sets on connections
26
     * @var array $_driver
27
     */
28
    private $_driver = [];
29
30
    /**
31
     * List of drivers sets on connections
32
     * @var $_driver
33
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $_driver at position 0 could not be parsed: Unknown type name '$_driver' at position 0 in $_driver.
Loading history...
34
    private $_charset = [];
35
36
    /**
37
     * List of schemas sets on connections
38
     * @var array $_schema
39
     */
40
    private $_schema = [];
41
42
    /**
43
     * Constant to define set accepted drivers.
44
     * @var array DRIVERS all accepted drivers
45
     */
46
    const DRIVERS = [
47
        "mysql",
48
        "pgsql",
49
    ];
50
51
    private $default = null;
52
53
    /**
54
     * @var boolean $appLogger Configure if application save log or no
55
     */
56
    private $appLogger;
57
58
    /**
59
     * Create configuration from connection
60
     * @param string $driver The driver from connection
61
     * @param string $username The username from connection
62
     * @param string $password The password from connection
63
     * @param string $host The host from connection
64
     * @param string $database The database from connection
65
     * @param string $connectionName The connection name from connection
66
     * @param integer $port The port from connection
67
     * @return $this This object for other iterators
68
     * @throws \Exception case one or some elements on parameters are invalid
69
     */
70 14
    public function addConfig(
71
        $driver = 'mysql',
72
        $username = "root",
73
        $password = null,
74
        $host = "localhost",
75
        $database = null,
76
        $connectionName = null,
77
        $port = null,
78
        $charset = 'utf8',
79
        $defaultSchema = null)
80
    {
81
        #Begin: Verify if all parameters send is valid.
82 14
        if (!is_string($driver) || !in_array($driver, self::DRIVERS)) throw new \Exception("The driver $driver don't supported.");
83 13
        if (!is_string($username) || empty($username)) throw new \Exception("Invalid username.");
84 12
        if (!is_string($password) || empty($password)) throw new \Exception("Invalid password.");
85 11
        if (!is_string($host) || empty($host)) throw new \Exception("Invalid host.");
86 10
        if (!is_string($database) || empty($database)) throw new \Exception("Invalid database name.");
87 9
        $this->validatesConnectionName($connectionName);
88
89 8
        $port = is_null($port) ? '' : (int)$port;
90 8
        if (!is_null($port) && !is_int($port)) throw new \Exception("Invalid port format.");
91
        #Constructor of the connection string
92 7
        $this->_connectionString[$connectionName] = "$driver:host=$host;dbname=$database;port=$port;";
93 7
        $this->_username[$connectionName] = $username;
94 7
        $this->_password[$connectionName] = $password;
95 7
        $this->_driver[$connectionName] = $driver;
96 7
        $this->_charset[$connectionName] = $charset;
97 7
        $this->_schema[$connectionName] = $defaultSchema;
98
99 7
        return $this;
100
    }
101
102
    /**
103
     * Get all connection string
104
     * @return array if have connection but not have this method return null
105
     */
106 3
    final public function getConfigs()
107
    {
108 3
        return $this->_connectionString;
109
    }
110
111
    /**
112
     * Set the current connection on connection name.
113
     * @param string $connectionName name on connection
114
     * @return $this This object from other interator
115
     * @throws \Exception if the connect name haven't set;
116
     */
117 1
    public function setDefault($connectionName)
118
    {
119 1
        $this->default = $connectionName;
120 1
        return $this;
121
    }
122
123
    /**
124
     * Set the current connection on connection name.
125
     * @param string $connectionName name on connection
126
     * @return $this This object from other interator
127
     * @throws \Exception if the connect name haven't set;
128
     */
129 1
    public function getDefault()
130
    {
131 1
        return $this->default;
132
    }
133
134
    /**
135
     * Set the current connection on connection name.
136
     * @param string $connectionName name on connection
137
     * @return $this This object from other interator
138
     * @throws \Exception if the connect name haven't set;
139
     */
140 3
    public function getConnection($connectionName)
141
    {
142 3
        $this->validatesConnectionName($connectionName);
143
144 2
        if (array_key_exists($connectionName, $this->_connectionString)) {
145
            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('connection...chema[$connectionName]) returns the type array which is incompatible with the documented return type MocOrm\Connection\Config.
Loading history...
146 1
                'connectionString' => $this->_connectionString[$connectionName],
147 1
                'driver' => $this->_driver[$connectionName],
148 1
                'username' => $this->_username[$connectionName],
149 1
                'password' => $this->_password[$connectionName],
150 1
                'charset' => $this->_charset[$connectionName],
151 1
                'schema' => $this->_schema[$connectionName]];
152
        } else {
153 1
            throw new \Exception("The connection name $connectionName is not set.");
154
        }
155
156
        return $this;
0 ignored issues
show
Unused Code introduced by
return $this is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
157
    }
158
159 9
    private function validatesConnectionName($connectionName)
160
    {
161 9
        if (!is_string($connectionName) || empty($connectionName)) {
162 2
            throw new \Exception("Invalid connection name.");
163
        }
164 8
    }
165
166
    /**
167
     * @return mixed
168
     */
169
    public function getConnectionString($connectionName)
170
    {
171
        return $this->_connectionString[$connectionName];
172
    }
173
174
    /**
175
     * @param mixed $connectionString
176
     */
177
    public function setSettings($connectionName, $connectionSettings)
178
    {
179
        if (!is_array($connectionSettings)) throw new \Exception('Invalid format connectionSettings');
180
        if (empty($this->_driver[$connectionName])) throw new \Exception('Driver not set.');
181
182
        $this->validatesConnectionName($connectionName);
183
184
        $connectionSettings = (object)$connectionSettings;
185
186
        $this->_connectionString[$connectionName] = $this->_driver[$connectionName].":host=$connectionSettings->host;dbname=$connectionSettings->database;port=$connectionSettings->port;";
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return mixed
193
     */
194
    public function getUsername($connectionName)
195
    {
196
        return $this->_username[$connectionName];
197
    }
198
199
    /**
200
     * @param mixed $username
201
     */
202
    public function setUsername($connectionName, $username = "root")
203
    {
204
        $this->validatesConnectionName($connectionName);
205
206
        $this->_username[$connectionName] = $username;
207
208
        return $this;
209
    }
210
211
    /**
212
     * @return mixed
213
     */
214
    public function getPassword($connectionName)
215
    {
216
        return $this->_password[$connectionName];
217
    }
218
219
    /**
220
     * @param mixed $password
221
     */
222
    public function setPassword($connectionName, $password)
223
    {
224
        $this->validatesConnectionName($connectionName);
225
226
        $this->_password[$connectionName] = $password;
227
228
        return $this;
229
    }
230
231
    /**
232
     * @return mixed
233
     */
234
    public function getDriver($connectionName)
235
    {
236
        return $this->_driver[$connectionName];
237
    }
238
239
    /**
240
     * @param mixed $driver
241
     */
242
    public function setDriver($connectionName, $driver = 'mysql')
243
    {
244
        $this->validatesConnectionName($connectionName);
245
246
        $this->_driver[$connectionName] = $driver;
247
248
        return $this;
249
    }
250
251
    /**
252
     * @return mixed
253
     */
254
    public function getCharset($connectionName)
255
    {
256
        return $this->_charset[$connectionName];
257
    }
258
259
    /**
260
     * @param mixed $charset
261
     */
262
    public function setCharset($connectionName, $charset = 'utf8')
263
    {
264
        $this->validatesConnectionName($connectionName);
265
266
        $this->_charset[$connectionName] = $charset;
267
268
        return $this;
269
    }
270
271
    /**
272
     * @return mixed
273
     */
274
    public function getSchema($connectionName)
275
    {
276
        return $this->_schema[$connectionName];
277
    }
278
279
    /**
280
     * @param mixed $schema
281
     */
282
    public function setSchema($connectionName, $schema)
283
    {
284
        $this->validatesConnectionName($connectionName);
285
286
        $this->_schema[$connectionName] = $schema;
287
288
        return $this;
289
    }
290
291
    /**
292
     * @return $this
293
     */
294
    public function appEnableLogger() {
295
        $this->appLogger = true;
296
297
        return $this;
298
    }
299
300
    /**
301
     * @return $this
302
     */
303
    public function appDisableLogger() {
304
        $this->appLogger = false;
305
306
        return $this;
307
    }
308
309
    /**
310
     * @return boolean
311
     */
312
    public function getAppLogger() {
313
        return $this->appLogger;
314
    }
315
}
316