Completed
Push — master ( d0f8f2...ef5419 )
by Pierre
07:45 queued 05:26
created

PdoMysql::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
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 4
    public function __construct(string $dbname, array $params)
24
    {
25 4
        $this->dbname = $dbname;
26 4
        $this->host = $params['host'];
27 4
        $this->username = $params['username'];
28 4
        $this->password = $params['password'];
29 4
        $this->options = $params['options'];
30
    }
31
32
    /**
33
     * connect to db
34
     *
35
     * @return PdoMysql
36
     */
37 2
    public function connect(): PdoMysql
38
    {
39 2
        $this->connection = new PDO(
40 2
            $this->dsn(),
41 2
            $this->username,
42 2
            $this->password,
43 2
            $this->options
44
        );
45 2
        return $this;
46
    }
47
48
    /**
49
     * return PDO instance
50
     *
51
     * @return PDO
52
     */
53 1
    public function getConnection(): PDO
54
    {
55 1
        return $this->connection;
56
    }
57
58
    /**
59
     * return dsn
60
     *
61
     * @return string
62
     */
63 1
    protected function dsn(): string
64
    {
65 1
        return sprintf(
66 1
            'mysql:host=%s;dbname=%s',
67 1
            $this->host,
68 1
            $this->dbname
69
        );
70
    }
71
}
72