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

PdoMysql   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 61
ccs 0
cts 19
cp 0
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getConnection() 0 3 1
A dsn() 0 6 1
A connect() 0 7 1
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