Completed
Push — master ( bf97fc...c83b9c )
by Pierre
03:33
created

PdoPgsql::getConnection()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Component\Db\Adapter;
4
5
use \PDO;
6
7
class PdoPgsql
8
{
9
10
    /**
11
     * hostname ip
12
     *
13
     * @var String
14
     */
15
    private $host;
16
17
    /**
18
     * port number
19
     *
20
     * @var integer
21
     */
22
    private $port;
23
24
    /**
25
     * database name
26
     *
27
     * @var String
28
     */
29
    private $dbname;
30
31
    /**
32
     * login name
33
     *
34
     * @var String
35
     */
36
    private $username;
37
38
    /**
39
     * password
40
     *
41
     * @var String
42
     */
43
44
    private $password;
45
46
    /**
47
     * options
48
     *
49
     * @var array
50
     */
51
    private $options;
52
53
    /**
54
     * connexion
55
     *
56
     * @var PDO
57
     */
58
    private $connection;
59
60
    /**
61
     * instanciate
62
     *
63
     * @param string $dbname
64
     * @param array $params
65
     */
66
    public function __construct(string $dbname, array $params)
67
    {
68
        $this->dbname = $dbname;
69
        $this->host = $params['host'];
70
        $this->port = $params['port'];
71
        $this->username = $params['username'];
72
        $this->password = $params['password'];
73
        $this->options = $params['options'];
74
    }
75
76
    /**
77
     * connect to db
78
     *
79
     * @return PdoPgsql
80
     */
81
    public function connect(): PdoPgsql
82
    {
83
        $this->connection = new PDO(
84
            $this->dsn(),
85
            $this->username,
86
            $this->password,
87
            $this->options
88
        );
89
        return $this;
90
    }
91
92
    /**
93
     * return PDO instance
94
     *
95
     * @return PDO
96
     */
97
    public function getConnection(): PDO
98
    {
99
        return $this->connection;
100
    }
101
102
    /**
103
     * dsn
104
     *
105
     * @return string
106
     */
107
    protected function dsn(): string
108
    {
109
        return sprintf(
110
            'pgsql:host=%s;dbname=%s;port=%s;user=%s;password=%s',
111
            $this->host,
112
            $this->dbname,
113
            $this->port,
114
            $this->username,
115
            $this->password
116
        );
117
    }
118
}
119