Passed
Push — master ( 0ad4bf...cebcdc )
by Camilo
02:13
created

PacketIdentifierFunctionality   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 85
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setPacketIdentifierFromRawHeaders() 0 7 1
A setPacketIdentifier() 0 4 1
A controlPacketIdentifiers() 0 11 2
A getPacketIdentifier() 0 7 2
A generateRandomPacketIdentifier() 0 15 2
A getPacketIdentifierBinaryRepresentation() 0 3 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 11
    final public function setPacketIdentifier(PacketIdentifier $packetIdentifier): self
24
    {
25 11
        $this->packetIdentifier = $packetIdentifier;
26 11
        return $this;
27
    }
28
29 12
    final public function getPacketIdentifier(): int
30
    {
31 12
        if ($this->packetIdentifier === null) {
32 1
            $this->generateRandomPacketIdentifier();
33
        }
34
35 12
        return $this->packetIdentifier->getPacketIdentifierValue();
36
    }
37
38
    /**
39
     * Returns the binary representation of the packet identifier
40
     *
41
     * @return string
42
     * @throws \OutOfRangeException
43
     */
44 6
    final public function getPacketIdentifierBinaryRepresentation(): string
45
    {
46 6
        return Utilities::convertNumberToBinaryString($this->getPacketIdentifier());
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 1
    final public function generateRandomPacketIdentifier(): self
66
    {
67
        try {
68 1
            $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 1
        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 6
    private function controlPacketIdentifiers(WritableContentInterface $originalRequest): bool
90
    {
91
        /** @var PacketIdentifierFunctionality $originalRequest */
92 6
        if ($this->getPacketIdentifier() !== $originalRequest->getPacketIdentifier()) {
93 2
            $e = new NonMatchingPacketIdentifiers('Packet identifiers do not match');
94 2
            $e->setOriginPacketIdentifier(new PacketIdentifier($originalRequest->getPacketIdentifier()));
95 2
            $e->setReturnedPacketIdentifier($this->packetIdentifier);
96 2
            throw $e;
97
        }
98
99 4
        return true;
100
    }
101
}
102