-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.php
More file actions
104 lines (88 loc) · 2.18 KB
/
Message.php
File metadata and controls
104 lines (88 loc) · 2.18 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* This file is part of the Vection package.
*
* (c) David M. Lung <vection@davidlung.de>
*
* 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;
/**
* Class Messenger
*
* @package Vection\Component\Messenger
*
* @author David Lung <vection@davidlung.de>
*/
class Message implements MessageInterface
{
protected MessageHeadersInterface $headers;
protected object $body;
/**
* @param object $body
* @param string[] $headers
*/
public function __construct(object $body, array $headers = [])
{
$this->body = $body;
if (!isset($headers[MessageHeaders::MESSAGE_ID])) {
$headers[MessageHeaders::MESSAGE_ID] = (new MessageIdGenerator())->generate();
}
$this->headers = new MessageHeaders($headers);
}
/**
* @return string
*/
public function getId(): string
{
return $this->headers->get(MessageHeaders::MESSAGE_ID);
}
/**
* @return MessageHeadersInterface
*/
public function getHeaders(): MessageHeadersInterface
{
return $this->headers;
}
/**
* @return object
*/
public function getBody(): object
{
return $this->body;
}
/**
* @inheritDoc
*/
public function getBodyType(): string
{
return str_replace('\\', '.', get_class($this->body));
}
/**
* @param string $name
* @param string $value
*
* @return static
*/
public function withHeader(string $name, string $value): MessageInterface
{
$message = clone $this;
$message->headers = new MessageHeaders(([$name => $value] + $this->headers->toArray()));
return $message;
}
/**
* @param object $body
*
* @return static
*/
public function withBody(object $body): MessageInterface
{
$message = clone $this;
$message->body = $body;
return $message;
}
}