Completed
Push — master ( b34ec5...5542e3 )
by Camilo
02:15
created

Message::validateMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Application;
6
7
use unreal4u\MQTT\DataTypes\QoSLevel;
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 QoSLevel
22
     */
23
    private $qosLevel;
24
25
    /**
26
     * @var string
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 Topic
41
     */
42
    private $topic;
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
        // Getter of topicname will validate whether the topic name is set and valid
54 8
        $this->getTopicName();
55
56 7
        if (mb_strlen($this->payload) > 65535) {
57 1
            $this->logger->error('Message payload exceeds 65535 bytes');
58 1
            throw new MessageTooBig('Message payload can not exceed 65535 bytes!');
59
        }
60
61 6
        return $this;
62
    }
63
64
    /**
65
     * Sets the actual payload to be sent to the message broker
66
     *
67
     * @param string $payload
68
     * @return Message
69
     */
70 14
    public function setPayload(string $payload): Message
71
    {
72 14
        $this->payload = $payload;
73 14
        return $this;
74
    }
75
76 6
    public function getPayload(): string
77
    {
78 6
        return $this->payload;
79
    }
80
81
    /**
82
     * Sets the QoS level to the indicated value. Must be 0, 1 or 2.
83
     *
84
     * @param QoSLevel $level
85
     * @return Message
86
     * @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel
87
     */
88 6
    public function setQoSLevel(QosLevel $level): Message
89
    {
90 6
        $this->qosLevel = $level;
91 6
        return $this;
92
    }
93
94
    /**
95
     * Sets the retain flag to the given value
96
     *
97
     * @param bool $flag Set to true if message should be retained, false otherwise (default)
98
     * @return Message
99
     */
100 2
    public function setRetainFlag(bool $flag): Message
101
    {
102 2
        $this->isRetained = $flag;
103 2
        return $this;
104
    }
105
106
    /**
107
     * Sets the topic name to the given value
108
     *
109
     * @param Topic $topic
110
     * @return Message
111
     */
112 14
    public function setTopic(Topic $topic): Message
113
    {
114 14
        $this->topic = $topic;
115 14
        return $this;
116
    }
117
118
    /**
119
     * Gets the topic name
120
     *
121
     * @return string
122
     * @throws \unreal4u\MQTT\Exceptions\MissingTopicName
123
     */
124 11
    public function getTopicName(): string
125
    {
126 11
        if ($this->topic === null) {
127 1
            throw new MissingTopicName('A topic must be set before calling getTopicName()');
128
        }
129
130 10
        return $this->topic->getTopicName();
131
    }
132
133
    /**
134
     * Gets the current QoS level
135
     *
136
     * @return int
137
     * @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel
138
     */
139 12
    public function getQoSLevel(): int
140
    {
141 12
        if ($this->qosLevel === null) {
142 6
            $this->qosLevel = new QoSLevel(0);
143
        }
144 12
        return $this->qosLevel->getQoSLevel();
145
    }
146
147
    /**
148
     * Gets the set retain flag
149
     *
150
     * @return bool
151
     */
152 9
    public function isRetained(): bool
153
    {
154 9
        return $this->isRetained;
155
    }
156
}
157