Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12242666
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
9 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/AMQPBroker.php b/src/AMQPBroker.php
index 5bdcb4c..c61ca53 100644
--- a/src/AMQPBroker.php
+++ b/src/AMQPBroker.php
@@ -1,148 +1,184 @@
<?php
namespace Keruald\Broker;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class AMQPBroker extends Broker {
///
/// Private members
///
/**
* @var PhpAmqpLib\Connection\AMQPStreamConnection
*/
private $connection = null;
/**
* @var PhpAmqpLib\Channel\AMQPChannel
*/
private $channel = null;
/**
* The routing key, for topic exchange
*
* @var string
*/
private $routingKey = '';
/**
* The target name
*/
private $targetName = '';
///
/// Constructor, destructor
///
/**
* Cleans up resources
*/
function __destruct() {
$this->disconnect();
}
+ /**
+ * Gets the default values for connect method
+ *
+ * @return array
+ */
+ private static function getDefaultValues () {
+ return [
+ 'host' => 'localhost',
+ 'port' => 5672,
+ 'username' => 'guest',
+ 'password' => 'guest',
+ 'vhost' => '/',
+ ];
+ }
+
+ /**
+ * Initializes a new instance of the broker from specified parameters
+ *
+ * @param array $params An array with connect information
+ * @return Keruald\Broker\AMQPBroker A connected instance of the broker
+ */
+ static public function makeFromConfig ($params) {
+ $instance = new self;
+
+ $params = self::getDefaultValues() + $params;
+ $instance->connect(
+ $params['host'],
+ $params['port'],
+ $params['username'],
+ $params['password'],
+ $params['vhost']
+ );
+
+ return $instance;
+ }
+
///
/// Connection
///
/**
* Connects to the message broker
*
* @param string $host The broker host
* @param int $port The broker port
* @param string $username The broker username
* @param string $password The broker password
* @param string $vhost The broker vhost
*/
public function connect (
$host = 'localhost',
$port = 5672,
$username = 'guest',
$password = 'guest',
$vhost = '/'
) {
$this->connection = new AMQPStreamConnection(
$host, $port,
$username, $password,
$vhost
);
$this->channel = $this->connection->channel();
return $this;
}
/**
* Disconnects from the message broker
*/
public function disconnect () {
if ($this->connection !== null) {
$this->channel->close();
$this->connection->close();
}
return $this;
}
///
/// Target methods
///
/**
* Sets an exchange point as a target to publish messages to
*
* @param string $name The exchange name
* @param string $type The exchange type (direct, fanout, topic, headers)
*/
public function setExchangeTarget ($name, $type = 'topic') {
$this->targetName = $name;
$this->channel->exchange_declare(
$name,
$type,
false, false, false // don't autodelete this target
);
return $this;
}
/**
* Sets a queue as a target to publish messages to
*
* @param string $name The name of the queue
*/
public function setQueueTarget ($name) {
$this->targetName = $name;
$this->channel->queue_declare(
$name,
false, false, false, false // don't autodelete this target
);
return $this;
}
///
/// Message methods
///
/**
* Sets the routing key
*
* @param string $key the routing key
*/
public function routeTo ($key) {
$this->routingKey = $key;
return $this;
}
/**
* Sends a message to the specified target queue or exchange
*
* @param string $message The message to send
*/
public function sendMessage ($message) {
$this->channel->basic_publish(
new AMQPMessage($message),
$this->targetName,
$this->routingKey
);
return $this;
}
}
diff --git a/src/BlackholeBroker.php b/src/BlackholeBroker.php
index 744d78c..36ecdeb 100644
--- a/src/BlackholeBroker.php
+++ b/src/BlackholeBroker.php
@@ -1,52 +1,68 @@
<?php
namespace Keruald\Broker;
class BlackholeBroker extends Broker {
///
/// Broker implementation
///
/**
* Sends a message to the broker, which will silently discards it.
*
* @param string $message The message to send
*/
public final function sendMessage ($message) {
return $this;
}
+ /**
+ * Initializes a new instance of the broker from specified parameters
+ *
+ * @param array $params An array with omnipotence information
+ * @return Keruald\Broker\BlackholeBroker A connected instance of the broker
+ */
+ static public function makeFromConfig ($params) {
+ $instance = new self;
+
+ if ($params['omnipotence']) {
+ $instance->acceptAllMethodCalls();
+ }
+
+ return $instance;
+ }
+
///
/// Omnipotence to ease lightweight mock testing without any lib
///
/**
* @var bool
*/
private $acceptAllMethodCalls = false;
/**
* Configures the broker to accept every method call
*/
public function acceptAllMethodCalls () {
$this->acceptAllMethodCalls = true;
return $this;
}
/**
* Handles a method overloading call
*
* @param string $name The name of the method being called
* @param array $arguments An enumerated array containing the parameters
* passed to the method
*/
public function __call ($name, array $arguments) {
if ($this->acceptAllMethodCalls) {
return $this; // Brokers are intended to be fluent
}
throw new \BadFunctionCallException(
"Blackhole broker doesn't implement the $name method."
);
}
}
diff --git a/src/Broker.php b/src/Broker.php
index 53170e2..c8bb487 100644
--- a/src/Broker.php
+++ b/src/Broker.php
@@ -1,7 +1,9 @@
<?php
namespace Keruald\Broker;
abstract class Broker {
abstract public function sendMessage ($message);
+
+ abstract static public function makeFromConfig ($params);
}
diff --git a/src/BrokerFactory.php b/src/BrokerFactory.php
new file mode 100644
index 0000000..9fb0339
--- /dev/null
+++ b/src/BrokerFactory.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Keruald\Broker;
+
+class BrokerFactory {
+ /**
+ * Gets the map drivers <--> class
+ *
+ * @return array
+ */
+ public static function getDrivers () {
+ return require 'drivers.php';
+ }
+
+ /**
+ * Makes a new broker instance
+ *
+ * @param array $params The parameters
+ * @return Keruald\Broker\Broker
+ *
+ * @throws InvalidArgumentException when $params doesn't contain a valid
+ * driver entry
+ */
+ public static function make ($params) {
+ if (!array_key_exists('driver', $params)) {
+ throw new \InvalidArgumentException(
+ "Required parameter missing: driver"
+ );
+ }
+ $driver = $params['driver'];
+
+ $drivers = self::getDrivers();
+ if (!array_key_exists($driver, $drivers)) {
+ throw new \InvalidArgumentException(
+ "Broker driver not found: $driver"
+ );
+ }
+ $class = 'Keruald\Broker\\' . $drivers[$driver];
+
+ return $class::makeFromConfig($params);
+ }
+}
diff --git a/src/drivers.php b/src/drivers.php
new file mode 100644
index 0000000..efe76b0
--- /dev/null
+++ b/src/drivers.php
@@ -0,0 +1,6 @@
+<?php
+
+return [
+ 'amqp' => 'AMQPBroker',
+ 'blackhole' => 'BlackholeBroker',
+];
diff --git a/tests/BrokerFactoryTest.php b/tests/BrokerFactoryTest.php
new file mode 100644
index 0000000..52f51c2
--- /dev/null
+++ b/tests/BrokerFactoryTest.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Keruald\Broker\Tests;
+
+use Keruald\Broker\BrokerFactory;
+
+class BrokerFactoryTest extends TestCase {
+
+ public function testValidParameters () {
+ $broker = BrokerFactory::make([
+ 'driver' => 'blackhole'
+ ]);
+ $this->assertInstanceOf('Keruald\Broker\BlackholeBroker', $broker);
+
+ $broker = BrokerFactory::make([
+ 'driver' => 'amqp'
+ ]);
+ $this->assertInstanceOf('Keruald\Broker\AMQPBroker', $broker);
+ }
+
+ public function testOmnipotenceBlackhole () {
+ $broker = BrokerFactory::make([
+ 'driver' => 'blackhole',
+ 'omnipotence' => true,
+ ]);
+ $broker->spreadLove(); // a method not in Broker abstract class
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testEmptyParameters () {
+ BrokerFactory::make([]);
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testInvalidParameters () {
+ BrokerFactory::make([
+ 'foo' => 'bar'
+ ]);
+ }
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Oct 12, 10:01 (8 h, 25 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3065703
Default Alt Text
(9 KB)
Attached To
Mode
rKBROKER Keruald Broker
Attached
Detach File
Event Timeline
Log In to Comment