1 | <?php |
||
23 | class Handshake implements HandshakeInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $username; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $password; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $protocolVersion = 0; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $state; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $myR; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $clientFirstMessage; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $serverSignature; |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | private $version; |
||
64 | |||
65 | /** |
||
66 | * @param string $username |
||
67 | * @param string $password |
||
68 | * @param int $version |
||
69 | */ |
||
70 | 31 | public function __construct(string $username, string $password, int $version) |
|
77 | |||
78 | /** |
||
79 | * @inheritdoc |
||
80 | * @throws \RuntimeException |
||
81 | * @throws Exception |
||
82 | */ |
||
83 | 25 | public function hello(StreamInterface $stream): void |
|
84 | { |
||
85 | try { |
||
86 | 25 | $handshakeResponse = null; |
|
87 | 25 | while (true) { |
|
88 | 25 | if (!$stream->isWritable()) { |
|
89 | 1 | throw new Exception('Not connected'); |
|
90 | } |
||
91 | |||
92 | 24 | if ($handshakeResponse !== null && preg_match('/^ERROR:\s(.+)$/i', $handshakeResponse, |
|
93 | 24 | $errorMatch)) { |
|
94 | 2 | throw new Exception($errorMatch[1]); |
|
95 | } |
||
96 | |||
97 | try { |
||
98 | 24 | $msg = $this->nextMessage($handshakeResponse); |
|
99 | 2 | } catch (Exception $e) { |
|
100 | 2 | $stream->close(); |
|
101 | 2 | throw $e; |
|
102 | } |
||
103 | |||
104 | 24 | if ($msg === 'successful') { |
|
105 | 20 | break; |
|
106 | } |
||
107 | |||
108 | 24 | if ($msg !== '') { |
|
109 | 24 | $stream->write($msg); |
|
110 | } |
||
111 | |||
112 | // Read null-terminated response |
||
113 | 24 | $handshakeResponse = $stream->getContents(); |
|
114 | } |
||
115 | 5 | } catch (Exception $e) { |
|
116 | 5 | $stream->close(); |
|
117 | 5 | throw $e; |
|
118 | } |
||
119 | |||
120 | 20 | } |
|
121 | |||
122 | /** |
||
123 | * @param $response |
||
124 | * @return string |
||
125 | * @throws Exception |
||
126 | */ |
||
127 | 24 | private function nextMessage(string $response = null): ?string |
|
142 | |||
143 | /** |
||
144 | * @param string $password |
||
145 | * @param string $salt |
||
146 | * @param int $iterations |
||
147 | * @return string |
||
148 | */ |
||
149 | 20 | private function pkbdf2Hmac(string $password, string $salt, int $iterations): string |
|
160 | |||
161 | /** |
||
162 | * @param $response |
||
163 | * @return string |
||
164 | */ |
||
165 | 24 | private function createHandshakeMessage($response): string |
|
187 | |||
188 | /** |
||
189 | * @param $response |
||
190 | * @return string |
||
191 | * @throws Exception |
||
192 | */ |
||
193 | 22 | private function verifyProtocol($response): string |
|
194 | { |
||
195 | 22 | if (strpos($response, 'ERROR') === 0) { |
|
196 | throw new Exception( |
||
197 | 'Received an unexpected reply. You may be attempting to connect to ' |
||
198 | . 'a RethinkDB server that is too old for this driver. The minimum ' |
||
199 | . 'supported server version is 2.3.0.' |
||
200 | ); |
||
201 | } |
||
202 | |||
203 | 22 | $json = json_decode($response, true); |
|
204 | 22 | if ($json['success'] === false) { |
|
205 | 1 | throw new Exception('Handshake failed: ' . $json["error"]); |
|
206 | } |
||
207 | 21 | if ($this->protocolVersion > $json['max_protocol_version'] |
|
208 | 21 | || $this->protocolVersion < $json['min_protocol_version']) { |
|
209 | 1 | throw new Exception('Unsupported protocol version.'); |
|
210 | } |
||
211 | |||
212 | 20 | $this->state = 2; |
|
213 | |||
214 | 20 | return ''; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param $response |
||
219 | * @return string |
||
220 | * @throws Exception |
||
221 | */ |
||
222 | 20 | private function createAuthenticationMessage($response): string |
|
267 | |||
268 | /** |
||
269 | * @param $response |
||
270 | * @return string |
||
271 | * @throws Exception |
||
272 | */ |
||
273 | 20 | private function verifyAuthentication($response): string |
|
296 | } |
||
297 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.