Passed
Pull Request — master (#11)
by Łukasz
02:41
created

Connection::useSqlite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Database;
4
5
use PDO;
6
7
/**
8
 * @author Luke Adamczewski
9
 * @package Tworzenieweb\SqlProvisioner\Database
10
 */
11
class Connection
12
{
13
    /** @var string */
14
    private $host;
15
16
    /** @var string */
17
    private $port;
18
19
    /** @var string */
20
    private $user;
21
22
    /** @var string */
23
    private $password;
24
25
    /** @var string */
26
    private $databaseName;
27
28
    /** @var PDO */
29
    private $currentConnection;
30
31
    /** @var string */
32
    private $provisioningTable;
33
34
    /** @var string */
35
    private $criteriaColumn;
36
37
    private $dsn;
38
39
40
    /**
41
     * @param string $host
42
     * @return $this
43
     */
44
    public function setHost($host)
45
    {
46
        $this->host = $host;
47
48
        return $this;
49
    }
50
51
52
53
    /**
54
     * @param string $port
55
     * @return $this
56
     */
57
    public function setPort($port)
58
    {
59
        $this->port = $port;
60
61
        return $this;
62
    }
63
64
65
66
    /**
67
     * @param string $user
68
     * @return $this
69
     */
70
    public function setUser($user)
71
    {
72
        $this->user = $user;
73
74
        return $this;
75
    }
76
77
78
79
    /**
80
     * @param string $password
81
     * @return $this
82
     */
83
    public function setPassword($password)
84
    {
85
        $this->password = $password;
86
87
        return $this;
88
    }
89
90
91
92
    /**
93
     * @param string $databaseName
94
     * @return $this
95
     */
96
    public function setDatabaseName($databaseName)
97
    {
98
        $this->databaseName = $databaseName;
99
100
        return $this;
101
    }
102
103 1
    public function useSqlite($useMemoryStorage = false)
104
    {
105 1
        $this->dsn = $useMemoryStorage ? 'sqlite::memory:' : 'sqlite:%s';
106 1
    }
107
108
    public function useMysql()
109
    {
110
        $this->dsn = 'mysql:host=%s;port=%d;dbname=%s';
111
    }
112
113
    /**
114
     * @return PDO
115
     */
116 1
    public function getCurrentConnection()
117
    {
118 1
        if (null === $this->currentConnection) {
119 1
            $this->currentConnection = new PDO(
120 1
                sprintf($this->dsn, $this->host, $this->port, $this->databaseName),
121 1
                $this->user,
122 1
                $this->password
123 1
            );
124 1
            $this->currentConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
125 1
            $this->currentConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
126 1
            $this->currentConnection->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, true);
127 1
        }
128
129 1
        return $this->currentConnection;
130
    }
131
132
133
134
    /**
135
     * @param string $provisioningTable
136
     * @return $this
137
     */
138 1
    public function setProvisioningTable($provisioningTable)
139
    {
140 1
        $this->provisioningTable = $provisioningTable;
141
142 1
        return $this;
143
    }
144
145
146
147
    /**
148
     * @param string $criteriaColumn
149
     * @return $this
150
     */
151 1
    public function setCriteriaColumn($criteriaColumn)
152
    {
153 1
        $this->criteriaColumn = $criteriaColumn;
154
155 1
        return $this;
156
    }
157
158
159
160
    /**
161
     * @return string
162
     */
163
    public function getProvisioningTable()
164
    {
165
        return $this->provisioningTable;
166
    }
167
168
169
170
    /**
171
     * @return string
172
     */
173
    public function getCriteriaColumn()
174
    {
175
        return $this->criteriaColumn;
176
    }
177
}
178