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

StatementPrepareCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 83.72%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 148
ccs 36
cts 43
cp 0.8372
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncodedMessage() 0 2 1
A resetSequence() 0 2 1
C onNext() 0 35 13
A createResolve() 0 9 1
A __construct() 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 2
    function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, string $query) {
81 2
        parent::__construct($driver);
82
        
83 2
        $this->client = $client;
84 2
        $this->driver = $driver;
85 2
        $this->query = $query;
86 2
        $this->deprecatedEOF = (($driver->getHandshake()->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_DEPRECATE_EOF) !== 0);
87
        
88 2
        [ 'query' => $this->rewrittenQuery, 'parameters' => $this->rewrittenParams ] = \Plasma\Utility::parseParameters($this->query, '?');
89 2
    }
90
    
91
    /**
92
     * Get the encoded message for writing to the database connection.
93
     * @return string
94
     */
95 2
    function getEncodedMessage(): string {
96 2
        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 2
    function onNext($value): void {
106 2
        if($value instanceof \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage) {
107 2
            $this->okResponse = $value;
108 2
            $this->fieldsCount = $this->okResponse->numColumns;
109 2
        } elseif($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) {
110 2
            $buffer = $value->getBuffer();
111 2
            $parser = $value->getParser();
112
            
113 2
            $parsed = $this->handleQueryOnNextCallerColumns($buffer, $parser);
114
            
115 2
            if($this->paramsDone) {
116 2
                $this->fields[$parsed->getName()] = $parsed;
117
            } else {
118 2
                $this->params[] = $parsed;
119
            }
120
            
121 2
            if($this->deprecatedEOF) {
122 2
                if($this->paramsDone && $this->fieldsCount <= \count($this->fields)) {
123 2
                    $this->createResolve();
124 2
                } elseif($this->okResponse->numParams <= \count($this->params) && $this->resolveValue === null) {
125 2
                    $this->paramsDone = true;
126
                }
127
            }
128
        } elseif(
129
            $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage
130
        ) {
131
            if(!$this->paramsDone) {
132
                $this->paramsDone = true;
133
                return;
134
            }
135
            
136
            $this->createResolve();
137
        } else {
138
            throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received value of type '
139
                .(\is_object($value) ? \get_class($value) : \gettype($value)).' it can not handle');
140
        }
141 2
    }
142
    
143
    /**
144
     * Whether the sequence ID should be resetted.
145
     * @return bool
146
     */
147 2
    function resetSequence(): bool {
148 2
        return true;
149
    }
150
    
151
    /**
152
     * Creates the resolve value.
153
     * @return void
154
     */
155 2
    protected function createResolve(): void {
156 2
        $this->finished = true;
157
        
158 2
        $id = $this->okResponse->statementID;
159 2
        $queryr = $this->rewrittenQuery;
160 2
        $paramsr = $this->rewrittenParams;
161
        
162 2
        $this->resolveValue = new \Plasma\Drivers\MySQL\Statement($this->client, $this->driver, $id, $this->query, $queryr, $paramsr, $this->params, $this->fields);
163 2
        $this->deferred->resolve($this->resolveValue);
164 2
    }
165
}
166