Completed
Push — master ( ed1e17...f3eba8 )
by Camilo
02:15
created

ClientId::isEmptyClientId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\DataTypes;
6
7
/**
8
 * This Value Object will always contain a valid Packet Identifier
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 int $packetIdentifier
23
     * @throws \OutOfRangeException
24
     */
25
    public function __construct(string $clientId)
26
    {
27
        $this->clientId = $clientId;
28
    }
29
30
    /**
31
     * Gets the current clientId
32
     *
33
     * @return string
34
     */
35
    public function getClientId(): string
36
    {
37
        return $this->clientId;
38
    }
39
40
    public function __toString(): string
41
    {
42
        return $this->getClientId();
43
    }
44
45
    public function isEmptyClientId(): bool
46
    {
47
        return $this->clientId === '';
48
    }
49
50
    public function performStrictValidationCheck(): string
51
    {
52
        $possibleProblem = '';
53
54
        $utf8ClientIdSize = \mb_strlen($this->clientId);
55
56
        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
            $possibleProblem = 'ClientId size is 0 bytes. This has several implications, check comments';
62
        } elseif ($utf8ClientIdSize > 23) {
63
            $possibleProblem = 'The broker MAY reject the connection because the ClientId is too long';
64
        } elseif (\strlen($this->clientId) !== $utf8ClientIdSize) {
65
            $possibleProblem = 'The broker MAY reject the connection because of invalid characters';
66
        }
67
68
        return $possibleProblem;
69
    }
70
}
71