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 ( 63ab33...546fe5 )
by Charlotte
02:42
created

Transaction::rollback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plasma Core component
4
 * Copyright 2018 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 15
    function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, int $isolation) {
39 15
        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 14
            break;
46
            default:
47 1
                throw new \Plasma\Exception('Invalid isolation level given');
48
            break;
49
        }
50
        
51 14
        $this->client = $client;
52 14
        $this->driver = $driver;
53 14
        $this->isolation = $isolation;
54 14
    }
55
    
56
    /**
57
     * Destructor. Implicit rollback and automatically checks the connection back into the client on deallocation.
58
     */
59 14
    function __destruct() {
60 14
        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 14
    }
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 8
    function query(string $query): \React\Promise\PromiseInterface {
94 8
        if($this->driver === null) {
95
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
96
        }
97
        
98 8
        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
    function prepare(string $query): \React\Promise\PromiseInterface {
109
        if($this->driver === null) {
110
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
111
        }
112
        
113
        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
    function execute(string $query, array $params = array()): \React\Promise\PromiseInterface {
128
        if($this->driver === null) {
129
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
130
        }
131
        
132
        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
     * @return string
139
     * @throws \LogicException               Thrown if the driver does not support quoting.
140
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
141
     */
142
    function quote(string $str): string {
143
        if($this->driver === null) {
144
            throw new \Plasma\TransactionException('Transaction has been committed or rolled back');
145
        }
146
        
147
        return $this->driver->quote($str);
148
    }
149
    
150
    /**
151
     * Commits the changes.
152
     * @return \React\Promise\PromiseInterface
153
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
154
     */
155 1
    function commit(): \React\Promise\PromiseInterface {
156
        return $this->query('COMMIT')->then(function () {
157 1
            $this->driver->endTransaction();
158 1
            $this->client->checkinConnection($this->driver);
159 1
            $this->driver = null;
160 1
        });
161
    }
162
    
163
    /**
164
     * Rolls back the changes.
165
     * @return \React\Promise\PromiseInterface
166
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
167
     */
168 4
    function rollback(): \React\Promise\PromiseInterface {
169
        return $this->query('ROLLBACK')->then(function () {
170 3
            $this->driver->endTransaction();
171 3
            $this->client->checkinConnection($this->driver);
172 3
            $this->driver = null;
173 4
        });
174
    }
175
    
176
    /**
177
     * Creates a savepoint with the given identifier.
178
     * @param string  $identifier
179
     * @return \React\Promise\PromiseInterface
180
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
181
     */
182 1
    function createSavepoint(string $identifier): \React\Promise\PromiseInterface {
183 1
        return $this->query('SAVEPOINT '.$this->driver->quote($identifier));
184
    }
185
    
186
    /**
187
     * Rolls back to the savepoint with the given identifier.
188
     * @param string  $identifier
189
     * @return \React\Promise\PromiseInterface
190
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
191
     */
192 1
    function rollbackTo(string $identifier): \React\Promise\PromiseInterface {
193 1
        return $this->query('ROLLBACK TO '.$this->driver->quote($identifier));
194
    }
195
    
196
    /**
197
     * Releases the savepoint with the given identifier.
198
     * @param string  $identifier
199
     * @return \React\Promise\PromiseInterface
200
     * @throws \Plasma\TransactionException  Thrown if the transaction has been committed or rolled back.
201
     */
202 1
    function releaseSavepoint(string $identifier): \React\Promise\PromiseInterface {
203 1
        return $this->query('RELEASE SAVEPOINT '.$this->driver->quote($identifier));
204
    }
205
}
206