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

Commit cba653f

Browse files
authored
Merge pull request repejota#85 from anthonysterling/fix-issue-57
Readability improvement of Connection::connect
2 parents d19c711 + 5b4a973 commit cba653f

3 files changed

Lines changed: 74 additions & 10 deletions

File tree

src/Nats/Connection.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private function getStream($address, $timeout)
214214
restore_error_handler();
215215

216216
if (!$fp) {
217-
throw new \Exception($errstr, $errno);
217+
throw Exception::forStreamSocketClientError($errstr, $errno);
218218
}
219219

220220
$timeout = number_format($timeout, 3);
@@ -256,16 +256,16 @@ public function connect($timeout = null)
256256
$msg = 'CONNECT '.$this->options;
257257
$this->send($msg);
258258
$connect_response = $this->receive();
259-
if (strpos($connect_response, '-ERR')!== false) {
260-
throw new \Exception("Failing connection: $connect_response");
259+
260+
if ($this->isErrorResponse($connect_response)) {
261+
throw Exception::forFailedConnection($connect_response);
261262
}
262263

263264
$this->ping();
264265
$ping_response = $this->receive();
265-
if ($ping_response !== "PONG") {
266-
if (strpos($ping_response, '-ERR')!== false) {
267-
throw new \Exception("Failing on first ping: $ping_response");
268-
}
266+
267+
if ($this->isErrorResponse($ping_response)) {
268+
throw Exception::forFailedPing($ping_response);
269269
}
270270
}
271271

@@ -408,14 +408,14 @@ private function handleMSG($line)
408408
$msg = new Message($subject, $payload, $sid, $this);
409409

410410
if (!isset($this->subscriptions[$sid])) {
411-
throw new Exception('subscription not found');
411+
throw Exception::forSubscriptionNotFound($sid);
412412
}
413413

414414
$func = $this->subscriptions[$sid];
415415
if (is_callable($func)) {
416416
$func($msg);
417417
} else {
418-
throw new Exception('not callable');
418+
throw Exception::forSubscriptionCallbackInvalid($sid);
419419
}
420420

421421
return;
@@ -524,4 +524,15 @@ public function streamSocket()
524524
{
525525
return $this->streamSocket;
526526
}
527+
528+
/**
529+
* Indicates whether $response is an error response.
530+
*
531+
* @param string $response The Nats Server response.
532+
* @return boolean
533+
*/
534+
private function isErrorResponse($response)
535+
{
536+
return false !== strpos('-ERR', $response);
537+
}
527538
}

src/Nats/Exception.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,59 @@
66
*/
77
class Exception extends \Exception
88
{
9+
/**
10+
* Creates an Exception for a failed connection.
11+
*
12+
* @param string $response The failed error response.
13+
* @return Nats\Exception
14+
*/
15+
public static function forFailedConnection($response)
16+
{
17+
return new static(sprintf('Failed to connect: %s', $response));
18+
}
919

20+
/**
21+
* Creates an Exception for a failed PING response.
22+
*
23+
* @param string $response The failed PING response.
24+
* @return Nats\Exception
25+
*/
26+
public static function forFailedPing($response)
27+
{
28+
return new static(sprintf('Failed to ping: %s', $response));
29+
}
30+
31+
/**
32+
* Creates an Exception for an invalid Subscription Identifier (sid).
33+
*
34+
* @param string $subscription The Subscription Identifier (sid).
35+
* @return Nats\Exception
36+
*/
37+
public static function forSubscriptionNotFound($subscription)
38+
{
39+
return new static(sprintf('Subscription not found: %s', $subscription));
40+
}
41+
42+
/**
43+
* Creates an Exception for an invalid Subscription Identifier (sid) callback.
44+
*
45+
* @param string $subscription The Subscription Identifier (sid).
46+
* @return Nats\Exception
47+
*/
48+
public static function forSubscriptionCallbackInvalid($subscription)
49+
{
50+
return new static(sprintf('Subscription callback is invalid: %s', $subscription));
51+
}
52+
53+
/**
54+
* Creates an Exception for the failed creation of a Stream Socket Client.
55+
*
56+
* @param string $message The system level error message.
57+
* @param integer $code The system level error code.
58+
* @return Nats\Exception
59+
*/
60+
public static function forStreamSocketClientError($message, $code)
61+
{
62+
return new static(sprintf('A Stream Socket Client could not be created: (%d) %s', $code, $message), $code);
63+
}
1064
}

test/ConnectionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ function ($message) use ($contentLen) {
158158

159159
$i++;
160160
} while ($i < 100);
161-
162161
}
163162

164163
/**

0 commit comments

Comments
 (0)