/
AmqpStamp.php
90 lines (76 loc) · 2.91 KB
/
AmqpStamp.php
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
84
85
86
87
88
89
90
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Bridge\Amqp\Transport;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
/**
* @author Guillaume Gammelin <ggammelin@gmail.com>
* @author Samuel Roze <samuel.roze@gmail.com>
*/
final class AmqpStamp implements NonSendableStampInterface
{
private ?string $routingKey;
private int $flags;
private array $attributes;
private bool $isRetryAttempt = false;
public function __construct(?string $routingKey = null, int $flags = \AMQP_NOPARAM, array $attributes = [])
{
$this->routingKey = $routingKey;
$this->flags = $flags;
$this->attributes = $attributes;
}
public function getRoutingKey(): ?string
{
return $this->routingKey;
}
public function getFlags(): int
{
return $this->flags;
}
public function getAttributes(): array
{
return $this->attributes;
}
public static function createFromAmqpEnvelope(\AMQPEnvelope $amqpEnvelope, ?self $previousStamp = null, ?string $retryRoutingKey = null): self
{
$attr = $previousStamp->attributes ?? [];
$attr['headers'] ??= $amqpEnvelope->getHeaders();
$attr['content_type'] ??= $amqpEnvelope->getContentType();
$attr['content_encoding'] ??= $amqpEnvelope->getContentEncoding();
$attr['delivery_mode'] ??= $amqpEnvelope->getDeliveryMode();
$attr['priority'] ??= $amqpEnvelope->getPriority();
$attr['timestamp'] ??= $amqpEnvelope->getTimestamp();
$attr['app_id'] ??= $amqpEnvelope->getAppId();
$attr['message_id'] ??= $amqpEnvelope->getMessageId();
$attr['user_id'] ??= $amqpEnvelope->getUserId();
$attr['expiration'] ??= $amqpEnvelope->getExpiration();
$attr['type'] ??= $amqpEnvelope->getType();
$attr['reply_to'] ??= $amqpEnvelope->getReplyTo();
$attr['correlation_id'] ??= $amqpEnvelope->getCorrelationId();
if (null === $retryRoutingKey) {
$stamp = new self($previousStamp->routingKey ?? $amqpEnvelope->getRoutingKey(), $previousStamp->flags ?? \AMQP_NOPARAM, $attr);
} else {
$stamp = new self($retryRoutingKey, $previousStamp->flags ?? \AMQP_NOPARAM, $attr);
$stamp->isRetryAttempt = true;
}
return $stamp;
}
public function isRetryAttempt(): bool
{
return $this->isRetryAttempt;
}
public static function createWithAttributes(array $attributes, ?self $previousStamp = null): self
{
return new self(
$previousStamp->routingKey ?? null,
$previousStamp->flags ?? \AMQP_NOPARAM,
array_merge($previousStamp->attributes ?? [], $attributes)
);
}
}