Completed
Push — master ( 304183...e1273d )
by Ryuichi
16:29
created

DatabaseManager::enableAutoCommit()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Database;
3
4
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WebStream\Exception\Extend\DatabaseException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\DatabaseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Doctrine\DBAL\Connection;
8
9
/**
10
 * DatabaseManager
11
 * @author Ryuichi TANAKA.
12
 * @since 2013/12/07
13
 * @version 0.4
14
 */
15
class DatabaseManager
16
{
17
    use Injector;
18
19
    /**
20
     * @var ConnectionManager コネクションマネージャ
21
     */
22
    private $connectionManager;
23
24
    /**
25
     * @var DatabaseDriver データベースコネクション
0 ignored issues
show
Bug introduced by
The type WebStream\Database\DatabaseDriver was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     */
27
    private $connection;
28
29
    /**
30
     * @var bool 自動コミットフラグ
31
     */
32
    private $isAutoCommit;
33
34
    /**
35
     * @var Query クエリオブジェクト
36
     */
37
    private $query;
38
39
    /**
40
     * @var Logger ロガー
0 ignored issues
show
Bug introduced by
The type WebStream\Database\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42
    private $logger;
43
44
    /**
45
     * constructor
46
     * @param Container 依存コンテナ
47
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 依存コンテナ at position 0 could not be parsed: Unknown type name '依存コンテナ' at position 0 in 依存コンテナ.
Loading history...
48 33
    public function __construct(Container $container)
49
    {
50 33
        $this->connectionManager = new ConnectionManager($container);
51 33
        $this->logger = $container->logger;
52 33
        $this->isAutoCommit = false;
53
    }
54
55
    /**
56
     * destructor
57
     */
58 33
    public function __destruct()
59
    {
60 33
        $this->disconnect();
61 33
        $this->query = null;
62
    }
63
64
    /**
65
     * データベース接続する
66
     * すでに接続中であれば再接続はしない
67
     */
68 33
    public function connect()
69
    {
70
        try {
71 33
            $this->connection->connect();
72 33
            $this->query = new Query($this->connection);
73 33
            $this->query->inject('logger', $this->logger);
74
        } catch (\PDOException $e) {
75
            throw new DatabaseException($e);
76
        }
77
    }
78
79
    /**
80
     * データベース切断する
81
     */
82 33
    public function disconnect()
83
    {
84 33
        if ($this->connection === null) {
85 33
            return;
86
        }
87
88 33
        if ($this->inTransaction()) {
89
            // トランザクションが明示的に開始された状態でcommit/rollbackが行われていない場合
90
            // ログに警告を書き込み、強制的にロールバックする
91 6
            $this->connection->rollback();
92 6
            $this->logger->warn("Not has been executed commit or rollback after the transaction started.");
93
        }
94
95 33
        $this->connection->disconnect();
96 33
        $this->connection = null;
97
    }
98
99
    /**
100
     * トランザクションを開始する
101
     * @param int $isolationLevel トランザクション分離レベル
102
     */
103 6
    public function beginTransaction(int $isolationLevel)
104
    {
105
        // 既にトランザクションが開始されている場合、継続しているトランザクションを有効のままにする
106
        // トランザクションを破棄して再度開始する場合は明示的に破棄してから再呼び出しする
107 6
        if ($this->inTransaction()) {
108
            $this->logger->debug("Transaction already started.");
109
110
            return;
111
        }
112
113 6
        if (!$this->connection->beginTransaction()) {
114
            throw new DatabaseException("Failed to start transaction.");
115
        }
116
117 6
        $this->connection->setAutoCommit($this->isAutoCommit);
118
119 6
        if ($isolationLevel === Connection::TRANSACTION_READ_UNCOMMITTED ||
120 6
            $isolationLevel === Connection::TRANSACTION_READ_COMMITTED ||
121
            $isolationLevel === Connection::TRANSACTION_REPEATABLE_READ ||
122
            $isolationLevel === Connection::TRANSACTION_SERIALIZABLE) {
123 6
            $this->connection->setTransactionIsolation($isolationLevel);
124
        } else {
125
            throw new DatabaseException("Invalid transaction isolation level: " . $isolationLevel);
126
        }
127
128 6
        $this->logger->debug("Transaction start.");
129
    }
130
131
    /**
132
     * コミットする
133
     */
134 3
    public function commit()
135
    {
136
        try {
137 3
            if ($this->connection !== null) {
138 3
                if ($this->inTransaction()) {
139 3
                    $this->connection->commit();
140 3
                    $this->logger->debug("Execute commit.");
141
                } else {
142
                    $this->logger->warn("Not executed commit because the transaction is not started.");
143
                }
144
            } else {
145
                throw new DatabaseException("Can't execute commit.");
146
            }
147
        } catch (\PDOException $e) {
148
            $this->query = null;
149
            throw new DatabaseException($e);
150
        }
151
    }
152
153
    /**
154
     * ロールバックする
155
     */
156 3
    public function rollback()
157
    {
158
        try {
159 3
            if ($this->connection !== null) {
160 3
                if ($this->inTransaction()) {
161 3
                    $this->connection->rollback();
162 3
                    $this->logger->debug("Execute rollback.");
163
                } else {
164
                    $this->logger->warn("Not executed rollback because the transaction is not started.");
165
                }
166
            } else {
167
                throw new DatabaseException("Can't execute rollback.");
168
            }
169
        } catch (\PDOException $e) {
170
            $this->query = null;
171
            throw new DatabaseException($e);
172
        }
173
    }
174
175
    /**
176
     * 自動コミットを有効化
177
     */
178
    public function enableAutoCommit()
179
    {
180
        $this->isAutoCommit = true;
181
    }
182
183
    /**
184
     * 自動コミットを無効化
185
     */
186 3
    public function disableAutoCommit()
187
    {
188 3
        $this->isAutoCommit = false;
189
    }
190
191
    /**
192
     * ロールバックが発生したかどうか
193
     * @return boolean ロールバックが発生したかどうか
194
     */
195
    public function isRollback()
196
    {
197
        return $this->isRollback;
198
    }
199
200
    /**
201
     * トランザクション内かどうか
202
     * @return boolean トランザクション内かどうか
203
     */
204 33
    public function inTransaction()
205
    {
206 33
        return $this->connection->inTransaction();
207
    }
208
209
    /**
210
     * DB接続されているか
211
     * @param boolean 接続有無
212
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 接続有無 at position 0 could not be parsed: Unknown type name '接続有無' at position 0 in 接続有無.
Loading history...
213
    public function isConnected()
214
    {
215
        return $this->connection->isConnected();
216
    }
217
218
    /**
219
     * トランザクション分離レベルを返却する
220
     * @return int トランザクション分離レベル
221
     */
222
    public function getTransactionIsolation()
223
    {
224
        return $this->connection->getTransactionIsolation();
225
    }
226
227
    /**
228
     * データベース接続が可能かどうか
229
     * @param string Modelファイルパス
230
     * @return boolean 接続可否
231
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment Modelファイルパス at position 0 could not be parsed: Unknown type name 'Modelファイルパス' at position 0 in Modelファイルパス.
Loading history...
232 33
    public function loadConnection($filepath)
233
    {
234 33
        $connection = $this->connectionManager->getConnection($filepath);
235 33
        if ($connection !== null) {
236 33
            $this->connection = $connection;
237
        }
238
239 33
        return $this->connection !== null;
240
    }
241
242
    /**
243
     * クエリを設定する
244
     * @param string SQL
245
     * @param array<string> パラメータ
246
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment パラメータ at position 0 could not be parsed: Unknown type name 'パラメータ' at position 0 in パラメータ.
Loading history...
247 33
    public function query($sql, array $bind = [])
248
    {
249 33
        if ($this->query === null) {
250
            throw new DatabaseException("Query does not set because database connection failed.");
251
        }
252 33
        $this->query->setSql($sql);
253 33
        $this->query->setBind($bind);
254
255 33
        return $this->query;
256
    }
257
}
258