GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Transaction   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 207
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0
wmc 27

14 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 6 2
A releaseSavepoint() 0 2 1
A rollback() 0 5 1
A quote() 0 6 2
A query() 0 6 2
A __construct() 0 17 6
A runQuery() 0 6 2
A getIsolationLevel() 0 2 1
A createSavepoint() 0 2 1
A prepare() 0 6 2
A __destruct() 0 6 4
A isActive() 0 2 1
A commit() 0 5 1
A rollbackTo() 0 2 1
1
<?php
2
/**
3
 * Plasma Core component
4
 * Copyright 2018-2019 PlasmaPHP, All Rights Reserved
5
 *
6
 * Website: https://github.com/PlasmaPHP
7
 * License: https://github.com/PlasmaPHP/core/blob/master/LICENSE
8
*/
9
10
namespace Plasma;
11
12
/**
13
 * Represents a transaction.
14
 */
15
class Transaction implements TransactionInterface {
16
    /**
17
     * @var \Plasma\ClientInterface
18
     */
19
    protected $client;
20
    
21
    /**
22
     * @var \Plasma\DriverInterface|null
23
     */
24
    protected $driver;
25
    
26
    /**
27
     * @var int
28
     */
29
    protected $isolation;
30
    
31
    /**
32
     * Creates a client with the specified factory and options.
33
     * @param \Plasma\ClientInterface  $client
34
     * @param \Plasma\DriverInterface  $driver
35
     * @param int                      $isolation
36
     * @throws \Plasma\Exception  Thrown if the transaction isolation level is invalid.
37
     */
38 25
    function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, int $isolation) {
39 25
        switch($isolation) {
40
            case \Plasma\TransactionInterface::ISOLATION_NO_CHANGE:
41
            case \Plasma\TransactionInterface::ISOLATION_UNCOMMITTED:
42
            case \Plasma\TransactionInterface::ISOLATION_COMMITTED:
43
            case \Plasma\TransactionInterface::ISOLATION_REPEATABLE:
44
            case \Plasma\TransactionInterface::ISOLATION_SERIALIZABLE:
45
                // Valid isolation level
46 24
            break;
47
            default:
48 1
                throw new \Plasma\Exception('Invalid isolation level given');
49
            break;
50
        }
51
        
52 24
        $this->client = $client;
53 24
        $this->driver = $driver;
54 24
        $this->isolation = $isolation;
55 24
    }
56
    
57
    /**
58
     * Destructor. Implicit rollback and automatically checks the connection back into the client on deallocation.
59
     */
60 24
    function __destruct() {
61 24
        if($this->driver !== null && $this->driver->getConnectionState() === \Plasma\DriverInterface::CONNECTION_OK) {
62
            $this->rollback()->then(null, function () {
63 1
                if($this->driver !== null) {
64
                    // Error during implicit rollback, close the session
65 1
                    $this->driver->close();
66
                }
67 2
            });
68
        }
69 24
    }
70
    
71
    /**
72
     * Get the isolation level for this transaction.
73
     * @return int
74
     */
75 1
    function getIsolationLevel(): int {
76 1
        return $this->isolation;
77
    }
78
    
79
    /**
80
     * Whether the transaction is still active, or has been committed/rolled back.
81
     * @return bool
82
     */
83 2
    function isActive(): bool {
84 2
        return ($this->driver !== null);
85
    }
86
    
87
    /**
88
     * Executes a plain query. Resolves with a `QueryResult` instance.
89
     * @param string  $query
90
     * @return \React\Promise\PromiseInterface
91
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
92
     * @see \Plasma\QueryResultInterface
93
     */
94 14
    function query(string $query): \React\Promise\PromiseInterface {
95 14
        if($this->driver === null) {
96 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
97
        }
98
        
99 14
        return $this->driver->query($this->client, $query);
100
    }
101
    
102
    /**
103
     * Prepares a query. Resolves with a `StatementInterface` instance.
104
     * @param string  $query
105
     * @return \React\Promise\PromiseInterface
106
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
107
     * @see \Plasma\StatementInterface
108
     */
