Passed
Push — master ( e1273d...5be80c )
by Ryuichi
02:33
created

DatabaseManager::transactional()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 5
nop 1
dl 0
loc 14
ccs 0
cts 12
cp 0
crap 12
rs 9.4285
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 (\Exception $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 (\Exception $e) {
170
            $this->query = null;
171
            throw new DatabaseException($e);
172
        }
173
    }
174
175
    /**
176
     * トランザクションスコープを使用する
177
     * @param  Closure $closure クロージャ
0 ignored issues
show
Bug introduced by
The type WebStream\Database\Closure was not found. Did you mean Closure? If so, make sure to prefix the type with \.
Loading history...
178
     * @return object 処理結果
179
     */
180
    public function transactional(\Closure $closure)
181
    {
182
        $this->beginTransaction();
0 ignored issues
show
Bug introduced by
The call to WebStream\Database\Datab...ger::beginTransaction() has too few arguments starting with isolationLevel. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
        $this->/** @scrutinizer ignore-call */ 
183
               beginTransaction();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
183
        $this->disableAutoCommit();
184
        try {
185
            $result = $closure($this);
186
            $this->commit();
187
            return $result;
188
        } catch (DatabaseException $e) {
189
            $this->rollback();
190
            throw $e;
191
        } catch (\Throwable $e) {
192
            $this->rollback();
193
            throw new DatabaseException($e);
194
        }
195
    }
196
197
    /**
198
     * 自動コミットを有効化
199
     */
200 6
    public function enableAutoCommit()
201
    {
202 6
        $this->isAutoCommit = true;
203
    }
204
205
    /**
206
     * 自動コミットを無効化
207
     */
208 3
    public function disableAutoCommit()
209
    {
210 3
        $this->isAutoCommit = false;
211
    }
212
213
    /**
214
     * ロールバックが発生したかどうか
215
     * @return boolean ロールバックが発生したかどうか
216
     */
217
    public function isRollback()
218
    {
219
        return $this->isRollback;
220
    }
221
222
    /**
223
     * トランザクション内かどうか
224
     * @return boolean トランザクション内かどうか
225
     */
226 33
    public function inTransaction()
227
    {
228 33
        return $this->connection->inTransaction();
229
    }
230
231
    /**
232
     * DB接続されているか
233
     * @param boolean 接続有無
234
     */
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...
235
    public function isConnected()
236
    {
237
        return $this->connection->isConnected();
238
    }
239
240
    /**
241
     * トランザクション分離レベルを返却する
242
     * @return int トランザクション分離レベル
243
     */
244
    public function getTransactionIsolation()
245
    {
246
        return $this->connection->getTransactionIsolation();
247
    }
248
249
    /**
250
     * データベース接続が可能かどうか
251
     * @param string Modelファイルパス
252
     * @return boolean 接続可否
253
     */
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...
254 33
    public function loadConnection($filepath)
255
    {
256 33
        $connection = $this->connectionManager->getConnection($filepath);
257 33
        if ($connection !== null) {
258 33
            $this->connection = $connection;
259
        }
260
261 33
        return $this->connection !== null;
262
    }
263
264
    /**
265
     * クエリを設定する
266
     * @param string SQL
267
     * @param array<string> パラメータ
268
     */
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...
269 33
    public function query($sql, array $bind = [])
270
    {
271 33
        if ($this->query === null) {
272
            throw new DatabaseException("Query does not set because database connection failed.");
273
        }
274 33
        $this->query->setSql($sql);
275 33
        $this->query->setBind($bind);
276
277 33
        return $this->query;
278
    }
279
}
280