Completed
Push — master ( dabd6a...bb30b8 )
by Camilo
02:29
created

Message::setTopic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 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 8
        if ($this->getTopicName() === '') {
54
            throw new MissingTopicName('Topic name can\'t be empty, please provide one');
55
        }
56
57 7
        if (mb_strlen($this->payload) > 65535) {
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 int $level
85
     * @return Message
86
     * @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel
87
     */
88 10
    public function setQoSLevel(int $level): Message
89
    {
90 10
        if ($level > 2 || $level < 0) {
91 1
            throw new InvalidQoSLevel('The QoS level must be 0, 1 or 2');
92
        }
93
94 9
        $this->qosLevel = $level;
95 9
        return $this;
96
    }
97
98
    /**
99
     * Sets the retain flag to the given value
100
     *
101
     * @param bool $flag Set to true if message should be retained, false otherwise (default)
102
     * @return Message
103
     */
104 2
    public function setRetainFlag(bool $flag): Message
105
    {
106 2
        $this->isRetained = $flag;
107 2
        return $this;
108
    }
109
110
    /**
111
     * Sets the topic name to the given value
112
     *
113
     * @param Topic $topic
114
     * @return Message
115
     */
116 14
    public function setTopic(Topic $topic): Message
117
    {
118 14
        $this->topic = $topic;
119 14
        return $this;
120
    }
121
122
    /**
123
     * Gets the topic name
124
     *
125
     * @return string
126
     * @throws \unreal4u\MQTT\Exceptions\MissingTopicName
127
     */
128 11
    public function getTopicName(): string
129
    {
130 11
        if ($this->topic === null) {
131 1
            throw new MissingTopicName('A topic must be set before calling getTopicName()');
132
        }
133
134 10
        return $this->topic->getTopicName();
135
    }
136
137
    /**
138
     * Gets the current QoS level
139
     *
140
     * @return int
141
     */
142 15
    public function getQoSLevel(): int
143
    {
144 15
        return $this->qosLevel;
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