Skip to content

Commit

Permalink
consumable message can decode messages into array & object (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi-anik authored May 15, 2022
1 parent 30916f1 commit eea4f44
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,22 @@ $msg = new ConsumableMessage(function (ConsumableMessage $message/*, AMQPMessage
echo $message->getRoutingKey() . PHP_EOL;
$message->ack();
// Alternatively, $original->ack();

/**
* Method: `decodeMessage`
* Returns:
* - `array` if message body contains valid json
* - `null` if json could not be decoded
*/
var_dump($message->decodeMessage());

/**
* Method: `decodeMessageAsObject`
* Returns:
* - `\stdClass` if message body contains valid json
* - `null` if json could not be decoded
*/
var_dump($message->decodeMessageAsObject());
});
```

Expand Down
22 changes: 22 additions & 0 deletions src/ConsumableMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Anik\Amqp\Exceptions\AmqpException;
use PhpAmqpLib\Message\AMQPMessage;
use stdClass;

class ConsumableMessage implements Consumable
{
Expand Down Expand Up @@ -59,6 +60,27 @@ public function getRoutingKey(): string
return $this->message->getRoutingKey();
}

protected function jsonDecodeMessage(bool $associative = false, int $depth = 512)
{
$data = json_decode($this->getMessageBody(), $associative, $depth);

if (json_last_error() !== JSON_ERROR_NONE) {
return null;
}

return $data;
}

public function decodeMessage(int $depth = 512): ?array
{
return $this->jsonDecodeMessage(true, $depth);
}

public function decodeMessageAsObject(int $depth = 512): ?stdClass
{
return $this->jsonDecodeMessage(false, $depth);
}

public function handle(): void
{
$this->ensureThatMessageIsSet();
Expand Down
40 changes: 40 additions & 0 deletions tests/Integration/ConsumableMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Anik\Amqp\ConsumableMessage;
use Anik\Amqp\Exceptions\AmqpException;
use PhpAmqpLib\Message\AMQPMessage;
use PHPUnit\Framework\Assert;

class ConsumableMessageTest extends AmqpTestCase
{
Expand Down Expand Up @@ -257,4 +258,43 @@ function () {
}
))->$method();
}

public function decodeMessageDataProvider(): array
{
return [
'not a json message' => [
[
'message' => 'this is a simple text message',
'expectation' => [],
],
],
'valid json message' => [
[
'message' => '{"package":"amqp"}',
'expectation' => ['key' => 'package', 'value' => 'amqp'],
],
],
];
}

/**
* @dataProvider decodeMessageDataProvider
*/
public function testMessageDecoding(array $data)
{
$amqpMessage = new AMQPMessage($data['message']);
$expectation = $data['expectation'];

(new ConsumableMessage(
function (ConsumableMessage $message) use ($expectation) {
if (empty($expectation)) {
Assert::assertNull($message->decodeMessage());
Assert::assertNull($message->decodeMessageAsObject());
} else {
Assert::assertSame($expectation['value'], $message->decodeMessage()[$expectation['key']]);
Assert::assertSame($expectation['value'], $message->decodeMessageAsObject()->{$expectation['key']});
}
}
))->setMessage($amqpMessage)->handle();
}
}

0 comments on commit eea4f44

Please sign in to comment.