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

SubAck::getOriginControlPacket()   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\Protocol;
6
7
use unreal4u\MQTT\Exceptions\UnmatchingPacketIdentifiers;
8
use unreal4u\MQTT\Internals\ClientInterface;
9
use unreal4u\MQTT\Internals\ProtocolBase;
10
use unreal4u\MQTT\Internals\ReadableContent;
11
use unreal4u\MQTT\Internals\ReadableContentInterface;
12
use unreal4u\MQTT\Internals\WritableContentInterface;
13
14
/**
15
 * A SUBACK Packet is sent by the Server to the Client to confirm receipt and processing of a SUBSCRIBE Packet.
16
 */
17
final class SubAck extends ProtocolBase implements ReadableContentInterface
18
{
19
    use ReadableContent;
20
21
    const CONTROL_PACKET_VALUE = 9;
22
23
    /**
24
     * @var int
25
     */
26
    private $packetIdentifier = 0;
27
28
    /**
29
     * Information about each topic
30
     * @var string
31
     */
32
    private $payload = '';
33
34
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
35
    {
36
        // Read out the remaining length bytes should only 1 byte have come in until now
37
        if (\strlen($rawMQTTHeaders) === 1) {
38
            $rawMQTTHeaders .= $client->readBrokerData(1);
39
        }
40
41
        $remainingLength = \ord($rawMQTTHeaders[1]);
42
        // Check if we have a complete message
43
        if ($remainingLength + 2 !== \strlen($rawMQTTHeaders)) {
44
            $rawMQTTHeaders .= $client->readBrokerData($remainingLength + 2 - \strlen($rawMQTTHeaders));
45
        }
46
47
        $this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders);
48
        // TODO Check which QoS corresponds to each topic we are subscribed to
49
        $this->payload = substr($rawMQTTHeaders, 4);
50
        return $this;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     * @throws \LogicException
56
     */
57
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
58
    {
59
        /** @var Subscribe $originalRequest */
60
        if ($this->packetIdentifier !== $originalRequest->getPacketIdentifier()) {
61
            throw new UnmatchingPacketIdentifiers('Packet identifiers do not match!');
62
        }
63
64
        $client->updateLastCommunication();
65
        return true;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function getOriginControlPacket(): int
72
    {
73
        return Subscribe::getControlPacketValue();
74
    }
75
}
76