|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Queue\Message; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractEnvelope implements EnvelopeInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected array $metadata = []; |
|
12
|
|
|
private MessageInterface $message; |
|
13
|
|
|
|
|
14
|
25 |
|
public function __construct(MessageInterface $message) |
|
15
|
|
|
{ |
|
16
|
25 |
|
$this->metadata = $message->getMetadata(); |
|
17
|
25 |
|
$envelopes = [static::class]; |
|
18
|
25 |
|
while ($message instanceof EnvelopeInterface) { |
|
19
|
2 |
|
if ($message::class !== static::class) { |
|
20
|
|
|
$envelopes = [$message::class]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
2 |
|
$message = $message->getMessage(); |
|
24
|
|
|
} |
|
25
|
25 |
|
$this->message = $message; |
|
26
|
|
|
|
|
27
|
25 |
|
if (isset($this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY]) && is_array($this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY])) { |
|
28
|
4 |
|
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = array_merge( |
|
29
|
4 |
|
$envelopes, |
|
30
|
4 |
|
array_filter( |
|
31
|
4 |
|
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY], |
|
32
|
4 |
|
static fn (string $envelope): bool => !in_array($envelope, $envelopes), |
|
33
|
4 |
|
), |
|
34
|
4 |
|
); |
|
35
|
|
|
} else { |
|
36
|
24 |
|
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = [static::class]; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function getMessage(): MessageInterface |
|
41
|
|
|
{ |
|
42
|
5 |
|
return $this->message; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
public function getHandlerName(): string |
|
46
|
|
|
{ |
|
47
|
8 |
|
return $this->message->getHandlerName(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
public function getData(): mixed |
|
51
|
|
|
{ |
|
52
|
3 |
|
return $this->message->getData(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
24 |
|
public function getMetadata(): array |
|
56
|
|
|
{ |
|
57
|
24 |
|
return ArrayHelper::merge( |
|
58
|
24 |
|
$this->metadata, |
|
59
|
24 |
|
$this->getEnvelopeMetadata(), |
|
60
|
24 |
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Metadata of the envelope |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract protected function getEnvelopeMetadata(): array; |
|
69
|
|
|
} |
|
70
|
|
|
|