Skip to content

Commit

Permalink
Merge pull request #9 from joblocal/feature/refactor-command-building
Browse files Browse the repository at this point in the history
Feature/refactor command building
  • Loading branch information
jayhof authored Sep 25, 2018
2 parents 90fcb3b + 308251e commit 8513240
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"name": "Julius Liebert",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "Bastian Hofmann",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
Expand All @@ -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"
Expand Down
54 changes: 43 additions & 11 deletions src/Queue/Jobs/SqsSnsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ 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)
{
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);
Expand All @@ -33,7 +40,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);

Expand All @@ -51,22 +58,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(new $commandName(
$body['Subject'],
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.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/SqsSnsJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->container = $this->createMock(Container::class);
$this->container = new Container;
}

private function createSqsSnsJob($routes = [])
Expand Down
5 changes: 4 additions & 1 deletion tests/SqsSnsQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 8513240

Please sign in to comment.