Passed
Push — master ( fc052b...13a97b )
by Camilo
02:52
created

Topic   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 122
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setQoSLevel() 0 9 3
A getTopicName() 0 3 1
A __construct() 0 6 1
A setTopicName() 0 9 2
A getTopicQoSLevel() 0 3 1
A getPayloadType() 0 3 1
A setPayloadType() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Protocol\Subscribe;
6
7
use unreal4u\MQTT\Application\PayloadInterface;
8
use unreal4u\MQTT\Application\SimplePayload;
9
use unreal4u\MQTT\Exceptions\InvalidQoSLevel;
10
11
/**
12
 * When the client wants to subscribe to a topic, this is done by adding a topic filter.
13
 */
14
final class Topic
15
{
16
    /**
17
     * The 1st byte can contain some bits
18
     *
19
     * The order of these flags are:
20
     *
21
     *   7-6-5-4-3-2-1-0
22
     * b'0-0-0-0-0-0-0-0'
23
     *
24
     * Bit 7-4: Control packet value ID (3 for PUBLISH)
25
     * Bit 3: Duplicate delivery of a PUBLISH Control Packet
26
     * Bit 2 & 1: PUBLISH Quality of Service
27
     * Bit 0: PUBLISH Retain flag
28
     *
29
     * @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.2_-
30
     * @var string
31
     */
32
    private $topicName = '';
33
34
    /**
35
     * The QoS lvl, choose between 0 and 2
36
     * @var int
37
     */
38
    private $qosLevel = 0;
39
40
    /**
41
     * Allows for a custom payload type per topic, defaults to SimplePayload
42
     *
43
     * @TODO This is still work in progress and may change in the future
44
     *
45
     * @see SimplePayload
46
     * @var PayloadInterface
47
     */
48
    private $payloadType;
49
50
    /**
51
     * Topic constructor.
52
     * @param string $topicName
53
     * @param int $qosLevel
54
     * @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel
55
     * @throws \InvalidArgumentException
56
     */
57 5
    public function __construct(string $topicName, int $qosLevel = 0)
58
    {
59
        $this
60 5
            ->setTopicName($topicName)
61 4
            ->setQoSLevel($qosLevel)
62 3
            ->setPayloadType(new SimplePayload());
63 3
    }
64
65
    /**
66
     * Contains the name of the Topic Filter
67
     *
68
     * @param string $topicName
69
     * @return Topic
70
     * @throws \InvalidArgumentException
71
     */
72 5
    private function setTopicName(string $topicName): Topic
73
    {
74 5
        if ($topicName === '') {
75 1
            throw new \InvalidArgumentException('Topic name must be set');
76
        }
77
78 4
        $this->topicName = $topicName;
79
80 4
        return $this;
81
    }
82
83
    /**
84
     * Requested QoS level is the maximum QoS level at which the Server can send Application Messages to the Client
85
     *
86
     * @param int $qosLevel
87
     * @return Topic
88
     * @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel
89
     */
90 4
    private function setQoSLevel(int $qosLevel): Topic
91
    {
92 4
        if ($qosLevel > 2 || $qosLevel < 0) {
93 1
            throw new InvalidQoSLevel('The provided QoS level is invalid');
94
        }
95
96 3
        $this->qosLevel = $qosLevel;
97
98 3
        return $this;
99
    }
100
101
    /**
102
     * Allows to set a different payload type for each topic
103
     *
104
     * @param PayloadInterface $payloadType
105
     * @return Topic
106
     */
107 3
    public function setPayloadType(PayloadInterface $payloadType): Topic
108
    {
109 3
        $this->payloadType = $payloadType;
110
111 3
        return $this;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 3
    public function getTopicName(): string
118
    {
119 3
        return $this->topicName;
120
    }
121
122
    /**
123
     * @return int
124
     */
125 3
    public function getTopicQoSLevel(): int
126
    {
127 3
        return $this->qosLevel;
128
    }
129
130
    /**
131
     * @return PayloadInterface
132
     */
133 3
    public function getPayloadType(): PayloadInterface
134
    {
135 3
        return $this->payloadType;
136
    }
137
}
138