-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessageFactory.php
More file actions
36 lines (27 loc) · 922 Bytes
/
MessageFactory.php
File metadata and controls
36 lines (27 loc) · 922 Bytes
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
<?php
declare(strict_types=1);
namespace RabbitEvents\Publisher;
use Illuminate\Support\Carbon;
use RabbitEvents\Foundation\Message;
use RabbitEvents\Foundation\Serialization\SerializerRegistry;
class MessageFactory
{
public function __construct(private SerializerRegistry $registry)
{
}
public function make(ShouldPublish $event): Message
{
$rawPayload = $event->toPublish();
$serializer = $this->registry->resolve($rawPayload);
$payload = $serializer->serialize($rawPayload);
$properties = [
'content_type' => (string) $payload->contentType(),
];
if (is_object($rawPayload)) {
$properties['type'] = get_class($rawPayload);
}
$message = new Message($event->publishEventKey(), $payload, $properties);
$message->setTimestamp(Carbon::now()->getTimestamp());
return $message;
}
}