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

UnsubAck::performSpecialActions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 6
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