Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Message/DelayEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function __construct(MessageInterface $message, float $delaySeconds)

public static function fromMessage(MessageInterface $message): static
{
$raw = $message->getMetadata()[self::META_DELAY_SECONDS] ?? null;
$raw = $message->getMeta()[self::META_DELAY_SECONDS] ?? null;
return new self($message, is_array($raw) ? 0.0 : (float) $raw);
}

public function getDelaySeconds(): float
{
return $this->metadata[self::META_DELAY_SECONDS];
return $this->meta[self::META_DELAY_SECONDS];
}
}
26 changes: 13 additions & 13 deletions src/Message/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
use LogicException;

/**
* @template TMetadata of MessageMetadata
* @template TMeta of MessageMeta
*
* @psalm-import-type MessageMetadata from MessageInterface
* @psalm-import-type MessageMeta from MessageInterface
*/
abstract class Envelope implements MessageInterface
{
/**
* @psalm-var TMetadata
* @psalm-var TMeta
*/
protected readonly array $metadata;
protected readonly array $meta;

private readonly MessageInterface $message;

/**
* @psalm-param TMetadata $metadata
* @psalm-param TMeta $meta
*/
public function __construct(MessageInterface $message, array $metadata)
public function __construct(MessageInterface $message, array $meta)
{
/** @var TMetadata */
$this->metadata = array_merge($message->getMetadata(), $metadata);
/** @var TMeta */
$this->meta = array_merge($message->getMeta(), $meta);

while ($message instanceof self) {
$message = $message->getMessage();
Expand Down Expand Up @@ -69,15 +69,15 @@ final public function getPayload(): bool|int|float|string|array|null
}

/**
* @psalm-return TMetadata
* @psalm-return TMeta
*/
final public function getMetadata(): array
final public function getMeta(): array
{
return $this->metadata;
return $this->meta;
}

final public function withMetadata(array $metadata): static
final public function withMeta(array $meta): static
{
return static::fromMessage($this->message->withMetadata($metadata));
return static::fromMessage($this->message->withMeta($meta));
}
}
4 changes: 2 additions & 2 deletions src/Message/IdEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(MessageInterface $message, string|int|null $id)

public static function fromMessage(MessageInterface $message): static
{
$rawId = $message->getMetadata()[self::META_ID] ?? null;
$rawId = $message->getMeta()[self::META_ID] ?? null;

$id = match (true) {
$rawId === null => null, // don't remove this branch: it's important for compute speed
Expand All @@ -47,6 +47,6 @@ public static function fromMessage(MessageInterface $message): static
*/
public function getId(): string|int|null
{
return $this->metadata[self::META_ID];
return $this->meta[self::META_ID];
}
}
10 changes: 5 additions & 5 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ abstract class Message implements MessageInterface
/**
* @psalm-var array<string, mixed>
*/
private array $metadata = [];
private array $meta = [];

final public function getMetadata(): array
final public function getMeta(): array
{
return $this->metadata;
return $this->meta;
}

final public function withMetadata(array $metadata): static
final public function withMeta(array $meta): static
{
$new = clone $this;
$new->metadata = $metadata;
$new->meta = $meta;
return $new;
}
}
12 changes: 6 additions & 6 deletions src/Message/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Represents a queue message with a type identifier, payload data, and metadata.
*
* @psalm-type MessagePayload = scalar|null|array<scalar|null|array>
* @psalm-type MessageMetadata = array<string, scalar|null|array<scalar|null|array>>
* @psalm-type MessageMeta = array<string, scalar|null|array<scalar|null|array>>
*/
interface MessageInterface
{
Expand Down Expand Up @@ -46,17 +46,17 @@ public function getPayload(): bool|int|float|string|array|null;
* @return array<string, bool|int|float|string|array|null> Metadata containing only `null`, scalars (`bool`, `int`,
* `float`, `string`), or arrays composed of the same types recursively.
*
* @psalm-return MessageMetadata
* @psalm-return MessageMeta
*/
public function getMetadata(): array;
public function getMeta(): array;

/**
* Returns a new instance with the given message metadata.
*
* @param array<string, bool|int|float|string|array|null> $metadata Metadata containing only `null`, scalars (`bool`,
* @param array<string, bool|int|float|string|array|null> $meta Metadata containing only `null`, scalars (`bool`,
* `int`, `float`, `string`), or arrays composed of the same types recursively.
*
* @psalm-param MessageMetadata $metadata
* @psalm-param MessageMeta $meta
*/
public function withMetadata(array $metadata): static;
public function withMeta(array $meta): static;
}
10 changes: 5 additions & 5 deletions src/Message/Serializer/MessageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function serialize(MessageInterface $message): string
return $this->encoder->encode([
'type' => $message->getType(),
'payload' => $message->getPayload(),
'meta' => $message->getMetadata(),
'meta' => $message->getMeta(),
]);
}

