ClientId::__toString()   A
last analyzed

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 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\DataTypes;
6
7
/**
8
 * This Value Object will set a clientId and be able to diagnose problems with the clientId's value
9
 */
10
final class ClientId
11
{
12
    /**
13
     * This field indicates the name of the clientId that we'll pass on to the broker.
14
     *
15
     * @var string
16
     */
17
    private $clientId;
18
19
    /**
20
     * QoSLevel constructor.
21
     *
22
     * @param string $clientId
23
     */
24 24
    public function __construct(string $clientId)
25
    {
26 24
        $this->clientId = $clientId;
27 24
    }
28
29
    /**
30
     * Gets the current clientId
31
     *
32
     * @return string
33
     */
34 21
    public function getClientId(): string
35
    {
36 21
        return $this->clientId;
37
    }
38
39 21
    public function __toString(): string
40
    {
41 21
        return $this->getClientId();
42
    }
43
44 23
    public function isEmptyClientId(): bool
45
    {
46 23
        return $this->clientId === '';
47
    }
48
49
    /**
50
     * @return \Generator|string[]
51
     */
52 3
    public function performStrictValidationCheck(): \Generator
53
    {
54 3
        $utf8ClientIdSize = \mb_strlen($this->clientId);
55
56 3
        if ($this->isEmptyClientId()) {
57
            /*
58
             * If you ever wind up in this situation, search for MQTT-3.1.3-7 on the following document for more
59
             * information: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718067
60
             */
61 1
            yield 'ClientId size is 0 bytes. This has several implications, check comments';
62
        }
63
64 3
        if ($utf8ClientIdSize > 23) {
65 1
            yield 'The broker MAY reject the connection because the ClientId is too long';
66
        }
67
68 3
        if (\strlen($this->clientId) !== $utf8ClientIdSize) {
69 1
            yield 'The broker MAY reject the connection because of invalid characters';
70
        }
71 3
    }
72
}
73