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

UnsubAck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 43
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fillObject() 0 9 2
A performSpecialActions() 0 9 2
A getOriginControlPacket() 0 3 1
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
 * The UNSUBACK Packet is sent by the Server to the Client to confirm receipt of an UNSUBSCRIBE Packet.
16
 */
17
final class UnsubAck extends ProtocolBase implements ReadableContentInterface
18
{
19
    use ReadableContent;
20
21
    const CONTROL_PACKET_VALUE = 11;
22
23
    /**
24
     * @var int
25
     */
26
    private $packetIdentifier = 0;
27
28
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
29
    {
30
        // Read the rest of the request out should only 1 byte have come in
31
        if (\strlen($rawMQTTHeaders) === 1) {
32
            $rawMQTTHeaders .= $client->readBrokerData(3);
33
        }
34
35
        $this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders);
36
        return $this;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     * @throws \LogicException
42
     */
43
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
44
    {
45
        /** @var Unsubscribe $originalRequest */
46
        if ($this->packetIdentifier !== $originalRequest->getPacketIdentifier()) {
47
            throw new UnmatchingPacketIdentifiers('Packet identifiers do not match!');
48
        }
49
50
        $client->updateLastCommunication();
51
        return true;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function getOriginControlPacket(): int
58
    {
59
        return Unsubscribe::getControlPacketValue();
60
    }
61
}
62