Passed
Push — master ( 56612a...1b3824 )
by Camilo
09:25
created

UnSubAck   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fillObject() 0 9 2
A performSpecialActions() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Protocol;
6
7
use unreal4u\MQTT\Internals\ClientInterface;
8
use unreal4u\MQTT\Internals\ProtocolBase;
9
use unreal4u\MQTT\Internals\ReadableContent;
10
use unreal4u\MQTT\Internals\ReadableContentInterface;
11
use unreal4u\MQTT\Internals\WritableContentInterface;
12
13
/**
14
 * Class SubAck
15
 * @package unreal4u\MQTT\Protocol
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 (mb_strlen($rawMQTTHeaders) === 1) {
32
            $rawMQTTHeaders .= $client->readSocketData(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 Subscribe $originalRequest */
46
        if ($this->packetIdentifier !== $originalRequest->getPacketIdentifier()) {
47
            throw new \LogicException('Packet identifiers do not match!');
48
        }
49
50
        $client
51
            ->updateLastCommunication()
52
            ->setBlocking(false)
53
        ;
54
        return true;
55
    }
56
}
57