Passed
Push — master ( c28170...630c00 )
by Camilo
02:49
created

ClientId::getClientId()   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
        if ($clientId !== '') {
28
            $this->clientId = $clientId;
29
            $clientIdSize = \strlen($this->clientId);
30
            $utf8ClientIdSize = \mb_strlen($this->clientId);
31
32
            if ($clientIdSize !== $utf8ClientIdSize) {
33
                $this->logger->warning('The broker MAY reject the connection because of invalid characters');
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on unreal4u\MQTT\DataTypes\ClientId. Did you maybe forget to declare it?
Loading history...
34
            }
35
36
            if ($utf8ClientIdSize > 23) {
37
                $this->logger->warning('The broker MAY reject the connection because the ClientId is too long');
38
            }
39
        } else {
40
            /*
41
             * If you ever wind up in this situation, search for MQTT-3.1.3-7 on the following document for more
42
             * information: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718067
43
             */
44
            $this->logger->warning('ClientId size is 0 bytes. This has several implications, check comments', [
45
                'file' => __FILE__,
46
                'line' => __LINE__,
47
            ]);
48
            $this->cleanSession = true;
0 ignored issues
show
Bug Best Practice introduced by
The property cleanSession does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
        }
50
51
        $this->clientId = $clientId;
52
    }
53
54
    /**
55
     * Gets the current clientId
56
     *
57
     * @return string
58
     */
59
    public function getClientId(): string
60
    {
61
        return $this->clientId;
62
    }
63
64
    public function __toString(): string
65
    {
66
        return $this->getClientId();
67
    }
68
}
69