Completed
Push — master ( 5652bd...9532f5 )
by Pierre
02:48
created

PdoMysql::dsn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Component\Db\Adapter;
4
5
use \PDO;
6
7
class PdoMysql
8
{
9
10
    private $host;
11
    private $dbname;
12
    private $username;
13
    private $password;
14
    private $options;
15
    private $connection;
16
17
    /**
18
     * instanciate
19
     *
20
     * @param string $dbname
21
     * @param array $params
22
     */
23
    public function __construct(string $dbname, array $params)
24
    {
25
        $this->dbname = $dbname;
26
        $this->host = $params['host'];
27
        $this->username = $params['username'];
28
        $this->password = $params['password'];
29
        $this->options = $params['options'];
30
    }
31
32
     /**
33
      * connect to db
34
      *
35
      * @return void
36
      */
37
    public function connect()
38
    {
39
        $this->connection = new PDO(
40
            $this->dsn(),
41
            $this->username,
42
            $this->password,
43
            $this->options
44
        );
45
    }
46
47
    /**
48
     * return PDO instance
49
     *
50
     * @return PDO
51
     */
52
    public function getConnection(): PDO
53
    {
54
        return $this->connection;
55
    }
56
57
    /**
58
     * return dsn
59
     *
60
     * @return string
61
     */
62
    protected function dsn(): string
63
    {
64
        return sprintf(
65
            'mysql:host=%s;dbname=%s',
66
            $this->host,
67
            $this->dbname
68
        );
69
    }
70
}
71