Page MenuHomeDevCentral

D225.diff
No OneTemporary

D225.diff

diff --git a/src/AMQPBroker.php b/src/AMQPBroker.php
--- a/src/AMQPBroker.php
+++ b/src/AMQPBroker.php
@@ -44,6 +44,42 @@
$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
///
diff --git a/src/BlackholeBroker.php b/src/BlackholeBroker.php
--- a/src/BlackholeBroker.php
+++ b/src/BlackholeBroker.php
@@ -16,6 +16,22 @@
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
///
diff --git a/src/Broker.php b/src/Broker.php
--- a/src/Broker.php
+++ b/src/Broker.php
@@ -4,4 +4,6 @@
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
--- /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
--- /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
--- /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

Mime Type
text/plain
Expires
Fri, Dec 20, 13:50 (21 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2307017
Default Alt Text
D225.diff (4 KB)

Event Timeline