Completed
Push — master ( bb30b8...368962 )
by Camilo
02:11
created

UnsubAck::fillObject()   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
 * Class SubAck
16
 * @package unreal4u\MQTT\Protocol
17
 */
18
final class UnsubAck extends ProtocolBase implements ReadableContentInterface
19
{
20
    use ReadableContent;
21
22
    const CONTROL_PACKET_VALUE = 11;
23
24
    /**
25
     * @var int
26
     */
27
    private $packetIdentifier = 0;
28
29
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
30
    {
31
        // Read the rest of the request out should only 1 byte have come in
32
        if (\strlen($rawMQTTHeaders) === 1) {
33
            $rawMQTTHeaders .= $client->readSocketData(3);
34
        }
35
36
        $this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders);
37
        return $this;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     * @throws \LogicException
43
     */
44
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
45
    {
46
        /** @var Unsubscribe $originalRequest */
47
        if ($this->packetIdentifier !== $originalRequest->getPacketIdentifier()) {
48
            throw new UnmatchingPacketIdentifiers('Packet identifiers do not match!');
49
        }
50
51
        $client->updateLastCommunication();
52
        return true;
53
    }
54
}
55