-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEnvelope.php
More file actions
83 lines (69 loc) · 2.07 KB
/
Copy pathEnvelope.php
File metadata and controls
83 lines (69 loc) · 2.07 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Message;
use LogicException;
/**
* @template TMeta of MessageMeta
*
* @psalm-import-type MessageMeta from MessageInterface
*/
abstract class Envelope implements MessageInterface
{
/**
* @psalm-var TMeta
*/
protected readonly array $meta;
private readonly MessageInterface $message;
/**
* @psalm-param TMeta $meta
*/
public function __construct(MessageInterface $message, array $meta)
{
/** @var TMeta */
$this->meta = array_merge($message->getMeta(), $meta);
while ($message instanceof self) {
$message = $message->getMessage();
}
$this->message = $message;
}
/**
* Envelopes cannot be created from a raw payload. Use {@see fromMessage()} to wrap an existing message instead.
*
* @throws LogicException Always, since this method is not supported for envelopes.
*/
final public static function fromPayload(string $type, mixed $payload): static
{
throw new LogicException(
'Envelopes cannot be created via "fromPayload()". Wrap an existing "MessageInterface" instance instead.',
);
}
/**
* Creates an envelope for the given message, restoring the envelope's own parameters from the message metadata.
*
* @param MessageInterface $message The message to create an envelope for.
*/
abstract public static function fromMessage(MessageInterface $message): static;
final public function getMessage(): MessageInterface
{
return $this->message;
}
final public function getType(): string
{
return $this->message->getType();
}
final public function getPayload(): bool|int|float|string|array|null
{
return $this->message->getPayload();
}
/**
* @psalm-return TMeta
*/
final public function getMeta(): array
{
return $this->meta;
}
final public function withMeta(array $meta): static
{
return static::fromMessage($this->message->withMeta($meta));
}
}