Completed
Push — master ( 336156...094757 )
by Camilo
02:28
created

setPacketIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
use unreal4u\MQTT\DataTypes\PacketIdentifier;
8
use unreal4u\MQTT\Exceptions\NonMatchingPacketIdentifiers;
9
use unreal4u\MQTT\Utilities;
10
11
/**
12
 * Trait ReadableContent
13
 * @package unreal4u\MQTT\Internals
14
 */
15
trait PacketIdentifierFunctionality
16
{
17
    /**
18
     * The packet identifier variable
19
     * @var PacketIdentifier
20
     */
21
    private $packetIdentifier;
22
23 8
    final public function setPacketIdentifier(PacketIdentifier $packetIdentifier): self
24
    {
25 8
        $this->packetIdentifier = $packetIdentifier;
26 8
        return $this;
27
    }
28
29 5
    final public function getPacketIdentifier(): int
30
    {
31 5
        return $this->packetIdentifier->getPacketIdentifierValue();
32
    }
33
34
    /**
35
     * Returns the binary representation of the packet identifier
36
     *
37
     * @return string
38
     * @throws \OutOfRangeException
39
     */
40 4
    final public function getPacketIdentifierBinaryRepresentation(): string
41
    {
42 4
        if ($this->packetIdentifier === null) {
43
            $this->generateRandomPacketIdentifier();
44
        }
45
46 4
        return Utilities::convertNumberToBinaryString($this->packetIdentifier->getPacketIdentifierValue());
47
    }
48
49
    /**
50
     * Sets the packet identifier straight from the raw MQTT headers
51
     *
52
     * @param string $rawMQTTHeaders
53
     * @return self
54
     * @throws \OutOfRangeException
55
     */
56 5
    final public function setPacketIdentifierFromRawHeaders(string $rawMQTTHeaders): self
57
    {
58 5
        $this->packetIdentifier = new PacketIdentifier(
59 5
            Utilities::convertBinaryStringToNumber($rawMQTTHeaders{2} . $rawMQTTHeaders{3})
60
        );
61
62 5
        return $this;
63
    }
64
65
    final public function generateRandomPacketIdentifier(): self
66
    {
67
        try {
68
            $this->packetIdentifier = new PacketIdentifier(random_int(1, 65535));
69
        } catch (\Exception $e) {
70
            /*
71
             * Default to an older method, there should be no security issues here I believe.
72
             *
73
             * If I am mistaken, please contact me at https://t.me/unreal4u
74
             */
75
            /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
76
            /** @noinspection RandomApiMigrationInspection */
77
            $this->packetIdentifier = new PacketIdentifier(mt_rand(1, 65535));
78
        }
79
        return $this;
80
    }
81
82
    /**
83
     * Checks whether the original request with the current stored packet identifier matches
84
     *
85
     * @param WritableContentInterface $originalRequest
86
     * @throws NonMatchingPacketIdentifiers
87
     * @return bool
88
     */
89 4
    private function controlPacketIdentifiers(WritableContentInterface $originalRequest): bool
90
    {
91
        /** @var PacketIdentifierFunctionality $originalRequest */
92 4
        if ($this->getPacketIdentifier() !== $originalRequest->getPacketIdentifier()) {
93 2
            throw new NonMatchingPacketIdentifiers('Packet identifiers to not match!');
94
        }
95
96 2
        return true;
97
    }
98
}
99