Completed
Push — master ( 1e5401...54e54a )
by Mr
02:19
created

Mysql::getHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace DrMVC\Database\Drivers;
4
5
use DrMVC\Config\ConfigInterface;
6
7
class Mysql extends SQL
8
{
9
    const DEFAULT_HOST = 'localhost';
10
    const DEFAULT_PORT = 3306;
11
12
    public function __construct(ConfigInterface $config, string $collection)
13
    {
14
        parent::__construct($config, $collection);
15
        $db_host = $this->getHost();
16
        $db_port = $this->getPort();
17
        $db_auth = $this->getAuth();
18
        $db_driv = $this->getConfig()->get('driver');
19
        $db_name = $this->getConfig()->get('database');
20
21
        $dsn = "$db_driv://$db_auth$db_host:$db_port/$db_name";
22
23
        $this->setDsn($dsn);
24
    }
25
26
    private function getHost(): string
27
    {
28
        $host = $this->getConfig()->get('host');
29
        return $host ?? self::DEFAULT_HOST;
30
    }
31
32
    private function getPort(): int
33
    {
34
        $port = $this->getConfig()->get('port');
35
        return $port ?? self::DEFAULT_PORT;
36
    }
37
38
    private function getAuth(): string
39
    {
40
        $user = $this->getConfig()->get('username');
41
        $pass = $this->getConfig()->get('password');
42
43
        return ($user && $pass) ? $user . ':' . $pass . '@' : '';
44
    }
45
46
}
47