Completed
Push — master ( accf18...c40548 )
by Camilo
02:13
created

PacketIdentifier   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 65
ccs 8
cts 18
cp 0.4444
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generateRandomPacketIdentifier() 0 15 2
A getPacketIdentifier() 0 3 1
A setPacketIdentifierFromRawHeaders() 0 7 1
A setPacketIdentifier() 0 4 1
A getPacketIdentifierBinaryRepresentation() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
use unreal4u\MQTT\DataTypes\PacketIdentifier as PacketIdentifierDataType;
8
use unreal4u\MQTT\Utilities;
9
10
/**
11
 * Trait ReadableContent
12
 * @package unreal4u\MQTT\Internals
13
 */
14
trait PacketIdentifier
15
{
16
    /**
17
     * The packet identifier variable
18
     * @var PacketIdentifierDataType
19
     */
20
    private $packetIdentifier;
21
22 2
    final public function setPacketIdentifier(PacketIdentifierDataType $packetIdentifier): self
23
    {
24 2
        $this->packetIdentifier = $packetIdentifier;
25 2
        return $this;
26
    }
27
28 1
    final public function getPacketIdentifier(): int
29
    {
30 1
        return $this->packetIdentifier->getPacketIdentifierValue();
31
    }
32
33
    /**
34
     * Returns the binary representation of the packet identifier
35
     *
36
     * @return string
37
     * @throws \OutOfRangeException
38
     */
39 2
    final public function getPacketIdentifierBinaryRepresentation(): string
40
    {
41 2
        if ($this->packetIdentifier === null) {
42
            $this->generateRandomPacketIdentifier();
43
        }
44
45 2
        return Utilities::convertNumberToBinaryString($this->packetIdentifier->getPacketIdentifierValue());
46
    }
47
48
    /**
49
     * Sets the packet identifier straight from the raw MQTT headers
50
     *
51
     * @param string $rawMQTTHeaders
52
     * @return self
53
     * @throws \OutOfRangeException
54
     */
55
    final public function setPacketIdentifierFromRawHeaders(string $rawMQTTHeaders): self
56
    {
57
        $this->packetIdentifier = new PacketIdentifierDataType(Utilities::convertBinaryStringToNumber(
58
            $rawMQTTHeaders{2} . $rawMQTTHeaders{3})
59
        );
60
61
        return $this;
62
    }
63
64
    final public function generateRandomPacketIdentifier(): self
65
    {
66
        try {
67
            $this->packetIdentifier = new PacketIdentifierDataType(random_int(1, 65535));
68
        } catch (\Exception $e) {
69
            /*
70
             * Default to an older method, there should be no security issues here I believe.
71
             *
72
             * If I am mistaken, please contact me at https://t.me/unreal4u
73
             */
74
            /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
75
            /** @noinspection RandomApiMigrationInspection */
76
            $this->packetIdentifier = new PacketIdentifierDataType(mt_rand(1, 65535));
77
        }
78
        return $this;
79
    }
80
}
81