|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Queue\Message; |
|
6
|
|
|
|
|
7
|
|
|
trait EnvelopeTrait |
|
8
|
|
|
{ |
|
9
|
|
|
private MessageInterface $message; |
|
10
|
|
|
|
|
11
|
4 |
|
public function getMessage(): MessageInterface |
|
12
|
|
|
{ |
|
13
|
4 |
|
return $this->message; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function withMessage(MessageInterface $message): self |
|
17
|
|
|
{ |
|
18
|
|
|
$instance = clone $this; |
|
19
|
|
|
$instance->message = $message; |
|
20
|
|
|
|
|
21
|
|
|
return $instance; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
8 |
|
public function getHandlerName(): string |
|
25
|
|
|
{ |
|
26
|
8 |
|
return $this->message->getHandlerName(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
public function getData(): mixed |
|
30
|
|
|
{ |
|
31
|
4 |
|
return $this->message->getData(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
12 |
|
public function getMetadata(): array |
|
35
|
|
|
{ |
|
36
|
12 |
|
return array_merge( |
|
37
|
12 |
|
$this->message->getMetadata(), |
|
38
|
12 |
|
[ |
|
39
|
12 |
|
self::ENVELOPE_STACK_KEY => array_merge( |
|
|
|
|
|
|
40
|
12 |
|
$this->message->getMetadata()[self::ENVELOPE_STACK_KEY] ?? [], |
|
41
|
12 |
|
[self::class], |
|
42
|
12 |
|
), |
|
43
|
12 |
|
], |
|
44
|
12 |
|
$this->getEnvelopeMetadata(), |
|
45
|
12 |
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Finds an envelope in the current envelope stack or creates a new one from the message. |
|
50
|
|
|
* |
|
51
|
|
|
* @template T of EnvelopeInterface |
|
52
|
|
|
* |
|
53
|
|
|
* @psalm-param class-string<T> $className |
|
54
|
|
|
* @throws NotEnvelopInterfaceException is thrown if the given class does not implement {@see EnvelopeInterface}. |
|
55
|
|
|
* |
|
56
|
|
|
* @psalm-return T |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function getEnvelopeFromStack(string $className): EnvelopeInterface |
|
59
|
|
|
{ |
|
60
|
4 |
|
if (!is_a($className, EnvelopeInterface::class, true)) { |
|
61
|
1 |
|
throw new NotEnvelopInterfaceException($className); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
if (static::class === $className) { |
|
65
|
2 |
|
return $this; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
if ($this->message instanceof EnvelopeInterface) { |
|
69
|
2 |
|
return $this->message->getEnvelopeFromStack($className); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return $className::fromMessage($this->message); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public static function getEnvelopeFromMessage(MessageInterface $message): EnvelopeInterface |
|
76
|
|
|
{ |
|
77
|
2 |
|
if ($message instanceof EnvelopeInterface) { |
|
78
|
1 |
|
return $message->getEnvelopeFromStack(static::class); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return static::fromMessage($message); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getEnvelopeMetadata(): array |
|
85
|
|
|
{ |
|
86
|
|
|
return []; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|