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 ( 2ab4bc...1bb41b )
by Charlotte
03:40
created

StatementPrepareCommand   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Test Coverage

Coverage 82%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 162
ccs 41
cts 50
cp 0.82
rs 10
c 0
b 0
f 0
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncodedMessage() 0 2 1
A __construct() 0 9 1
A resetSequence() 0 2 1
C onNext() 0 49 17
A createResolve() 0 9 1
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\Commands;
11
12
/**
13
 * Statement Prepare command.
14
 * @internal
15
 */
16
class StatementPrepareCommand extends PromiseCommand {
17
    /**
18
     * The identifier for this command.
19
     * @var int
20
     * @source
21
     */
22
    const COMMAND_ID = 0x16;
23
    
24
    /**
25
     * @var \Plasma\ClientInterface
26
     */
27
    protected $client;
28
    
29
    /**
30
     * @var string
31
     */
32
    protected $query;
33
    
34
    /**
35
     * @var string
36
     */
37
    protected $rewrittenQuery;
38
    
39
    /**
40
     * @var array
41
     */
42
    protected $rewrittenParams;
43
    
44
    /**
45
     * @var \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage
46
     */
47
    protected $okResponse;
48
    
49
    /**
50
     * @var \Plasma\ColumnDefinitionInterface[]
51
     */
52
    protected $params = array();
53
    
54
    /**
55
     * @var \Plasma\ColumnDefinitionInterface[]
56
     */
57
    protected $fields = array();
58
    
59
    /**
60
     * @var bool
61
     */
62
    protected $paramsDone = false;
63
    
64
    /**
65
     * @var \Plasma\StatementInterface|null
66
     */
67
    protected $resolveValue;
68
    
69
    /**
70
     * @var bool
71
     */
72
    protected $deprecatedEOF;
73
    
74
    /**
75
     * Constructor.
76
     * @param \Plasma\ClientInterface  $client
77
     * @param \Plasma\DriverInterface  $driver
78
     * @param string                   $query
79
     */
80 34
    function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, string $query) {
81 34
        parent::__construct($driver);
82
        
83 34
        $this->client = $client;
84 34
        $this->driver = $driver;
85 34
        $this->query = $query;
86 34
        $this->deprecatedEOF = (($driver->getHandshake()->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_DEPRECATE_EOF) !== 0);
87
        
88 34
        [ 'query' => $this->rewrittenQuery, 'parameters' => $this->rewrittenParams ] = \Plasma\Utility::parseParameters($this->query, '?');
89 34
    }
90
    
91
    /**
92
     * Get the encoded message for writing to the database connection.
93
     * @return string
94
     */
95 34
    function getEncodedMessage(): string {
96 34
        return \chr(static::COMMAND_ID).$this->rewrittenQuery;
97
    }
98
    
99
    /**
100
     * Sends the next received value into the command.
101
     * @param mixed  $value
102
     * @return void
103
     * @throws \Plasma\Drivers\MySQL\Messages\ParseException
104
     */
105 34
    function onNext($value): void {
106 34
        if($value instanceof \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage) {
107 34
            $this->okResponse = $value;
108 34
            $this->fieldsCount = $this->okResponse->numColumns;
109 34
            $this->paramsDone = ($this->okResponse->numParams === 0);
110
            
111 34
            if($this->paramsDone && $this->fieldsCount === 0) {
112 34
                $this->createResolve();
113
            }
114 34
        } elseif($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) {
115 34
            $buffer = $value->getBuffer();
116 34
            $parser = $value->getParser();
117
            
118 34
            $parsed = $this->handleQueryOnNextCallerColumns($buffer, $parser);
119
            
120 34
            if($this->paramsDone) {
121 2
                $this->fields[$parsed->getName()] = $parsed;
122
            } else {
123 34
                $this->params[] = $parsed;
124
            }
125
            
126 34
            if($this->deprecatedEOF) {
127 34
                if($this->paramsDone && $this->fieldsCount <= \count($this->fields)) {
128 2
                    $this->createResolve();
129 34
                } elseif($this->okResponse->numParams <= \count($this->params) && $this->resolveValue === null) {
130 34
                    $this->paramsDone = true;
131
                    
132 34
                    if($this->fieldsCount === 0) {
133 34
                        $this->createResolve();
134
                    }
135
                }
136
            }
137
        } elseif(
138
            $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage
139
        ) {
140
            if(!$this->paramsDone) {
141
                $this->paramsDone = true;
142
                
143
                if($this->fieldsCount === 0) {
144
                    $this->createResolve();
145
                }
146
                
147
                return;
148
            }
149
            
150
            $this->createResolve();
151
        } else {
152
            throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received value of type '
153
                .(\is_object($value) ? \get_class($value) : \gettype($value)).' it can not handle');
154
        }
155 34
    }
156
    
157
    /**
158
     * Whether the sequence ID should be resetted.
159
     * @return bool
160
     */
161 34
    function resetSequence(): bool {
162 34
        return true;
163
    }
164
    
165
    /**
166
     * Creates the resolve value.
167
     * @return void
168
     */
169 34
    protected function createResolve(): void {
170 34
        $this->finished = true;
171
        
172 34
        $id = $this->okResponse->statementID;
173 34
        $queryr = $this->rewrittenQuery;
174 34
        $paramsr = $this->rewrittenParams;
175
        
176 34
        $this->resolveValue = new \Plasma\Drivers\MySQL\Statement($this->client, $this->driver, $id, $this->query, $queryr, $paramsr, $this->params, $this->fields);
177 34
        $this->deferred->resolve($this->resolveValue);
178 34
    }
179
}
180