-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageRelation.php
85 lines (72 loc) · 2.24 KB
/
MessageRelation.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
<?php
/**
* This file is part of the Vection-Framework project.
* Visit project at https://github.com/Vection-Framework/Vection
*
* (c) Vection-Framework <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Vection\Component\Messenger;
use Vection\Contracts\Messenger\MessageHeadersInterface;
use Vection\Contracts\Messenger\MessageInterface;
use Vection\Contracts\Messenger\MessageRelationInterface;
/**
* Class MessageRelation
*
* @package Vection\Component\Messenger
*
* @author David Lung <[email protected]>
*/
class MessageRelation implements MessageRelationInterface
{
protected string|null $correlationId = null;
protected string|null $causationId = null;
protected MessageInterface|null $message = null;
/**
* @inheritDoc
*/
public function inCorrelation(string $id): MessageRelationInterface
{
$this->correlationId = $id;
return $this;
}
/**
* @inheritDoc
*/
public function causedBy(string $id): MessageRelationInterface
{
$this->causationId = $id;
return $this;
}
/**
* @inheritDoc
*/
public function with(MessageInterface $message): MessageRelationInterface
{
$this->message = $message;
return $this;
}
/**
* @inheritDoc
*/
public function getHeaders(): MessageHeadersInterface
{
$headers = [
MessageHeaders::CORRELATION_ID => $this->correlationId,
MessageHeaders::CAUSATION_ID => $this->causationId
];
if ( $this->message instanceof MessageInterface ) {
$messageHeaders = $this->message->getHeaders();
if ($messageHeaders->has(MessageHeaders::CORRELATION_ID)) {
$headers[MessageHeaders::CORRELATION_ID] = $messageHeaders->get(MessageHeaders::CORRELATION_ID);
}
if ($messageHeaders->has(MessageHeaders::MESSAGE_ID)) {
$headers[MessageHeaders::CAUSATION_ID] = $messageHeaders->get(MessageHeaders::MESSAGE_ID);
}
}
return new MessageHeaders(array_filter($headers));
}
}