-
-
Notifications
You must be signed in to change notification settings - Fork 28
Convert envelope trait to abstract class #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8700123
3ecc37e
bff438d
e4e4723
95b6ea4
666d650
c6b758d
7516793
198ea47
d0deac6
fefb5cb
1cdcfe1
3ec891f
b63f5e7
f8c47ba
4370fa0
f3f388b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Message; | ||
|
||
abstract class Envelope implements EnvelopeInterface | ||
{ | ||
private ?array $metadata = null; | ||
|
||
public function __construct(protected MessageInterface $message) | ||
{ | ||
} | ||
|
||
/** @psalm-suppress MoreSpecificReturnType */ | ||
public static function fromData(string $handlerName, mixed $data, array $metadata = []): static | ||
{ | ||
/** @psalm-suppress LessSpecificReturnStatement */ | ||
return static::fromMessage(Message::fromData($handlerName, $data, $metadata)); | ||
} | ||
|
||
public function getMessage(): MessageInterface | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getHandlerName(): string | ||
{ | ||
return $this->message->getHandlerName(); | ||
} | ||
|
||
public function getData(): mixed | ||
{ | ||
return $this->message->getData(); | ||
} | ||
|
||
public function getMetadata(): array | ||
{ | ||
if ($this->metadata === null) { | ||
$this->metadata = array_merge( | ||
$this->message->getMetadata(), | ||
[ | ||
EnvelopeInterface::ENVELOPE_STACK_KEY => array_merge( | ||
$this->message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY] ?? [], | ||
[static::class], | ||
), | ||
], | ||
$this->getEnvelopeMetadata(), | ||
); | ||
} | ||
|
||
return $this->metadata; | ||
} | ||
|
||
abstract protected function getEnvelopeMetadata(): array; | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,34 +7,40 @@ | |
/** | ||
* ID envelope allows to identify a message. | ||
*/ | ||
final class IdEnvelope implements EnvelopeInterface | ||
final class IdEnvelope extends Envelope | ||
{ | ||
use EnvelopeTrait; | ||
|
||
public const MESSAGE_ID_KEY = 'yii-message-id'; | ||
|
||
public function __construct( | ||
private MessageInterface $message, | ||
private string|int|null $id = null, | ||
MessageInterface $message, | ||
private readonly string|int|null $id, | ||
) { | ||
parent::__construct($message); | ||
} | ||
|
||
public static function fromMessage(MessageInterface $message): self | ||
{ | ||
return new self($message, $message->getMetadata()[self::MESSAGE_ID_KEY] ?? null); | ||
} | ||
|
||
public function setId(string|int|null $id): void | ||
public static function fromMessage(MessageInterface $message): static | ||
{ | ||
$this->id = $id; | ||
/** @var mixed $rawId */ | ||
$rawId = $message->getMetadata()[self::MESSAGE_ID_KEY] ?? null; | ||
|
||
/** @var int|string|null $id */ | ||
$id = match (true) { | ||
$rawId === null => null, | ||
is_string($rawId) => $rawId, | ||
is_int($rawId) => $rawId, | ||
is_object($rawId) && method_exists($rawId, '__toString') => (string)$rawId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check for Stringable instead? |
||
default => throw new \InvalidArgumentException(sprintf('Message ID must be string|int|null, %s given.', get_debug_type($rawId))), | ||
}; | ||
|
||
return new self($message, $id); | ||
} | ||
|
||
public function getId(): string|int|null | ||
{ | ||
return $this->id ?? $this->message->getMetadata()[self::MESSAGE_ID_KEY] ?? null; | ||
return $this->id; | ||
} | ||
|
||
private function getEnvelopeMetadata(): array | ||
protected function getEnvelopeMetadata(): array | ||
{ | ||
return [self::MESSAGE_ID_KEY => $this->getId()]; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,9 @@ | |
final class Message implements MessageInterface | ||
{ | ||
/** | ||
* @param string $handlerName A name of a handler which should handle this message. | ||
* @param mixed $data Message data, encodable by a queue adapter | ||
* @param array $metadata Message metadata, encodable by a queue adapter | ||
* @param string|null $id Message id | ||
*/ | ||
public function __construct( | ||
private string $handlerName, | ||
|
@@ -37,12 +37,4 @@ public function getMetadata(): array | |
{ | ||
return $this->metadata; | ||
} | ||
|
||
public function withMetadata(array $metadata): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any real-world use cases. Do you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Message handlers may add metadata into the message |
||
{ | ||
$instance = clone $this; | ||
$instance->metadata = $metadata; | ||
|
||
return $instance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,16 +5,10 @@ | |
namespace Yiisoft\Queue\Tests\Unit\Message; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Queue\Message\Message; | ||
use Yiisoft\Queue\Tests\App\DummyEnvelope; | ||
|
||
final class EnvelopeTraitTest extends TestCase | ||
{ | ||
private function createTestEnvelope(): DummyEnvelope | ||
{ | ||
return new DummyEnvelope(); | ||
} | ||
|
||
public function testFromData(): void | ||
{ | ||
$handlerName = 'test-handler'; | ||
|
@@ -29,21 +23,4 @@ public function testFromData(): void | |
$this->assertArrayHasKey('meta', $envelope->getMetadata()); | ||
$this->assertSame('data', $envelope->getMetadata()['meta']); | ||
} | ||
|
||
public function testWithMessage(): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd keep it |
||
{ | ||
$originalMessage = new Message('original-handler', 'original-data'); | ||
$newMessage = new Message('new-handler', 'new-data'); | ||
|
||
$envelope = $this->createTestEnvelope(); | ||
$envelope = $envelope->withMessage($originalMessage); | ||
|
||
$this->assertSame($originalMessage, $envelope->getMessage()); | ||
|
||
$newEnvelope = $envelope->withMessage($newMessage); | ||
|
||
$this->assertNotSame($envelope, $newEnvelope); | ||
$this->assertSame($newMessage, $newEnvelope->getMessage()); | ||
$this->assertSame($originalMessage, $envelope->getMessage()); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.