Expand All @@ -62,13 +62,13 @@ public function unserialize(string $value): MessageInterface
throw new MessageSerializerException('Message type must be a string. Got ' . get_debug_type($type) . '.');
}

$metadata = $data['meta'] ?? [];
if (!is_array($metadata)) {
throw new MessageSerializerException('Metadata must be an array. Got ' . get_debug_type($metadata) . '.');
$meta = $data['meta'] ?? [];
if (!is_array($meta)) {
throw new MessageSerializerException('Metadata must be an array. Got ' . get_debug_type($meta) . '.');
}

$class = $this->resolver->resolve($type) ?? GenericMessage::class;

return $class::fromPayload($type, $data['payload'] ?? null)->withMetadata($metadata);
return $class::fromPayload($type, $data['payload'] ?? null)->withMeta($meta);
}
}
32 changes: 16 additions & 16 deletions src/Middleware/FailureHandling/FailureEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,44 @@ final class FailureEnvelope extends Envelope
{
public const META_FAILURE = 'yii-failure';

private array $failureMetadata;
private array $failureMeta;

public function __construct(MessageInterface $message, array $failureMetadata = [])
public function __construct(MessageInterface $message, array $failureMeta = [])
{
$this->failureMetadata = $failureMetadata === []
? self::getFailureMetadataFromMessage($message)
$this->failureMeta = $failureMeta === []
? self::getFailureMetaFromMessage($message)
: ArrayHelper::merge(
self::getFailureMetadataFromMessage($message),
$failureMetadata,
self::getFailureMetaFromMessage($message),
$failureMeta,
);
parent::__construct($message, [
self::META_FAILURE => $this->failureMetadata,
self::META_FAILURE => $this->failureMeta,
]);
}

public function getFailureMetadata(): array
public function getFailureMeta(): array
{
return $this->failureMetadata;
return $this->failureMeta;
}

public function getFailureMetadataValue(string $key, mixed $default = null): mixed
public function getFailureMetaValue(string $key, mixed $default = null): mixed
{
return $this->failureMetadata[$key] ?? $default;
return $this->failureMeta[$key] ?? $default;
}

public static function fromMessage(MessageInterface $message): static
{
return new self(
$message,
self::getFailureMetadataFromMessage($message),
self::getFailureMetaFromMessage($message),
);
}

private static function getFailureMetadataFromMessage(MessageInterface $message): array
private static function getFailureMetaFromMessage(MessageInterface $message): array
{
$metadata = $message->getMetadata();
if (array_key_exists(self::META_FAILURE, $metadata)) {
$result = $metadata[self::META_FAILURE];
$meta = $message->getMeta();
if (array_key_exists(self::META_FAILURE, $meta)) {
$result = $meta[self::META_FAILURE];
return is_array($result) ? $result : [];
}
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ private function createNewMeta(MessageInterface $message): array
private function getAttempts(MessageInterface $message): int
{
return (int) FailureEnvelope::fromMessage($message)
->getFailureMetadataValue(self::META_KEY_ATTEMPTS . "-$this->id", 0);
->getFailureMetaValue(self::META_KEY_ATTEMPTS . "-$this->id", 0);
}

private function getDelay(MessageInterface $message): float
{
$delayOriginal = (float) FailureEnvelope::fromMessage($message)
->getFailureMetadataValue(self::META_KEY_DELAY . "-$this->id", 0);
->getFailureMetaValue(self::META_KEY_DELAY . "-$this->id", 0);

if ($delayOriginal <= 0) {
$delayOriginal = $this->delayInitial;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function createMeta(MessageInterface $message): array

private function getAttempts(MessageInterface $message): int
{
$result = FailureEnvelope::fromMessage($message)->getFailureMetadataValue($this->getMetaKey(), 0);
$result = FailureEnvelope::fromMessage($message)->getFailureMetaValue($this->getMetaKey(), 0);
if ($result < 0) {
$result = 0;
}
Expand Down
4 changes: 1 addition & 3 deletions stubs/DummyEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
/**
* Dummy envelope stub for testing purposes.
*
* @extends Envelope<MessageMetadata>
*
* @psalm-import-type MessageMetadata from MessageInterface
* @extends Envelope<array<never,never>>
*/
final class DummyEnvelope extends Envelope
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Middleware\FailureHandling\FailureEnvelope;

final class MetadataBench
final class MetaBench
{
/**
* Create metadata as an array and read its value from an array.
*/
public function benchArrayRead(): void
{
$message = (new GenericMessage('foo', 'bar'))->withMetadata(['id' => 1]);
$id = $message->getMetadata()['id'];
$message = (new GenericMessage('foo', 'bar'))->withMeta(['id' => 1]);
$id = $message->getMeta()['id'];
}

/**
Expand All @@ -36,7 +36,7 @@ public function benchEnvelopeRead(): void
*/
public function benchEnvelopeReadRestored(): void
{
$message = IdEnvelope::fromMessage((new GenericMessage('foo', 'bar'))->withMetadata(['id' => 1]));
$message = IdEnvelope::fromMessage((new GenericMessage('foo', 'bar'))->withMeta(['id' => 1]));
$id = $message->getId();
}

Expand Down Expand Up @@ -91,12 +91,12 @@ public function benchEnvelopeStackCreation(array $params): void
* @psalm-param array{0: int} $params
*/
#[ParamProviders('provideEnvelopeStackCounts')]
public function benchMetadataArrayCreation(array $params): void
public function benchMetaArrayCreation(array $params): void
{
$metadata = [FailureEnvelope::META_FAILURE => []];
$meta = [FailureEnvelope::META_FAILURE => []];
for ($i = 0; $i < $params[0]; $i++) {
$metadata[FailureEnvelope::META_FAILURE]["fail$i"] = "fail$i";
$meta[FailureEnvelope::META_FAILURE]["fail$i"] = "fail$i";
}
$message = (new GenericMessage('foo', 'bar'))->withMetadata($metadata);
$message = (new GenericMessage('foo', 'bar'))->withMeta($meta);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testEnvelopeStack(): void
$message = new IdEnvelope($message, 'test-id');

$this->assertEquals('test', $message->getMessage()->getPayload());
$this->assertEquals('test-id', $message->getMetadata()[IdEnvelope::META_ID]);
$this->assertEquals('test-id', $message->getMeta()[IdEnvelope::META_ID]);
}

public function testEnvelopeDuplicates(): void
Expand All @@ -27,6 +27,6 @@ public function testEnvelopeDuplicates(): void
$message = new IdEnvelope($message, 'test-id-3');

$this->assertEquals('test', $message->getMessage()->getPayload());
$this->assertEquals('test-id-3', $message->getMetadata()[IdEnvelope::META_ID]);
$this->assertEquals('test-id-3', $message->getMeta()[IdEnvelope::META_ID]);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Message/DelayEnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public function testDelayEnvelope(): void
self::assertSame(300.5, $delayEnvelope->getDelaySeconds());
self::assertSame(
[DelayEnvelope::META_DELAY_SECONDS => 300.5],
$delayEnvelope->getMetadata(),
$delayEnvelope->getMeta(),
);
}

public function testFromMessage(): void
{
$delayEnvelope = DelayEnvelope::fromMessage(
(new GenericMessage('test', ['data' => 'value']))
->withMetadata([DelayEnvelope::META_DELAY_SECONDS => 150]),
->withMeta([DelayEnvelope::META_DELAY_SECONDS => 150]),
);

self::assertSame(150.0, $delayEnvelope->getDelaySeconds());
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Message/IdEnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ public function testFromMessageWithInvalidIdType(): void
$this->assertNull($message->getId());
}

public function testGetEnvelopeMetadata(): void
public function testGetEnvelopeMeta(): void
{
$id = 'test-id';
$message = $this->createMessage();
$envelope = new IdEnvelope($message, $id);

$metadata = $envelope->getMetadata();
$meta = $envelope->getMeta();

$this->assertArrayHasKey(IdEnvelope::META_ID, $metadata);
$this->assertSame($id, $metadata[IdEnvelope::META_ID]);
$this->assertArrayHasKey(IdEnvelope::META_ID, $meta);
$this->assertSame($id, $meta[IdEnvelope::META_ID]);
}

private function createMessage(array $metadata = []): MessageInterface
private function createMessage(array $meta = []): MessageInterface
{
return (new GenericMessage('test-handler', ['test-data']))->withMetadata($metadata);
return (new GenericMessage('test-handler', ['test-data']))->withMeta($meta);
}
}
Loading
Loading