Passed
Push — master ( ec7bc7...bfdde1 )
by Camilo
02:25
created

Message   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 146
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setTopic() 0 4 1
A setPayload() 0 4 1
A getQoSLevel() 0 3 1
A getTopicName() 0 7 2
A setRetainFlag() 0 4 1
A setQoSLevel() 0 9 3
A getPayload() 0 3 1
A validateMessage() 0 13 3
A isRetained() 0 3 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 7
    public function validateMessage(): Message
52
    {
53 7
        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 6
        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 5
        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 13
    public function setPayload(string $payload): Message
73
    {
74 13
        $this->payload = $payload;
75 13
        return $this;
76
    }
77
78 5
    public function getPayload(): string
79
    {
80 5
        return $this->payload;
81
    }
82
83
    /**
84
     * Sets the QoS level to the indicated value. Must be 0, 1 or 2.
85
     *
86
     * @param int $level
87
     * @return Message
88
     * @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel
89
     */
90 10
    public function setQoSLevel(int $level): Message
91
    {
92 10
        if ($level > 2 || $level < 0) {
93 1
            $this->logger->error('Invalid QoS level detected, must be 0, 1 or 2');
94 1
            throw new InvalidQoSLevel('The QoS level must be 0, 1 or 2');
95
        }
96
97 9
        $this->qosLevel = $level;
98 9
        return $this;
99
    }
100
101
    /**
102
     * Sets the retain flag to the given value
103
     *
104
     * @param bool $flag Set to true if message should be retained, false otherwise (default)
105
     * @return Message
106
     */
107 2
    public function setRetainFlag(bool $flag): Message
108
    {
109 2
        $this->isRetained = $flag;
110 2
        return $this;
111
    }
112
113
    /**
114
     * Sets the topic name to the given value
115
     *
116
     * @param Topic $topic
117
     * @return Message
118
     */
119 13
    public function setTopic(Topic $topic): Message
120
    {
121 13
        $this->topic = $topic;
122 13
        return $this;
123
    }
124
125
    /**
126
     * Gets the topic name
127
     *
128
     * @return string
129
     * @throws \unreal4u\MQTT\Exceptions\MissingTopicName
130
     */
131 10
    public function getTopicName(): string
132
    {
133 10
        if ($this->topic === null) {
134 1
            throw new MissingTopicName('A topic must be set before calling getTopicName()');
135
        }
136
137 9
        return $this->topic->getTopicName();
138
    }
139
140
    /**
141
     * Gets the current QoS level
142
     *
143
     * @return int
144
     */
145 14
    public function getQoSLevel(): int
146
    {
147 14
        return $this->qosLevel;
148
    }
149
150
    /**
151
     * Gets the set retain flag
152
     *
153
     * @return bool
154
     */
155 8
    public function isRetained(): bool
156
    {
157 8
        return $this->isRetained;
158
    }
159
}
160