From d72671f3c64c4b6bf71b0fc7a423b75defc55370 Mon Sep 17 00:00:00 2001 From: Ben Schwartz Date: Thu, 18 Jun 2020 00:35:24 -0500 Subject: [PATCH 1/4] adding factory and fluent assimilation --- .../Fliglio/Borg/AmqpCollectiveFactory.php | 30 +++++++++++++++++++ src/main/Fliglio/Borg/Collective.php | 1 + 2 files changed, 31 insertions(+) create mode 100644 src/main/Fliglio/Borg/AmqpCollectiveFactory.php diff --git a/src/main/Fliglio/Borg/AmqpCollectiveFactory.php b/src/main/Fliglio/Borg/AmqpCollectiveFactory.php new file mode 100644 index 0000000..529a20c --- /dev/null +++ b/src/main/Fliglio/Borg/AmqpCollectiveFactory.php @@ -0,0 +1,30 @@ +rabbitConnection = $rabbitConnection; + $this->routingNamespace = $routingNamespace; + } + + public function create($instancesToAssimilate = []) { + $driver = new AmqpCollectiveDriver($this->rabbitConnection); + $mapper = new DefaultMapper($driver); + $routing = new RoutingConfiguration($this->routingNamespace); + + $coll = new Collective($driver, $mapper, $routing); + foreach ($instancesToAssimilate as $inst) { + $coll->assimilate($inst); + } + return $coll; + } +} + diff --git a/src/main/Fliglio/Borg/Collective.php b/src/main/Fliglio/Borg/Collective.php index 7683b5f..b7ddc9c 100644 --- a/src/main/Fliglio/Borg/Collective.php +++ b/src/main/Fliglio/Borg/Collective.php @@ -42,6 +42,7 @@ public function getMasterRoutingKey() { public function assimilate($i) { $i->setCollective($this); $this->drones[get_class($i)] = $i; + return $this; } public function mkchan($type = null) { From 5da3030de541991907492f135a010db419f00130 Mon Sep 17 00:00:00 2001 From: Ben Schwartz Date: Thu, 18 Jun 2020 00:48:08 -0500 Subject: [PATCH 2/4] adding typehint --- src/main/Fliglio/Borg/Amqp/AmqpCollectiveDriver.php | 7 ++----- src/main/Fliglio/Borg/AmqpCollectiveFactory.php | 5 ++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/Fliglio/Borg/Amqp/AmqpCollectiveDriver.php b/src/main/Fliglio/Borg/Amqp/AmqpCollectiveDriver.php index 0d2b75c..adbbf6d 100644 --- a/src/main/Fliglio/Borg/Amqp/AmqpCollectiveDriver.php +++ b/src/main/Fliglio/Borg/Amqp/AmqpCollectiveDriver.php @@ -2,13 +2,10 @@ namespace Fliglio\Borg\Amqp; -use PhpAmqpLib\Connection\AMQPStreamConnection; -use PhpAmqpLib\Message\AMQPMessage; - use Fliglio\Borg\Driver\CollectiveDriver; -use Fliglio\Borg\Chan; - use Fliglio\Http\RequestReader; +use PhpAmqpLib\Connection\AMQPStreamConnection; +use PhpAmqpLib\Message\AMQPMessage; class AmqpCollectiveDriver implements CollectiveDriver { diff --git a/src/main/Fliglio/Borg/AmqpCollectiveFactory.php b/src/main/Fliglio/Borg/AmqpCollectiveFactory.php index 529a20c..7c94377 100644 --- a/src/main/Fliglio/Borg/AmqpCollectiveFactory.php +++ b/src/main/Fliglio/Borg/AmqpCollectiveFactory.php @@ -6,11 +6,14 @@ use Fliglio\Borg\Amqp\AmqpCollectiveDriver; use Fliglio\Borg\Mapper\DefaultMapper; +use PhpAmqpLib\Connection\AMQPStreamConnection; class AmqpCollectiveFactory { + private $rabbitConnection; private $routingNamespace; - public function __construct($rabbitConnection, $routingNamespace) { + + public function __construct(AMQPStreamConnection $rabbitConnection, $routingNamespace) { $this->rabbitConnection = $rabbitConnection; $this->routingNamespace = $routingNamespace; } From 0fcac0178421beb4301f4a71e17e7aa9d3134823 Mon Sep 17 00:00:00 2001 From: Ben Schwartz Date: Thu, 18 Jun 2020 00:56:37 -0500 Subject: [PATCH 3/4] removing assimilate shortcut in factory --- src/main/Fliglio/Borg/AmqpCollectiveFactory.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/Fliglio/Borg/AmqpCollectiveFactory.php b/src/main/Fliglio/Borg/AmqpCollectiveFactory.php index 7c94377..838ca46 100644 --- a/src/main/Fliglio/Borg/AmqpCollectiveFactory.php +++ b/src/main/Fliglio/Borg/AmqpCollectiveFactory.php @@ -18,16 +18,12 @@ public function __construct(AMQPStreamConnection $rabbitConnection, $routingName $this->routingNamespace = $routingNamespace; } - public function create($instancesToAssimilate = []) { + public function create() { $driver = new AmqpCollectiveDriver($this->rabbitConnection); $mapper = new DefaultMapper($driver); $routing = new RoutingConfiguration($this->routingNamespace); - $coll = new Collective($driver, $mapper, $routing); - foreach ($instancesToAssimilate as $inst) { - $coll->assimilate($inst); - } - return $coll; + return new Collective($driver, $mapper, $routing); } } From 0e8aa0c25acac9ce63b615cf4343624214ae0d4c Mon Sep 17 00:00:00 2001 From: Ben Schwartz Date: Thu, 18 Jun 2020 09:01:07 -0500 Subject: [PATCH 4/4] updating test to use factory --- src/test/Fliglio/Borg/TestConfiguration.php | 16 +++++++--------- src/test/component/ComponentTest.php | 1 + 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/test/Fliglio/Borg/TestConfiguration.php b/src/test/Fliglio/Borg/TestConfiguration.php index 24bea4b..b17ece1 100644 --- a/src/test/Fliglio/Borg/TestConfiguration.php +++ b/src/test/Fliglio/Borg/TestConfiguration.php @@ -18,18 +18,16 @@ class TestConfiguration extends DefaultConfiguration { public function getRoutes() { $rConn = new AMQPStreamConnection('localhost', 5672, "guest", "guest", "/"); - $driver = new AmqpCollectiveDriver($rConn); - + $resource = new TestResource(); $fun = new FunResource; $shake = new ShakespeareResource; - - $mapper = new DefaultMapper($driver); - $routing = new RoutingConfiguration("borg-demo"); - $coll = new Collective($driver, $mapper, $routing); - $coll->assimilate($resource); - $coll->assimilate($fun); - $coll->assimilate($shake); + + $coll = (new AmqpCollectiveFactory($rConn, "borg-demo")) + ->create() + ->assimilate($resource) + ->assimilate($fun) + ->assimilate($shake); diff --git a/src/test/component/ComponentTest.php b/src/test/component/ComponentTest.php index d9b0aed..1fd3a5b 100644 --- a/src/test/component/ComponentTest.php +++ b/src/test/component/ComponentTest.php @@ -12,6 +12,7 @@ public function setup() { $this->add = sprintf("http://%s:%s", getenv('LOCALDEV_PORT_80_TCP_ADDR'), 80); } + public function testRoundTrip() { // given $expected = 'hello world';