-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathIdEnvelope.php
More file actions
57 lines (45 loc) · 1.34 KB
/
IdEnvelope.php
File metadata and controls
57 lines (45 loc) · 1.34 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
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Message;
/**
* ID envelope allows to identify a message.
*/
final class IdEnvelope implements EnvelopeInterface
{
use EnvelopeTrait;
public const MESSAGE_ID_KEY = 'yii-message-id';
public function __construct(
private MessageInterface $message,
private string|int|null $id = null,
) {
}
public static function fromMessage(MessageInterface $message): self
{
return new self($message, self::getIdFromMessage($message));
}
public function setId(string|int|null $id): void
{
$this->id = $id;
}
public function getId(): string|int|null
{
return $this->id ?? self::getIdFromMessage($this->message);
}
private function getEnvelopeMetadata(): array
{
return [self::MESSAGE_ID_KEY => $this->getId()];
}
private static function getIdFromMessage(MessageInterface $message): string|int|null
{
$id = $message->getMetadata()[self::MESSAGE_ID_KEY] ?? null;
if ($id instanceof \Stringable) {
$id = (string) $id;
}
// We don't throw an error as this value could come from external sources,
// and we should process the message either way
if (!is_string($id) && !is_int($id)) {
return null;
}
return $id;
}
}