Response::getResultData()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 6
nop 0
1
<?php
2
namespace evseevnn\Cassandra\Protocol;
3
use evseevnn\Cassandra\Enum\DataTypeEnum;
4
use evseevnn\Cassandra\Enum\ErrorCodesEnum;
5
use evseevnn\Cassandra\Enum\OpcodeEnum;
6
use evseevnn\Cassandra\Enum\ResultTypeEnum;
7
use evseevnn\Cassandra\Exception\ResponseException;
8
use evseevnn\Cassandra\Protocol\Response\DataStream;
9
use evseevnn\Cassandra\Protocol\Response\DataStream\TypeReader;
10
use evseevnn\Cassandra\Protocol\Response\Rows;
11
12
class Response {
13
14
	/**
15
	 * @var int
16
	 */
17
	private $type;
18
19
	/**
20
	 * @var string
21
	 */
22
	private $binary;
23
24
	/**
25
	 * @var Response\DataStream
26
	 */
27
	private $dataStream;
28
29
	/**
30
	 * @param int $type OpcodeEnum::* constants
31
	 * @param string $binary
32
	 */
33
	public function __construct($type, $binary) {
34
		$this->type = $type;
35
		$this->binary = $binary;
36
		$this->dataStream = new DataStream($binary);
37
	}
38
39
	/**
40
	 * Return response type
41
	 * @return int
42
	 */
43
	public function getType() {
44
		return $this->type;
45
	}
46
47
	public function getData() {
48
		switch($this->type) {
49
			case OpcodeEnum::ERROR:
50
				return $this->getErrorData();
51
52
			case OpcodeEnum::READY:
53
				/**
54
				 * Indicates that the server is ready to process queries. This message will be
55
				 * sent by the server either after a STARTUP message if no authentication is
56
				 * required, or after a successful CREDENTIALS message.
57
				 */
58
				return null;
59
60
			case OpcodeEnum::AUTHENTICATE:
61
				return unpack('n', $this->binary)[1];
62
63
			case OpcodeEnum::SUPPORTED:
64
				/**
65
				 * TODO Check it!
66
				 * Indicates which startup options are supported by the server. This message
67
				 * comes as a response to an OPTIONS message.
68
				 *
69
				 * The body of a SUPPORTED message is a [string multimap]. This multimap gives
70
				 * for each of the supported STARTUP options, the list of supported values.
71
				 */
72
				return $this->dataStream->readByType(['type' => DataTypeEnum::COLLECTION_MAP]);
73
74
			case OpcodeEnum::RESULT:
75
				return $this->getResultData();
76
77
			case OpcodeEnum::EVENT:
78
				// TODO
79
				return '';
80
81
			default:
82
				throw new ResponseException('Unknown response');
83
		}
84
	}
85
86
	/**
87
	 * Indicates an error processing a request. The body of the message will be an
88
	 * error code ([int]) followed by a [string] error message. Then, depending on
89
	 * the exception, more content may follow. The error codes are defined in
90
	 * Section 7, along with their additional content if any.
91
	 *
92
	 * @return string
93
	 */
94
	private function getErrorData() {
95
		$stream = $this->dataStream;
96
		$error = $stream->readInt();
97
98
		$errorMessages = [
99
			ErrorCodesEnum::SERVER_ERROR => function(DataStream $stream) {
100
					return "Server error: {$stream->readString()}";
101
				},
102
			ErrorCodesEnum::PROTOCOL_ERROR => function(DataStream $stream) {
103
					return "Protocol error: {$stream->readString()}";
104
				},
105
			ErrorCodesEnum::BAD_CREDENTIALS => function(DataStream $stream) {
106
					return "Bad credentials: {$stream->readString()}";
107
				},
108 View Code Duplication
			ErrorCodesEnum::UNAVAILABLE_EXCEPTION => function(DataStream $stream) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
					$errorData = var_export([
110
						'consistency' => $stream->readInt(),
111
						'node' => $stream->readInt(),
112
						'replica' => $stream->readInt()
113
					], true);
114
115
					return "Unavailable exception. Error data: {$errorData}";
116
				},
117
			ErrorCodesEnum::OVERLOADED => function(DataStream $stream) {
118
					return "Overloaded: {$stream->readString()}";
119
				},
120
			ErrorCodesEnum::IS_BOOTSTRAPPING => function(DataStream $stream) {
121
					return "Is_bootstrapping: {$stream->readString()}";
122
				},
123
			ErrorCodesEnum::TRUNCATE_ERROR => function(DataStream $stream) {
124
					return "Truncate_error: {$stream->readString()}";
125
				},
126 View Code Duplication
			ErrorCodesEnum::WRITE_TIMEOUT => function(DataStream $stream) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
					$errorData = var_export([
128
						'consistency' => $stream->readInt(),
129
						'node' => $stream->readInt(),
130
						'replica' => $stream->readInt(),
131
						'writeType' => $stream->readString()
132
					], true);
133
					return "Write_timeout. Error data: {$errorData}";
134
				},
135 View Code Duplication
			ErrorCodesEnum::READ_TIMEOUT => function(DataStream $stream) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
					$errorData = var_export([
137
						'consistency' => $stream->readInt(),
138
						'node' => $stream->readInt(),
139
						'replica' => $stream->readInt(),
140
						'dataPresent' => $stream->readChar()
141
					], true);
142
					return "Read_timeout. Error data: {$errorData}";
143
				},