109 2
    function prepare(string $query): \React\Promise\PromiseInterface {
110 2
        if($this->driver === null) {
111 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
112
        }
113
        
114 1
        return $this->driver->prepare($this->client, $query);
115
    }
116
    
117
    /**
118
     * Prepares and executes a query. Resolves with a `QueryResultInterface` instance.
119
     * This is equivalent to prepare -> execute -> close.
120
     * If you need to execute a query multiple times, prepare the query manually for performance reasons.
121
     * @param string  $query
122
     * @param array   $params
123
     * @return \React\Promise\PromiseInterface
124
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
125
     * @throws \Plasma\Exception
126
     * @see \Plasma\StatementInterface
127
     */
128 2
    function execute(string $query, array $params = array()): \React\Promise\PromiseInterface {
129 2
        if($this->driver === null) {
130 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
131
        }
132
        
133 1
        return $this->driver->execute($this->client, $query, $params);
134
    }
135
    
136
    /**
137
     * Quotes the string for use in the query.
138
     * @param string  $str
139
     * @param int     $type  For types, see the driver interface constants.
140
     * @return string
141
     * @throws \LogicException               Thrown if the driver does not support quoting.
142
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
143
     */
144 5
    function quote(string $str, int $type = \Plasma\DriverInterface::QUOTE_TYPE_VALUE): string {
145 5
        if($this->driver === null) {
146 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
147
        }
148
        
149 4
        return $this->driver->quote($str, $type);
150
    }
151
    
152
    /**
153
     * Runs the given querybuilder on the underlying driver instance.
154
     * The driver CAN throw an exception if the given querybuilder is not supported.
155
     * An example would be a SQL querybuilder and a Cassandra driver.
156
     * @param \Plasma\QueryBuilderInterface  $query
157
     * @return \React\Promise\PromiseInterface
158
     * @throws \Plasma\Exception
159
     */
160 2
    function runQuery(\Plasma\QueryBuilderInterface $query): \React\Promise\PromiseInterface {
161 2
        if($this->driver === null) {
162 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
163
        }
164
        
165 1
        return $this->driver->runQuery($this->client, $query);
166
    }
167
    
168
    /**
169
     * Commits the changes.
170
     * @return \React\Promise\PromiseInterface
171
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
172
     */
173 6
    function commit(): \React\Promise\PromiseInterface {
174
        return $this->query('COMMIT')->then(function () {
175 6
            $this->driver->endTransaction();
176 6
            $this->client->checkinConnection($this->driver);
177 6
            $this->driver = null;
178 6
        });
179
    }
180
    
181
    /**
182
     * Rolls back the changes.
183
     * @return \React\Promise\PromiseInterface
184
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
185
     */
186 4
    function rollback(): \React\Promise\PromiseInterface {
187
        return $this->query('ROLLBACK')->then(function () {
188 3
            $this->driver->endTransaction();
189 3
            $this->client->checkinConnection($this->driver);
190 3
            $this->driver = null;
191 4
        });
192
    }
193
    
194
    /**
195
     * Creates a savepoint with the given identifier.
196
     * @param string  $identifier
197
     * @return \React\Promise\PromiseInterface
198
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
199
     */
200 1
    function createSavepoint(string $identifier): \React\Promise\PromiseInterface {
201 1
        return $this->query('SAVEPOINT '.$this->quote($identifier));
202
    }
203
    
204
    /**
205
     * Rolls back to the savepoint with the given identifier.
206
     * @param string  $identifier
207
     * @return \React\Promise\PromiseInterface
208
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
209
     */
210 1
    function rollbackTo(string $identifier): \React\Promise\PromiseInterface {
211 1
        return $this->query('ROLLBACK TO '.$this->quote($identifier));
212
    }
213
    
214
    /**
215
     * Releases the savepoint with the given identifier.
216
     * @param string  $identifier
217
     * @return \React\Promise\PromiseInterface
218
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
219
     */
220 1
    function releaseSavepoint(string $identifier): \React\Promise\PromiseInterface {
221 1
        return $this->query('RELEASE SAVEPOINT '.$this->quote($identifier));
222
    }
223
}
224