Completed
Push — master ( d37c8b...c70816 )
by Camilo
02:32
created

TopicName::getTopicName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\DataTypes;
6
7
use unreal4u\MQTT\Internals\GeneralTopicRules;
8
9
/**
10
 * When the client wants to send a message to a topic, this is done by adding a topic NAME.
11
 */
12
final class TopicName extends GeneralTopicRules
13
{
14
    /**
15
     * The TopicName identifies the information channel to which payload data is published.
16
     *
17
     * @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718106
18
     * @var string
19
     */
20
    private $topicName;
21
22
    /**
23
     * TopicName constructor.
24
     * @param string $topicName
25
     * @throws \OutOfBoundsException
26
     * @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel
27
     * @throws \InvalidArgumentException
28
     */
29 48
    public function __construct(string $topicName)
30
    {
31 48
        $this->setTopicName($topicName);
32 47
    }
33
34
    /**
35
     * Contains the name of the TopicFilter Filter
36
     *
37
     * @param string $topicName
38
     * @return TopicFilter
39
     * @throws \OutOfBoundsException
40
     * @throws \InvalidArgumentException
41
     */
42 48
    private function setTopicName(string $topicName): self
43
    {
44 48
        $this->generalRulesCheck($topicName);
45
46
        // A topic name has some additional checks, as no wildcard characters are allowed
47 48
        if (strpbrk($topicName, '#+') !== false) {
48 1
            throw new \InvalidArgumentException('Topic names can not contain wildcard characters');
49
        }
50
51 47
        $this->topicName = $topicName;
52 47
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type unreal4u\MQTT\DataTypes\TopicName which is incompatible with the documented return type unreal4u\MQTT\DataTypes\TopicFilter.
Loading history...
53
    }
54
55
    /**
56
     * @return string
57
     */
58 15
    public function getTopicName(): string
59
    {
60 15
        return $this->topicName;
61
    }
62
63 1
    public function __toString()
64
    {
65 1
        return $this->getTopicName();
66
    }
67
}
68