1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Application; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Exceptions\InvalidQoSLevel; |
8
|
|
|
use unreal4u\MQTT\Exceptions\MessageTooBig; |
9
|
|
|
use unreal4u\MQTT\Exceptions\MissingTopicName; |
10
|
|
|
use unreal4u\MQTT\Internals\ProtocolBase; |
11
|
|
|
|
12
|
|
|
final class Message extends ProtocolBase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* This field indicates the level of assurance for delivery of an Application Message. Can be 0, 1 or 2 |
16
|
|
|
* |
17
|
|
|
* 0: At most once delivery (default) |
18
|
|
|
* 1: At least once delivery |
19
|
|
|
* 2: Exactly once delivery |
20
|
|
|
* |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $qosLevel = 0; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PayloadInterface |
27
|
|
|
*/ |
28
|
|
|
private $payload; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* If the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a Server, the Server MUST store the |
32
|
|
|
* Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its |
33
|
|
|
* topic name |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $isRetained = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Topic Name identifies the information channel to which payload data is published |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $topicName = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Will perform validation on the message before sending it to the MQTT broker |
46
|
|
|
* |
47
|
|
|
* @return Message |
48
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MissingTopicName |
49
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MessageTooBig |
50
|
|
|
*/ |
51
|
8 |
|
public function validateMessage(): Message |
52
|
|
|
{ |
53
|
8 |
|
if ($this->topicName === '') { |
54
|
1 |
|
throw new MissingTopicName('Topic can\'t be empty, please provide one'); |
55
|
|
|
} |
56
|
|
|
|
57
|
7 |
|
$processedPayload = $this->payload->getProcessedPayload(); |
58
|
7 |
|
if (mb_strlen($processedPayload) > 65535) { |
59
|
1 |
|
throw new MessageTooBig('Message payload can not exceed 65535 bytes!'); |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sets the actual payload to be sent to the message broker |
67
|
|
|
* |
68
|
|
|
* @param PayloadInterface $payload |
69
|
|
|
* @return Message |
70
|
|
|
*/ |
71
|
14 |
|
public function setPayload(PayloadInterface $payload): Message |
72
|
|
|
{ |
73
|
14 |
|
$this->payload = $payload; |
74
|
14 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
public function getPayload(): string |
78
|
|
|
{ |
79
|
6 |
|
return $this->payload->getProcessedPayload(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the QoS level to the indicated value. Must be 0, 1 or 2. |
84
|
|
|
* |
85
|
|
|
* @param int $level |
86
|
|
|
* @return Message |
87
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
88
|
|
|
*/ |
89
|
10 |
|
public function setQoSLevel(int $level): Message |
90
|
|
|
{ |
91
|
10 |
|
if ($level > 2 || $level < 0) { |
92
|
1 |
|
throw new InvalidQoSLevel('The QoS level must be 0, 1 or 2'); |
93
|
|
|
} |
94
|
|
|
|
95
|
9 |
|
$this->qosLevel = $level; |
96
|
9 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the retain flag to the given value |
101
|
|
|
* |
102
|
|
|
* @param bool $flag Set to true if message should be retained, false otherwise (default) |
103
|
|
|
* @return Message |
104
|
|
|
*/ |
105
|
2 |
|
public function setRetainFlag(bool $flag): Message |
106
|
|
|
{ |
107
|
2 |
|
$this->isRetained = $flag; |
108
|
2 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets the topic name to the given value |
113
|
|
|
* |
114
|
|
|
* @param string $topicName |
115
|
|
|
* @return Message |
116
|
|
|
*/ |
117
|
7 |
|
public function setTopicName(string $topicName): Message |
118
|
|
|
{ |
119
|
7 |
|
$this->topicName = $topicName; |
120
|
7 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Gets the topic name |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
9 |
|
public function getTopicName(): string |
129
|
|
|
{ |
130
|
9 |
|
return $this->topicName; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Gets the current QoS level |
135
|
|
|
* |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
15 |
|
public function getQoSLevel(): int |
139
|
|
|
{ |
140
|
15 |
|
return $this->qosLevel; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Gets the set retain flag |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
9 |
|
public function isRetained(): bool |
149
|
|
|
{ |
150
|
9 |
|
return $this->isRetained; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|