144
			ErrorCodesEnum::SYNTAX_ERROR => function(DataStream $stream) {
145
					return "Syntax_error: {$stream->readString()}";
146
				},
147
			ErrorCodesEnum::UNAUTHORIZED => function(DataStream $stream) {
148
					return "Unauthorized: {$stream->readString()}";
149
				},
150
			ErrorCodesEnum::INVALID => function(DataStream $stream) {
151
					return "Invalid: {$stream->readString()}";
152
				},
153
			ErrorCodesEnum::CONFIG_ERROR => function(DataStream $stream) {
154
					return "Config_error: {$stream->readString()}";
155
				},
156
			ErrorCodesEnum::ALREADY_EXIST => function(DataStream $stream) {
157
					return "Already_exists: {$stream->readString()}";
158
				},
159
			ErrorCodesEnum::UNPREPARED => function(DataStream $stream) {
160
					return "Unprepared: {$stream->readShort()}";
161
				}
162
		];
163
164
		return isset($errorMessages[$error]) ? $errorMessages[$error]($stream) : 'Unknown error';
165
	}
166
167
	/**
168
	 * @return Rows|string|array|null
169
	 */
170
	private function getResultData() {
171
		$kind = $this->dataStream->readInt();
172
		switch($kind) {
173
			case ResultTypeEnum::VOID:
174
				return null;
175
176
			case ResultTypeEnum::ROWS:
177
				return new Rows($this->dataStream, $this->getMetadata());
178
179
			case ResultTypeEnum::SET_KEYSPACE:
180
				return $this->dataStream->readString();
181
182
			case ResultTypeEnum::PREPARED:
183
				return [
184
					'id' => $this->dataStream->readString(),
185
					'metadata' => $this->getMetadata()
186
				];
187
188
			case ResultTypeEnum::SCHEMA_CHANGE:
189
				return [
190
					'change' => $this->dataStream->readString(),
191
					'keyspace' => $this->dataStream->readString(),
192
					'table' => $this->dataStream->readString()
193
				];
194
		}
195
196
		return null;
197
	}
198
199
	/**
200
	 * Return metadata
201
	 * @return array
202
	 */
203
	private function getMetadata() {
204
		$flags = $this->dataStream->readInt();
205
		$columnCount = $this->dataStream->readInt();
206
		$globalTableSpec = $flags & 0x0001;
207
		if ($globalTableSpec) {
208
			$keyspace = $this->dataStream->readString();
209
			$tableName = $this->dataStream->readString();
210
		}
211
212
		$columns = [];
213
		for ($i = 0; $i < $columnCount; ++$i) {
214
			if (isset($keyspace, $tableName)) {
215
				$columnData = [
216
					'keyspace' => $keyspace,
217
					'tableName' => $tableName,
218
					'name' => $this->dataStream->readString(),
219
					'type' => TypeReader::readFromStream($this->dataStream)
220
				];
221
			} else {
222
				$columnData = [
223
					'keyspace' => $this->dataStream->readString(),
224
					'tableName' => $this->dataStream->readString(),
225
					'name' => $this->dataStream->readString(),
226
					'type' => TypeReader::readFromStream($this->dataStream)
227
				];
228
			}
229
230
			$columns[] = $columnData;
231
		}
232
233
		return [
234
			'columnCount' => $columnCount,
235
			'columns' => $columns
236
		];
237
	}
238
}