From c42023c8f1bfec328570c000f481865044a01131 Mon Sep 17 00:00:00 2001 From: Bastian Hofmann Date: Mon, 3 Sep 2018 15:07:01 +0200 Subject: [PATCH 1/3] Resolve job class from container --- src/Queue/Jobs/SqsSnsJob.php | 8 ++++---- tests/SqsSnsJobTest.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Queue/Jobs/SqsSnsJob.php b/src/Queue/Jobs/SqsSnsJob.php index 2d1b57a..f4349a3 100644 --- a/src/Queue/Jobs/SqsSnsJob.php +++ b/src/Queue/Jobs/SqsSnsJob.php @@ -56,10 +56,10 @@ private function resolveSnsSubscription(array $job, array $routes) 'job' => CallQueuedHandler::class . '@call', 'data' => [ 'commandName' => $commandName, - 'command' => serialize(new $commandName( - $body['Subject'], - json_decode($body['Message'], true) - )) + 'command' => serialize($this->container->make($commandName, [ + 'subject' => $body['Subject'], + 'payload' => json_decode($body['Message'], true) + ])) ], ]); } diff --git a/tests/SqsSnsJobTest.php b/tests/SqsSnsJobTest.php index 8bef652..82c4343 100644 --- a/tests/SqsSnsJobTest.php +++ b/tests/SqsSnsJobTest.php @@ -19,7 +19,7 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->container = $this->createMock(Container::class); + $this->container = new Container; } private function createSqsSnsJob($routes = []) From 0e7a12dfe5b53165a997aea42f159e209a75d4e2 Mon Sep 17 00:00:00 2001 From: Bastian Hofmann Date: Mon, 3 Sep 2018 16:37:33 +0200 Subject: [PATCH 2/3] refactoring --- composer.json | 9 ++++++-- src/Queue/Jobs/SqsSnsJob.php | 44 ++++++++++++++++++++++++++++-------- tests/SqsSnsQueueTest.php | 5 +++- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 4bdf779..18ffb80 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,11 @@ "name": "Julius Liebert", "email": "julius.liebert@joblocal.de", "role": "Developer" + }, + { + "name": "Bastian Hofmann", + "email": "bastian.hofmann@joblocal.de", + "role": "Developer" } ], "require": { @@ -39,10 +44,10 @@ }, "scripts": { "lint": [ - "./vendor/bin/phpcs --standard=phpcs.xml --colors -p ." + "phpcs --standard=phpcs.xml --colors -p ." ], "test": [ - "./vendor/bin/phpunit-randomizer -c phpunit.xml --order rand" + "phpunit-randomizer -c phpunit.xml --order rand" ] }, "minimum-stability": "stable" diff --git a/src/Queue/Jobs/SqsSnsJob.php b/src/Queue/Jobs/SqsSnsJob.php index f4349a3..5152570 100644 --- a/src/Queue/Jobs/SqsSnsJob.php +++ b/src/Queue/Jobs/SqsSnsJob.php @@ -18,6 +18,7 @@ class SqsSnsJob extends SqsJob * @param array $job * @param string $connectionName * @param array $routes + * @return void */ public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue, array $routes) { @@ -33,7 +34,7 @@ public function __construct(Container $container, SqsClient $sqs, array $job, $c * @param array $routes * @return array */ - private function resolveSnsSubscription(array $job, array $routes) + protected function resolveSnsSubscription(array $job, array $routes) { $body = json_decode($job['Body'], true); @@ -51,22 +52,47 @@ private function resolveSnsSubscription(array $job, array $routes) } if ($commandName !== null) { - // restructure job body + // If there is a command available, we will resolve the job instance for it from + // the service container, passing in the subject and the payload of the + // notification. + + $command = $this->makeCommand($commandName, $body); + + // The instance for the job will then be serialized and the body of + // the job is reconstructed. + $job['Body'] = json_encode([ 'job' => CallQueuedHandler::class . '@call', - 'data' => [ - 'commandName' => $commandName, - 'command' => serialize($this->container->make($commandName, [ - 'subject' => $body['Subject'], - 'payload' => json_decode($body['Message'], true) - ])) - ], + 'data' => compact('commandName', 'command'), ]); } return $job; } + /** + * Make the serialized command. + * + * @param string $commandName + * @param array $body + * @return string + */ + protected function makeCommand($commandName, $body) + { + $payload = json_decode($body['Message'], true); + + $data = [ + 'subject' => $body['Subject'], + 'payload' => $payload + ]; + + $instance = $this->container->make($commandName, $data); + + return serialize($instance); + } + + + /** * Get the underlying raw SQS job. * diff --git a/tests/SqsSnsQueueTest.php b/tests/SqsSnsQueueTest.php index ecf6105..f2bf969 100644 --- a/tests/SqsSnsQueueTest.php +++ b/tests/SqsSnsQueueTest.php @@ -46,7 +46,10 @@ public function testWillSetRoutes() public function testWillCallReceiveMessage() { $this->sqsClient->expects($this->once()) - ->method('receiveMessage'); + ->method('receiveMessage') + ->willReturn([ + 'Messages' => [], + ]); $queue = new SqsSnsQueue($this->sqsClient, 'default_queue'); $queue->setContainer($this->createMock(Container::class)); From bbc81215fe42c3520fa0301f50df739aa3068ab1 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Tue, 25 Sep 2018 08:31:52 +0200 Subject: [PATCH 3/3] fixes linter warning, line too long --- src/Queue/Jobs/SqsSnsJob.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Queue/Jobs/SqsSnsJob.php b/src/Queue/Jobs/SqsSnsJob.php index 5152570..bd83e43 100644 --- a/src/Queue/Jobs/SqsSnsJob.php +++ b/src/Queue/Jobs/SqsSnsJob.php @@ -20,8 +20,14 @@ class SqsSnsJob extends SqsJob * @param array $routes * @return void */ - public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue, array $routes) - { + public function __construct( + Container $container, + SqsClient $sqs, + array $job, + $connectionName, + $queue, + array $routes + ) { parent::__construct($container, $sqs, $job, $connectionName, $queue); $this->job = $this->resolveSnsSubscription($this->job, $routes);