Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit bb195b5

Browse files
author
Raül Pérez
committed
Small refactor
1 parent b9d249d commit bb195b5

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

src/Connection.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ public function isConnected()
225225
*/
226226
public function connect()
227227
{
228+
$options = '{ "verbose": false, "pedantic": false, "reconnect": true }';
229+
228230
$this->_streamSocket = $this->_getStream($this->_address);
229-
$msg = 'CONNECT {}';
231+
$msg = 'CONNECT '. $options;
230232
$this->_send($msg);
231233
}
232234

@@ -288,6 +290,37 @@ public function unsubscribe($sid)
288290
$this->_send($msg);
289291
}
290292

293+
/**
294+
* Handles PING command
295+
*
296+
* @return void
297+
*/
298+
private function _handlePING() {
299+
$this->_send("PONG");
300+
}
301+
302+
/**
303+
* Handles MSG command
304+
*
305+
* @param string $line Message command from NATS
306+
*
307+
* @return \Exception|void
308+
*/
309+
private function _handleMSG($line) {
310+
$parts = explode(" ", $line);
311+
$length = $parts[3];
312+
$sid = $parts[2];
313+
314+
$payload = $this->_receive($length);
315+
316+
$func = $this->_subscriptions[$sid];
317+
if (is_callable($func)) {
318+
$func($payload);
319+
} else {
320+
return new \Exception("not callable");
321+
}
322+
}
323+
291324
/**
292325
* Waits for messages
293326
*
@@ -303,26 +336,13 @@ public function wait($quantity = 0)
303336

304337
// PING
305338
if (strpos($line, 'PING') === 0) {
306-
$this->_send("PONG");
339+
$this->_handlePing();
307340
}
308341

309342
// MSG
310343
if (strpos($line, 'MSG') === 0) {
311344
$count = $count + 1;
312-
313-
$parts = explode(" ", $line);
314-
$length = $parts[3];
315-
$sid = $parts[2];
316-
317-
$payload = $this->_receive($length);
318-
319-
$func = $this->_subscriptions[$sid];
320-
if (is_callable($func)) {
321-
$func($payload);
322-
} else {
323-
return new \Exception("not callable");
324-
}
325-
345+
$this->_handleMSG($line);
326346
if (($quantity != 0) && ($count >= $quantity)) {
327347
return null;
328348
}

tests/Unit/ConnectionTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class TestConnection extends TestCase
2929
{
3030
private $_c;
3131

32-
private $_server;
33-
3432
/**
3533
* Setup tests
3634
*

0 commit comments

Comments
 (0)