-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add envelope stack + MessageSerializer (#188)
- Loading branch information
Showing
13 changed files
with
320 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Message; | ||
|
||
use InvalidArgumentException; | ||
use JsonException; | ||
|
||
final class JsonMessageSerializer implements MessageSerializerInterface | ||
{ | ||
/** | ||
* @throws JsonException | ||
*/ | ||
public function serialize(MessageInterface $message): string | ||
{ | ||
$payload = [ | ||
'name' => $message->getHandlerName(), | ||
'data' => $message->getData(), | ||
'meta' => $message->getMetadata(), | ||
]; | ||
|
||
return json_encode($payload, JSON_THROW_ON_ERROR); | ||
} | ||
|
||
/** | ||
* @throws JsonException | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function unserialize(string $value): MessageInterface | ||
{ | ||
$payload = json_decode($value, true, 512, JSON_THROW_ON_ERROR); | ||
if (!is_array($payload)) { | ||
throw new InvalidArgumentException('Payload must be array. Got ' . get_debug_type($payload) . '.'); | ||
} | ||
|
||
$meta = $payload['meta'] ?? []; | ||
if (!is_array($meta)) { | ||
throw new InvalidArgumentException('Metadata must be array. Got ' . get_debug_type($meta) . '.'); | ||
} | ||
|
||
// TODO: will be removed later | ||
$message = new Message($payload['name'] ?? '$name', $payload['data'] ?? null, $meta); | ||
|
||
if (isset($meta[EnvelopeInterface::ENVELOPE_STACK_KEY]) && is_array($meta[EnvelopeInterface::ENVELOPE_STACK_KEY])) { | ||
$message = $message->withMetadata( | ||
array_merge($message->getMetadata(), [EnvelopeInterface::ENVELOPE_STACK_KEY => []]), | ||
); | ||
foreach ($meta[EnvelopeInterface::ENVELOPE_STACK_KEY] as $envelope) { | ||
if (is_string($envelope) && class_exists($envelope) && is_subclass_of($envelope, EnvelopeInterface::class)) { | ||
$message = $envelope::fromMessage($message); | ||
} | ||
} | ||
} | ||
|
||
|
||
return $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Message; | ||
|
||
interface MessageSerializerInterface | ||
{ | ||
public function serialize(MessageInterface $message): string; | ||
|
||
public function unserialize(string $value): MessageInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Queue\Message\EnvelopeInterface; | ||
use Yiisoft\Queue\Message\IdEnvelope; | ||
use Yiisoft\Queue\Message\Message; | ||
|
||
final class EnvelopeTest extends TestCase | ||
{ | ||
public function testEnvelopeStack(): void | ||
{ | ||
$message = new Message('handler', 'test'); | ||
$message = new IdEnvelope($message, 'test-id'); | ||
|
||
$this->assertEquals('test', $message->getMessage()->getData()); | ||
|
||
$stack = $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY]; | ||
$this->assertIsArray($stack); | ||
|
||
$this->assertEquals([ | ||
IdEnvelope::class, | ||
], $stack); | ||
} | ||
|
||
public function testEnvelopeDuplicates(): void | ||
{ | ||
$message = new Message('handler', 'test'); | ||
$message = new IdEnvelope($message, 'test-id'); | ||
$message = new IdEnvelope($message, 'test-id'); | ||
$message = new IdEnvelope($message, 'test-id'); | ||
|
||
$this->assertEquals('test', $message->getMessage()->getData()); | ||
|
||
$stack = $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY]; | ||
$this->assertIsArray($stack); | ||
|
||
$this->assertEquals([ | ||
IdEnvelope::class, | ||
IdEnvelope::class, | ||
IdEnvelope::class, | ||
], $stack); | ||
} | ||
} |
Oops, something went wrong.