-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.php
More file actions
88 lines (73 loc) · 2.13 KB
/
Message.php
File metadata and controls
88 lines (73 loc) · 2.13 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
84
85
86
87
88
<?php
declare(strict_types=1);
namespace RabbitEvents\Foundation;
use RabbitEvents\Foundation\Contracts\Serializer;
use RabbitEvents\Foundation\Contracts\Payload;
use RabbitEvents\Foundation\Serialization\JsonSerializer;
use RabbitEvents\Foundation\Contracts\TransportMessage;
/**
* @mixin TransportMessage
*/
class Message
{
/**
* @var TransportMessage|null
*/
private ?TransportMessage $transportMessage = null;
public function __construct(
public readonly string $event,
public readonly Payload $payload,
private array $properties = []
) {
}
/**
* @param TransportMessage $message
* @param Serializer|null $serializer
* @return static
*/
public static function createFromTransportMessage(TransportMessage $message, ?Serializer $serializer = null): static
{
$serializer = $serializer ?? new JsonSerializer();
return (new static(
$message->getProperty('event') ?: $message->getRoutingKey(),
$serializer->deserialize($message),
$message->getProperties()
))->setTransportMessage($message);
}
/**
* @return TransportMessage
*/
public function transportMessage(): TransportMessage
{
if (is_null($this->transportMessage)) {
$this->transportMessage = MessageFactory::make(
$this->event,
$this->payload,
$this->properties
);
}
return $this->transportMessage;
}
public function __call(string $method, ?array $args)
{
return $this->transportMessage()->$method(...$args);
}
public function attempts(): int
{
return $this->getProperty('x-attempts', 0);
}
public function increaseAttempts(): self
{
$this->setProperty('x-attempts', $this->attempts() + 1);
return $this;
}
/**
* @param TransportMessage $transportMessage
* @return Message
*/
public function setTransportMessage(TransportMessage $transportMessage): self
{
$this->transportMessage = $transportMessage;
return $this;
}
}