-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ecbe8c
commit 88d5ca2
Showing
6 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
require_once __DIR__ . "/../../vendor/autoload.php"; | ||
|
||
$connectionOptions = new \Nats\ConnectionOptions(); | ||
$connectionOptions->setHost('localhost')->setPort(4222); | ||
|
||
$c = new Nats\Connection($connectionOptions); | ||
$c->connect(); | ||
|
||
$c->request('sayhello', 'Marty McFly', function ($response) { | ||
echo $response->getBody(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
require_once __DIR__ . "/../../vendor/autoload.php"; | ||
|
||
$connectionOptions = new \Nats\ConnectionOptions(); | ||
$connectionOptions | ||
->setHost('localhost') | ||
->setPort(4222); | ||
$c = new Nats\Connection($connectionOptions); | ||
$c->connect(); | ||
|
||
$sid = $c->subscribe("sayhello", function ($res) { | ||
$res->reply("Hello, " . $res->getBody() . " !!!"); | ||
}); | ||
|
||
$c->wait(2); | ||
|
||
$c->unsubscribe($sid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
namespace Nats; | ||
|
||
/** | ||
* Message Class. | ||
*/ | ||
class Message | ||
{ | ||
/** | ||
* Message Subject | ||
* | ||
* @var string | ||
*/ | ||
private $subject; | ||
|
||
/** | ||
* Message Body | ||
* | ||
* @var string | ||
*/ | ||
private $body; | ||
|
||
/** | ||
* Message Ssid | ||
* | ||
* @var string | ||
*/ | ||
private $sid; | ||
|
||
/** | ||
* Message related connection | ||
* | ||
* @var string | ||
*/ | ||
private $conn; | ||
|
||
/** | ||
* Message constructor | ||
* @param string $subject | ||
* @param string $body | ||
* @param string $sid | ||
* @param Connection $conn | ||
*/ | ||
public function __construct($subject, $body, $sid, $conn){ | ||
$this->setSubject($subject); | ||
$this->setBody($body); | ||
$this->setSid($sid); | ||
$this->setConn($conn); | ||
} | ||
|
||
/** | ||
* Set subject | ||
* | ||
* @param string $subject Subject | ||
* @return $this | ||
*/ | ||
public function setSubject($subject) { | ||
$this->subject = $subject; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get subject | ||
* | ||
* @return string | ||
*/ | ||
public function getSubject() { | ||
return $this->subject; | ||
} | ||
|
||
/** | ||
* Set body | ||
* | ||
* @param string $body Body | ||
* @return $this | ||
*/ | ||
public function setBody($body) { | ||
$this->body = $body; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get body | ||
* | ||
* @return string | ||
*/ | ||
public function getBody() { | ||
return $this->body; | ||
} | ||
|
||
/** | ||
* Set Ssid | ||
* | ||
* @param string $sid Ssid | ||
* @return $this | ||
*/ | ||
public function setSid($sid) { | ||
$this->sid = $sid; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get Ssid | ||
* | ||
* @return string | ||
*/ | ||
public function getSid() { | ||
return $this->sid; | ||
} | ||
|
||
/** | ||
* String representation of a message | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() { | ||
return $this->getBody(); | ||
} | ||
|
||
/** | ||
* Set Conn | ||
* | ||
* @param Connection $conn | ||
* @return $this | ||
*/ | ||
public function setConn($conn) { | ||
$this->conn = $conn; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get Conn | ||
* | ||
* @return string | ||
*/ | ||
public function getConn() { | ||
return $this->conn; | ||
} | ||
|
||
/** | ||
* Allows you reply the message with a specific body | ||
* | ||
* @param string $body | ||
*/ | ||
public function reply($body) { | ||
$this->getConn()->publish( | ||
$this->getSubject(), $body | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
namespace Nats\tests\Unit; | ||
|
||
use Nats\Connection; | ||
use Nats\Message; | ||
|
||
/** | ||
* Class MessageTest | ||
*/ | ||
class MessageTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Tests Message getters and setters. Only necessary for code coverage. | ||
* | ||
* @return void | ||
*/ | ||
public function testSettersAndGetters() | ||
{ | ||
$conn = new Connection(); | ||
|
||
$msg = new Message('subject', 'body', 'sid', $conn); | ||
|
||
$this->assertEquals('subject', $msg->getSubject()); | ||
$this->assertEquals('body', $msg->getBody()); | ||
$this->assertEquals('sid', $msg->getSid()); | ||
$this->assertEquals($conn, $msg->getConn()); | ||
|
||
$msg | ||
->setSubject('subject2') | ||
->setBody('body2') | ||
->setSid('sid2'); | ||
|
||
$this->assertEquals('subject2', $msg->getSubject()); | ||
$this->assertEquals('body2', $msg->getBody()); | ||
$this->assertEquals('sid2', $msg->getSid()); | ||
|
||
} | ||
} |