-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathIdEnvelope.php
More file actions
46 lines (37 loc) · 1.14 KB
/
Copy pathIdEnvelope.php
File metadata and controls
46 lines (37 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Message;
/**
* ID envelope allows to identify a message.
*/
final class IdEnvelope extends Envelope
{
public const MESSAGE_ID_KEY = 'yii-message-id';
public function __construct(
protected MessageInterface $message,
private readonly string|int|null $id,
) {
}
public static function fromMessage(MessageInterface $message): static
{
/** @var mixed $rawId */
$rawId = $message->getMetadata()[self::MESSAGE_ID_KEY] ?? null;
/** @var int|string|null $id */
$id = match (true) {
$rawId === null => null, // don't remove this branch: it's important for compute speed
is_string($rawId) => $rawId,
is_int($rawId) => $rawId,
is_object($rawId) && method_exists($rawId, '__toString') => (string)$rawId,
default => null,
};
return new self($message, $id);
}
public function getId(): string|int|null
{
return $this->id;
}
protected function getEnvelopeMetadata(): array
{
return [self::MESSAGE_ID_KEY => $this->getId()];
}
}