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.
Passed
Push — master ( 4ef7a4...19be87 )
by Charlotte
01:54
created

Transaction::createSavepoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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_UNCOMMITTED:
41
            case \Plasma\TransactionInterface::ISOLATION_COMMITTED:
42
            case \Plasma\TransactionInterface::ISOLATION_REPEATABLE:
43
            case \Plasma\TransactionInterface::ISOLATION_SERIALIZABLE:
44
                // Valid isolation level
45 24
            break;
46
            default:
47 1
                throw new \Plasma\Exception('Invalid isolation level given');
48
            break;
49
        }
50
        
51 24
        $this->client = $client;
52 24
        $this->driver = $driver;
53 24
        $this->isolation = $isolation;
54 24
    }
55
    
56
    /**
57
     * Destructor. Implicit rollback and automatically checks the connection back into the client on deallocation.
58
     */
59 24
    function __destruct() {
60 24
        if($this->driver !== null && $this->driver->getConnectionState() === \Plasma\DriverInterface::CONNECTION_OK) {
61
            $this->rollback()->then(null, function () {
62 1
                if($this->driver !== null) {
63
                    // Error during implicit rollback, close the session
64 1
                    $this->driver->close();
65
                }
66 2
            });
67
        }
68 24
    }
69
    
70
    /**
71
     * Get the isolation level for this transaction.
72
     * @return int
73
     */
74 1
    function getIsolationLevel(): int {
75 1
        return $this->isolation;
76
    }
77
    
78
    /**
79
     * Whether the transaction is still active, or has been committed/rolled back.
80
     * @return bool
81
     */
82 2
    function isActive(): bool {
83 2
        return ($this->driver !== null);
84
    }
85
    
86
    /**
87
     * Executes a plain query. Resolves with a `QueryResult` instance.
88
     * @param string  $query
89
     * @return \React\Promise\PromiseInterface
90
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
91
     * @see \Plasma\QueryResultInterface
92
     */
93 14
    function query(string $query): \React\Promise\PromiseInterface {
94 14
        if($this->driver === null) {
95 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
96
        }
97
        
98 14
        return $this->driver->query($this->client, $query);
99
    }
100
    
101
    /**
102
     * Prepares a query. Resolves with a `StatementInterface` instance.
103
     * @param string  $query
104
     * @return \React\Promise\PromiseInterface
105
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
106
     * @see \Plasma\StatementInterface
107
     */
108 2
    function prepare(string $query): \React\Promise\PromiseInterface {
109 2
        if($this->driver === null) {
110 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
111
        }
112
        
113 1
        return $this->driver->prepare($this->client, $query);
114
    }
115
    
116
    /**
117
     * Prepares and executes a query. Resolves with a `QueryResultInterface` instance.
118
     * This is equivalent to prepare -> execute -> close.
119
     * If you need to execute a query multiple times, prepare the query manually for performance reasons.
120
     * @param string  $query
121
     * @param array   $params
122
     * @return \React\Promise\PromiseInterface
123
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
124
     * @throws \Plasma\Exception
125
     * @see \Plasma\StatementInterface
126
     */
127 2
    function execute(string $query, array $params = array()): \React\Promise\PromiseInterface {
128 2
        if($this->driver === null) {
129 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
130
        }
131
        
132 1
        return $this->driver->execute($this->client, $query, $params);
133
    }
134
    
135
    /**
136
     * Quotes the string for use in the query.
137
     * @param string  $str
138
     * @param int     $type  For types, see the driver interface constants.
139
     * @return string
140
     * @throws \LogicException               Thrown if the driver does not support quoting.
141
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
142
     */
143 5
    function quote(string $str, int $type = \Plasma\DriverInterface::QUOTE_TYPE_VALUE): string {
144 5
        if($this->driver === null) {
145 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
146
        }
147
        
148 4
        return $this->driver->quote($str, $type);
149
    }
150
    
151
    /**
152
     * Runs the given querybuilder on the underlying driver instance.
153
     * The driver CAN throw an exception if the given querybuilder is not supported.
154
     * An example would be a SQL querybuilder and a Cassandra driver.
155
     * @param \Plasma\QuerybuilderInterface  $query
156
     * @return \React\Promise\PromiseInterface
157
     * @throws \Plasma\Exception
158
     */
159 2
    function runQuery(\Plasma\QuerybuilderInterface $query): \React\Promise\PromiseInterface {
160 2
        if($this->driver === null) {
161 1
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
162
        }
163
        
164 1
        return $this->driver->runQuery($this->client, $query);
165
    }
166
    
167
    /**
168
     * Commits the changes.
169
     * @return \React\Promise\PromiseInterface
170
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
171
     */
172 6
    function commit(): \React\Promise\PromiseInterface {
173
        return $this->query('COMMIT')->then(function () {
174 6
            $this->driver->endTransaction();
175 6
            $this->client->checkinConnection($this->driver);
176 6
            $this->driver = null;
177 6
        });
178
    }
179
    
180
    /**
181
     * Rolls back the changes.
182
     * @return \React\Promise\PromiseInterface
183
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
184
     */
185 4
    function rollback(): \React\Promise\PromiseInterface {
186
        return $this->query('ROLLBACK')->then(function () {
187 3
            $this->driver->endTransaction();
188 3
            $this->client->checkinConnection($this->driver);
189 3
            $this->driver = null;
190 4
        });
191
    }
192
    
193
    /**
194
     * Creates a savepoint with the given identifier.
195
     * @param string  $identifier
196
     * @return \React\Promise\PromiseInterface
197
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
198
     */
199 1
    function createSavepoint(string $identifier): \React\Promise\PromiseInterface {
200 1
        return $this->query('SAVEPOINT '.$this->quote($identifier));
201
    }
202
    
203
    /**
204
     * Rolls back to the savepoint with the given identifier.
205
     * @param string  $identifier
206
     * @return \React\Promise\PromiseInterface
207
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
208
     */
209 1
    function rollbackTo(string $identifier): \React\Promise\PromiseInterface {
210 1
        return $this->query('ROLLBACK TO '.$this->quote($identifier));
211
    }
212
    
213
    /**
214
     * Releases the savepoint with the given identifier.
215
     * @param string  $identifier
216
     * @return \React\Promise\PromiseInterface
217
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
218
     */
219 1
    function releaseSavepoint(string $identifier): \React\Promise\PromiseInterface {
220 1
        return $this->query('RELEASE SAVEPOINT '.$this->quote($identifier));
221
    }
222
}
223