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
Pull Request — master (#25)
by Charlotte
02:35
created

Statement   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 91.49%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 202
ccs 43
cts 47
cp 0.9149
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 5 2
A __construct() 0 12 1
A isClosed() 0 2 1
A getID() 0 2 1
A close() 0 12 2
A getQuery() 0 2 1
A execute() 0 11 2
A runQuery() 0 2 1
A getColumns() 0 2 1
A getParams() 0 2 1
A createCursor() 0 14 3
1
<?php
2
/**
3
 * Plasma Driver MySQL component
4
 * Copyright 2018-2019 PlasmaPHP, All Rights Reserved
5
 *
6
 * Website: https://github.com/PlasmaPHP
7
 * License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE
8
*/
9
10
namespace Plasma\Drivers\MySQL;
11
12
/**
13
 * Represents a Prepared Statement.
14
 */
15
class Statement implements \Plasma\StatementInterface {
16
    /**
17
     * @var \Plasma\ClientInterface
18
     */
19
    protected $client;
20
    
21
    /**
22
     * @var \Plasma\Drivers\MySQL\Driver
23
     */
24
    protected $driver;
25
    
26
    /**
27
     * @var mixed
28
     */
29
    protected $id;
30
    
31
    /**
32
     * @var string
33
     */
34
    protected $query;
35
    
36
    /**
37
     * @var string
38
     */
39
    protected $rewrittenQuery;
40
    
41
    /**
42
     * @var array
43
     */
44
    protected $rewrittenParams;
45
    
46
    /**
47
     * @var \Plasma\ColumnDefinitionInterface[]
48
     */
49
    protected $params;
50
    
51
    /**
52
     * @var \Plasma\ColumnDefinitionInterface[]
53
     */
54
    protected $columns;
55
    
56
    /**
57
     * @var bool
58
     */
59
    protected $closed = false;
60
    
61
    /**
62
     * Constructor.
63
     * @param \Plasma\ClientInterface              $client
64
     * @param \Plasma\Drivers\MySQL\Driver         $driver
65
     * @param mixed                                $id
66
     * @param string                               $query
67
     * @param string                               $rQuery
68
     * @param array                                $rParams
69
     * @param \Plasma\ColumnDefinitionInterface[]  $params
70
     * @param \Plasma\ColumnDefinitionInterface[]  $columns
71
     */
72 47
    function __construct(
73
        \Plasma\ClientInterface $client, \Plasma\Drivers\MySQL\Driver $driver, $id, string $query, string $rQuery, array $rParams, array $params, array $columns
74
    ) {
75 47
        $this->client = $client;
76 47
        $this->driver = $driver;
77
        
78 47
        $this->id = $id;
79 47
        $this->query = $query;
80 47
        $this->rewrittenQuery = $rQuery;
81 47
        $this->rewrittenParams = $rParams;
82 47
        $this->params = $params;
83 47
        $this->columns = $columns;
84 47
    }
85
    
86
    /**
87
     * Destructor. Runs once the instance goes out of scope.
88
     * Please do not rely on the destructor to properly close your statement.
89
     * ALWAYS explicitely close the statement once you're done.
90
     * @throws \Plasma\Exception
91
     * @codeCoverageIgnore
92
     */
93
    function __destruct() {
94
        if(!$this->closed) {
95
            $this->close()->then(null, function () {
96
                // Error during implicit close, close the session
97
                $this->driver->close();
98
            });
99
        }
100
    }
101
    
102
    /**
103
     * Get the driver-dependent ID of this statement.
104
     * The return type can be of ANY type, as the ID depends on the driver and DBMS.
105
     * @return mixed
106
     */
107 2
    function getID() {
108 2
        return $this->id;
109
    }
110
    
111
    /**
112
     * Get the prepared query.
113
     * @return string
114
     */
115 2
    function getQuery(): string {
116 2
        return $this->query;
117
    }
118
    
119
    /**
120
     * Whether the statement has been closed.
121
     * @return bool
122
     */
123 3
    function isClosed(): bool {
124 3
        return $this->closed;
125
    }
126
    
127
    /**
128
     * Closes the prepared statement and frees the associated resources on the server.
129
     * Closing a statement more than once has no effect.
130
     * @return \React\Promise\PromiseInterface
131
     */
132 47
    function close(): \React\Promise\PromiseInterface {
133 47
        if($this->closed) {
134 1
            return \React\Promise\resolve();
135
        }
136
        
137 47
        $this->closed = true;
138
        
139 47
        $close = new \Plasma\Drivers\MySQL\Commands\StatementCloseCommand($this->driver, $this->id);
140 47
        $this->driver->executeCommand($close);
141
        
142
        return $close->getPromise()->then(function () {
143 37
            $this->client->checkinConnection($this->driver);
144 47
        });
145
    }
146
    
147
    /**
148
     * Executes the prepared statement. Resolves with a `QueryResult` instance.
149
     * @param array  $params
150
     * @return \React\Promise\PromiseInterface
151
     * @throws \Plasma\Exception  Thrown if the statement is executed after it has been closed, or thrown for insufficent or missing parameters.
152
     * @see \Plasma\QueryResultInterface
153
     */
154 41
    function execute(array $params = array()): \React\Promise\PromiseInterface {
155 41
        if($this->closed) {
156 1
            throw new \Plasma\Exception('Statement has been closed');
157
        }
158
        
159 40
        $params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params);
160
        
161 38
        $execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params);
162 38
        $this->driver->executeCommand($execute);
163
        
164 38
        return $execute->getPromise();
165
    }
166
    
167
    /**
168
     * Runs the given querybuilder on the underlying driver instance. However the query will be ignored, only the parameters are used.
169
     * The driver CAN throw an exception if the given querybuilder is not supported.
170
     * An example would be a SQL querybuilder and a Cassandra driver.
171
     * @param \Plasma\QueryBuilderInterface  $query
172
     * @return \React\Promise\PromiseInterface
173
     * @throws \Plasma\Exception
174
     */
175
    function runQuery(\Plasma\QueryBuilderInterface $query): \React\Promise\PromiseInterface {
176
        return $this->execute($query->getParameters());
177
    }
178
    
179
    /**
180
     * Creates a cursor.
181
     * @param array  $params
182
     * @return \React\Promise\PromiseInterface
183
     * @throws \Plasma\Exception  Thrown if the statement is executed after it has been closed, or if it's not a SELECT query, or for insufficent or missing parameters.
184
     * @internal
185
     */
186 1
    function createCursor(array $params = array()): \React\Promise\PromiseInterface {
187 1
        if($this->closed) {
188
            throw new \Plasma\Exception('Statement has been closed');
189 1
        } elseif(empty($this->columns)) {
190
            throw new \Plasma\Exception('Query is not a SELECT query');
191
        }
192
        
193 1
        $params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params);
194
        
195 1
        $execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params, 0x05);
196 1
        $this->driver->executeCommand($execute);
197
        
198
        return $execute->getPromise()->then(function () {
199 1
            return (new \Plasma\Drivers\MySQL\StatementCursor($this->driver, $this));
200 1
        });
201
    }
202
    
203
    /**
204
     * Get the parsed parameters.
205
     * @return \Plasma\ColumnDefinitionInterface[]
206
     */
207 2
    function getParams(): array {
208 2
        return $this->params;
209
    }
210
    
211
    /**
212
     * Get the columns.
213
     * @return \Plasma\ColumnDefinitionInterface[]
214
     */
215 2
    function getColumns(): array {
216 2
        return $this->columns;
217
    }
218
}
219