Passed
Push — master ( 57475a...9da739 )
by Camilo
02:27
created

Message::getQoSLevel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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