Completed
Pull Request — master (#35)
by Mathieu
02:03
created

RetryConnection::wrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Ez\DbLinker\Driver\Connection;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\DriverManager;
7
use Ez\DbLinker\RetryStrategy;
8
9
class RetryConnection implements Connection, ConnectionWrapper
10
{
11
    private $wrappedConnectionParams;
12
    private $wrappedConnection;
13
    private $wrappedDriver;
14
    private $retryStrategy;
15
    private $transactionLevel = 0;
16
17
    use CallAndRetry;
18
19
    public function __construct(Array $wrappedConnectionParams, RetryStrategy $retryStrategy)
20
    {
21
        $this->wrappedConnectionParams = $wrappedConnectionParams;
22
        $this->retryStrategy = $retryStrategy;
23
    }
24
25
    /**
26
     * Prepares a statement for execution and returns a Statement object.
27
     *
28
     * @param string $prepareString
29
     *
30
     * @return \Doctrine\DBAL\Driver\Statement
31
     */
32
    public function prepare($prepareString) {
33
        return new RetryStatement(
34
            $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args()),
35
            $this,
36
            $this->retryStrategy
37
        );
38
    }
39
40
    /**
41
     * Executes an SQL statement, returning a result set as a Statement object.
42
     *
43
     * @return \Doctrine\DBAL\Driver\Statement
44
     */
45
    public function query() {
46
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
47
    }
48
49
    /**
50
     * Quotes a string for use in a query.
51
     *
52
     * @param string  $input
53
     * @param integer $type
54
     *
55
     * @return string
56
     */
57
    public function quote($input, $type = \PDO::PARAM_STR) {
58
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
59
    }
60
61
    /**
62
     * Executes an SQL statement and return the number of affected rows.
63
     *
64
     * @param string $statement
65
     *
66
     * @return integer
67
     */
68
    public function exec($statement) {
69
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
70
    }
71
72
    /**
73
     * Returns the ID of the last inserted row or sequence value.
74
     *
75
     * @param string|null $name
76
     *
77
     * @return string
78
     */
79
    public function lastInsertId($name = null) {
80
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
81
    }
82
83
    /**
84
     * Initiates a transaction.
85
     *
86
     * @return boolean TRUE on success or FALSE on failure.
87
     */
88
    public function beginTransaction() {
89
        $this->transactionLevel++;
90
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
91
    }
92
93
    /**
94
     * Commits a transaction.
95
     *
96
     * @return boolean TRUE on success or FALSE on failure.
97
     */
98
    public function commit() {
99
        $this->transactionLevel--;
100
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
101
    }
102
103
    /**
104
     * Rolls back the current transaction, as initiated by beginTransaction().
105
     *
106
     * @return boolean TRUE on success or FALSE on failure.
107
     */
108
    public function rollBack() {
109
        $this->transactionLevel--;
110
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
111
    }
112
113
    /**
114
     * Returns the error code associated with the last operation on the database handle.
115
     *
116
     * @return string|null The error code, or null if no operation has been run on the database handle.
117
     */
118
    public function errorCode() {
119
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
120
    }
121
122
    /**
123
     * Returns extended error information associated with the last operation on the database handle.
124
     *
125
     * @return array
126
     */
127
    public function errorInfo() {
128
        return $this->callWrappedConnectionAndRetry(__FUNCTION__, func_get_args());
129
    }
130
131
    public function close()
132
    {
133
        $this->wrappedConnection = null;
134
    }
135
136
    public function transactionLevel()
137
    {
138
        return $this->transactionLevel;
139
    }
140
141
    private function callWrappedConnectionAndRetry($method, array $arguments)
142
    {
143
        return $this->callAndRetry(function () use ($method, $arguments) {
144
            return call_user_func_array([$this->wrappedConnection(), $method], $arguments);
145
        }, $this->retryStrategy, $this);
146
    }
147
148
    /**
149
     * @inherit
150
     */
151
    public function wrappedConnection()
152
    {
153
        if ($this->wrappedConnection === null) {
154
            $this->wrap();
155
        }
156
        return $this->wrappedConnection;
157
    }
158
159
    /**
160
     * @inherit
161
     */
162
    public function wrappedDriver()
163
    {
164
        if ($this->wrappedDriver === null) {
165
            $this->wrap();
166
        }
167
        return $this->wrappedDriver;
168
    }
169
170
    private function wrap()
171
    {
172
        $connection = $this->wrappedConnection = DriverManager::getConnection(
173
            $this->wrappedConnectionParams
174
        );
175
        $this->wrappedConnection = $connection->getWrappedConnection();
176
        $this->wrappedDriver = $connection->getDriver();
177
    }
178
}
179