Completed
Push — master ( d94a2b...d27c94 )
by Camilo
02:20
created

PubRel   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 70
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A performSpecialActions() 0 13 2
A fillObject() 0 4 1
A createVariableHeader() 0 4 1
A originPacketIdentifier() 0 3 1
A shouldExpectAnswer() 0 3 1
A createPayload() 0 3 1
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\WritableContent;
12
use unreal4u\MQTT\Internals\WritableContentInterface;
13
use unreal4u\MQTT\Utilities;
14
15
/**
16
 * A PUBREL Packet is the response to a PUBREC Packet.
17
 *
18
 * It is the third packet of the QoS 2 protocol exchange.
19
 */
20
final class PubRel extends ProtocolBase implements ReadableContentInterface, WritableContentInterface
21
{
22
    use ReadableContent, WritableContent;
23
24
    public $packetIdentifier = 0;
25
26
    const CONTROL_PACKET_VALUE = 6;
27
28
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
29
    {
30
        $this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders);
31
        return $this;
32
    }
33
34
    /**
35
     * Creates the variable header that each method has
36
     * @return string
37
     * @throws \OutOfRangeException
38
     */
39
    public function createVariableHeader(): string
40
    {
41
        $this->specialFlags |= 2;
42
        return Utilities::convertNumberToBinaryString($this->packetIdentifier);
43
    }
44
45
    /**
46
     * Creates the actual payload to be sent
47
     * @return string
48
     */
49
    public function createPayload(): string
50
    {
51
        return '';
52
    }
53
54
    /**
55
     * PUBREL should ALWAYS expect an answer back (in the form of a PUBCOMP)
56
     * @return bool
57
     */
58
    public function shouldExpectAnswer(): bool
59
    {
60
        return true;
61
    }
62
63
    /**
64
     * @param ClientInterface $client
65
     * @param WritableContentInterface $originalRequest
66
     * @return bool
67
     * @throws \LogicException
68
     */
69
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
70
    {
71
        $this->logger->debug('Checking packet identifier on PubRel');
72
        /** @var PubRec $originalRequest */
73
        if ($this->packetIdentifier !== $originalRequest->packetIdentifier) {
74
            throw new \LogicException('Packet identifiers to not match!');
75
        }
76
77
        $pubComp = new PubComp($this->logger);
78
        $pubComp->packetIdentifier = $this->packetIdentifier;
79
        $client->sendData($pubComp);
80
81
        return true;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function originPacketIdentifier(): int
88
    {
89
        return PubRec::getControlPacketValue();
90
    }
91
}
92