Passed
Push — master ( af7355...fa199b )
by Phillip
01:28
created

LazyPDO   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 88
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getConnectionType() 0 3 1
A getPassword() 0 3 1
A __call() 0 3 1
A getDatabase() 0 3 1
A pdo() 0 7 2
A getUsername() 0 3 1
1
<?php
2
3
4
namespace Deployee\Components\Persistence;
5
6
/**
7
 * @mixin \PDO
8
 */
9
class LazyPDO
10
{
11
    /**
12
     * @var string
13
     */
14
    private $dsn;
15
16
    /**
17
     * @var string
18
     */
19
    private $username;
20
21
    /**
22
     * @var string
23
     */
24
    private $password;
25
26
    /**
27
     * @var array
28
     */
29
    private $options;
30
31
    /**
32
     * @var \PDO
33
     */
34
    private $pdo;
35
36
    /**
37
     * @param string $dsn
38
     * @param string $username
39
     * @param string $password
40
     * @param array $options
41
     */
42
    public function __construct(string $dsn, string $username = '', string $password = '', array $options = [])
43
    {
44
        $this->dsn = $dsn;
45
        $this->username = $username;
46
        $this->password = $password;
47
        $this->options = $options;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getConnectionType(): string
54
    {
55
        return substr($this->dsn, 0, strpos($this->dsn, ':'));
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getDatabase(): string
62
    {
63
        return substr($this->dsn, strrpos($this->dsn, '='));
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getUsername(): string
70
    {
71
        return $this->username;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getPassword(): string
78
    {
79
        return $this->password;
80
    }
81
82
    public function __call($name, $arguments)
83
    {
84
        return call_user_func_array([$this->pdo(), $name], $arguments);
85
    }
86
87
    /**
88
     * @return \PDO
89
     */
90
    private function pdo(): \PDO
91
    {
92
        if($this->pdo === null){
93
            $this->pdo = new \PDO($this->dsn, $this->username, $this->password, $this->options);
94
        }
95
96
        return $this->pdo;
97
    }
98
}