PdoMysql   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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