ConnectorMySql   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 69.23%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 37
ccs 9
cts 13
cp 0.6923
rs 10
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDsn() 0 19 4
A connect() 0 4 1
1
<?php
2
namespace JayaCode\Framework\Core\Database\Connector;
3
4
/**
5
 * Class ConnectorMySql
6
 * @package JayaCode\Framework\Core\Database\Connector
7
 */
8
class ConnectorMySql extends Connector
9
{
10
11
    /**
12
     * @param $config
13
     * @return string
14
     * @throws \Exception
15
     */
16 3
    public function getDsn($config)
17
    {
18 3
        $host = arr_get($config, "host");
19 3
        $port = arr_get($config, "port");
20 3
        $dbname = arr_get($config, "dbname");
21 3
        $charset = arr_get($config, "charset", "utf8");
22
23 3
        if (empty($host)) {
24
            throw new \Exception("\"host\" can not be blank.");
25
        }
26
27 3
        if (empty($dbname)) {
28
            throw new \Exception("\"dbname\" can not be blank.");
29
        }
30
31
        return $port ?
32 3
            sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s', $host, $port, $dbname, $charset):
33 3
            sprintf('mysql:host=%s;dbname=%s;charset=%s', $host, $dbname, $charset);
34
    }
35
36
    /**
37
     * @param $config
38
     * @return \PDO
39
     */
40
    public function connect($config)
41
    {
42
        return $this->createConnection($this->getDsn($config), $config);
43
    }
44
}
45