Completed
Push — feature/0.7.0 ( b44828...201ef6 )
by Ryuichi
03:01
created

DatabaseDriver::setTransactionIsolation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
namespace WebStream\Database\Driver;
3
4
use WebStream\DI\Injector;
5
use WebStream\Module\Container;
6
use Doctrine\DBAL\Connection;
7
8
/**
9
 * DatabaseDriver
10
 * @author Ryuichi TANAKA.
11
 * @since 2013/12/07
12
 * @version 0.4
13
 */
14
abstract class DatabaseDriver
15
{
16
    use Injector;
17
18
    /**
19
     * @var object DBオブジェクト
20
     */
21
    protected $connection;
22
23
    /**
24
     * @var Container DB接続設定
25
     */
26
    protected $config;
27
28
    /**
29
     * constructor
30
     */
31
    public function __construct(Container $config)
32
    {
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * destructor
38
     */
39
    public function __destruct()
40
    {
41
        // $this->logger->debug("Release driver: " . get_class($this));
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
    }
43
44
    /**
45
     * 接続する
46
     */
47
    abstract public function connect();
48
49
    /**
50
     * 切断する
51
     */
52
    public function disconnect()
53
    {
54
        if ($this->connection !== null) {
55
            $this->logger->debug("Database disconnect.");
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Database\Driver\DatabaseDriver>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
            $this->connection->close();
57
            $this->connection = null;
58
        }
59
    }
60
61
    /**
62
     * トランザクションを開始する
63
     * @return boolean トランザクション開始結果
64
     */
65
    public function beginTransaction()
66
    {
67
        $inTransaction = false;
68
        if ($this->connection !== null) {
69
            $this->connection->beginTransaction();
70
            $inTransaction = $this->inTransaction();
71
        }
72
73
        return $inTransaction;
74
    }
75
76
    /**
77
     * コミットする
78
     */
79
    public function commit()
80
    {
81
        if ($this->connection !== null) {
82
            $this->connection->commit();
83
        }
84
    }
85
86
    /**
87
     * ロールバックする
88
     */
89
    public function rollback()
90
    {
91
        if ($this->inTransaction()) {
92
            $this->connection->rollback();
93
        }
94
    }
95
96
    /**
97
     * DB接続されているか
98
     * @param boolean 接続有無
99
     */
100
    public function isConnected()
101
    {
102
        return $this->connection !== null;
103
    }
104
105
    /**
106
     * トランザクション内かどうか
107
     * @return boolean トランザクション内かどうか
108
     */
109
    public function inTransaction()
110
    {
111
        return $this->connection !== null ? $this->connection->isTransactionActive() : false;
112
    }
113
114
    /**
115
     * SQLをセットしてステートメントを返却する
116
     * @param string SQL
117
     * @return object ステートメント
118
     */
119
    public function getStatement($sql)
120
    {
121
        return $this->connection !== null ? $this->connection->prepare($sql) : null;
122
    }
123
124
    /**
125
     * トランザクション分離レベルを返却する
126
     * @return int トランザクション分離レベル
127
     */
128
    public function getTransactionIsolation()
129
    {
130
        return $this->connection->getTransactionIsolation();
131
    }
132
133
    /**
134
     * トランザクション分離レベルを設定する
135
     * @param int $isolationLevel トランザクション分離レベル
136
     */
137
    public function setTransactionIsolation(int $isolationLevel)
138
    {
139
        $this->connection->setTransactionIsolation($isolationLevel);
140
    }
141
}
142