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.
Completed
Push — master ( c2be35...bf6976 )
by Charlotte
03:23
created

Statement::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plasma Driver MySQL component
4
 * Copyright 2018 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 2
    function __construct(
73
        \Plasma\ClientInterface $client, \Plasma\Drivers\MySQL\Driver $driver, $id, string $query, string $rQuery, array $rParams, array $params, array $columns
74
    ) {
75 2
        $this->client = $client;
76 2
        $this->driver = $driver;
77
        
78 2
        $this->id = $id;
79 2
        $this->query = $query;
80 2
        $this->rewrittenQuery = $rQuery;
81 2
        $this->rewrittenParams = $rParams;
82 2
        $this->params = $params;
83 2
        $this->columns = $columns;
84 2
    }
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
    function getID() {
108
        return $this->id;
109
    }
110
    
111
    /**
112
     * Get the prepared query.
113
     * @return string
114
     */
115
    function getQuery(): string {
116
        return $this->query;
117
    }
118
    
119
    /**
120
     * Whether the statement has been closed.
121
     * @return bool
122
     */
123
    function isClosed(): bool {
124
        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 2
    function close(): \React\Promise\PromiseInterface {
133 2
        if($this->closed) {
134
            return \React\Promise\resolve();
135
        }
136
        
137 2
        $this->closed = true;
138
        
139 2
        $close = new \Plasma\Drivers\MySQL\Commands\StatementCloseCommand($this->driver, $this->id);
140 2
        $this->driver->executeCommand($close);
141
        
142
        return $close->getPromise()->then(function () {
143 2
            $this->client->checkinConnection($this->driver);
144 2
        });
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 2
    function execute(array $params = array()): \React\Promise\PromiseInterface {
155 2
        if($this->closed) {
156
            throw new \Plasma\Exception('Statement has been closed');
157
        }
158
        
159 2
        if(\count($params) < \count($this->params)) {
160
            throw new \Plasma\Exception('Not enough parameters for this statement, expected '.\count($this->params).', got '.\count($params));
161
        }
162
        
163 2
        $realParams = array();
164 2
        $pos = (\array_key_exists(0, $params) ? 0 : 1);
165
        
166 2
        foreach($this->rewrittenParams as $param) {
167 2
            $key = ($param[0] === ':' ? $param : ($pos++));
168
            
169 2
            if(!\array_key_exists($key, $params)) {
170
                throw new \Plasma\Exception('Missing parameter with key "'.$key.'"');
171
            }
172
            
173 2
            $realParams[] = $params[$key];
174
        }
175
        
176 2
        $execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $realParams);
177 2
        $this->driver->executeCommand($execute);
178
        
179 2
        return $execute->getPromise();
180
    }
181
    
182
    /**
183
     * Get the parsed parameters.
184
     * @return \Plasma\ColumnDefinitionInterface[]
185
     */
186
    function getParams(): array {
187
        return $this->params;
188
    }
189
    
190
    /**
191
     * Get the columns.
192
     * @return \Plasma\ColumnDefinitionInterface[]
193
     */
194
    function getColumns(): array {
195
        return $this->columns;
196
    }
197
}
198