Completed
Push — master ( f55639...d36af8 )
by Camilo
02:28
created

Unsubscribe   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 69
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createVariableHeader() 0 10 2
A createPayload() 0 8 2
A addTopics() 0 6 1
A shouldExpectAnswer() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Protocol;
6
7
use unreal4u\MQTT\DataTypes\Topic;
8
use unreal4u\MQTT\Exceptions\MustContainTopic;
9
use unreal4u\MQTT\Internals\PacketIdentifierFunctionality;
10
use unreal4u\MQTT\Internals\ProtocolBase;
11
use unreal4u\MQTT\Internals\WritableContent;
12
use unreal4u\MQTT\Internals\WritableContentInterface;
13
14
/**
15
 * An UNSUBSCRIBE Packet is sent by the Client to the Server, to unsubscribe from topics.
16
 */
17
final class Unsubscribe extends ProtocolBase implements WritableContentInterface
18
{
19
    use WritableContent, PacketIdentifierFunctionality;
20
21
    const CONTROL_PACKET_VALUE = 10;
22
23
    /**
24
     * An array of topics on which unsubscribe to
25
     * @var Topic[]
26
     */
27
    private $topics = [];
28
29
    /**
30
     * @return string
31
     * @throws \unreal4u\MQTT\Exceptions\MustContainTopic
32
     * @throws \OutOfRangeException
33
     */
34 3
    public function createVariableHeader(): string
35
    {
36 3
        if (count($this->topics) === 0) {
37 1
            throw new MustContainTopic('An unsubscribe command must contain at least one topic');
38
        }
39
40
        // Unsubscribe must always send a 2 flag
41 2
        $this->specialFlags = 2;
42
43 2
        return $this->getPacketIdentifierBinaryRepresentation();
44
    }
45
46
    /**
47
     * @return string
48
     * @throws \OutOfRangeException
49
     */
50 3
    public function createPayload(): string
51
    {
52 3
        $output = '';
53 3
        foreach ($this->topics as $topic) {
54
            // chr on QoS level is safe because it will create an 8-bit flag where the first 6 are only 0's
55 3
            $output .= $this->createUTF8String($topic->getTopicName());
56
        }
57 3
        return $output;
58
    }
59
60
    /**
61
     * When the Server receives a SUBSCRIBE Packet from a Client, the Server MUST respond with a SUBACK Packet
62
     *
63
     * This can however not be in the same order, as we may be able to receive PUBLISH packets before getting a SUBACK
64
     * back
65
     *
66
     * @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718134 (MQTT-3.8.4-1)
67
     * @return bool
68
     */
69 1
    public function shouldExpectAnswer(): bool
70
    {
71 1
        return true;
72
    }
73
74
    /**
75
     * A subscription is based on filters, this function allows us to pass on filters
76
     *
77
     * @param Topic[] $topics
78
     * @return Unsubscribe
79
     */
80 5
    public function addTopics(Topic ...$topics): self
81
    {
82 5
        $this->topics = array_merge($this->topics, $topics);
83 5
        $this->logger->debug('Topics added', ['totalTopics', count($this->topics)]);
84
85 5
        return $this;
86
    }
87
}
88