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

PubRel::shouldExpectAnswer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 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\PacketIdentifier;
9
use unreal4u\MQTT\Internals\ProtocolBase;
10
use unreal4u\MQTT\Internals\ReadableContent;
11
use unreal4u\MQTT\Internals\ReadableContentInterface;
12
use unreal4u\MQTT\Internals\WritableContent;
13
use unreal4u\MQTT\Internals\WritableContentInterface;
14
use unreal4u\MQTT\Utilities;
15
16
/**
17
 * A PUBREL Packet is the response to a PUBREC Packet.
18
 *
19
 * It is the third packet of the QoS 2 protocol exchange.
20
 */
21
final class PubRel extends ProtocolBase implements ReadableContentInterface, WritableContentInterface
22
{
23
    use ReadableContent, WritableContent, PacketIdentifier;
24
25
    const CONTROL_PACKET_VALUE = 6;
26
27
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
28
    {
29
        $rawHeadersSize = \strlen($rawMQTTHeaders);
30
        // A PubRel message is always 4 bytes in size
31
        if ($rawHeadersSize !== 4) {
32
            $this->logger->debug('Headers are smaller than 4 bytes, retrieving the rest', [
33
                'currentSize' => $rawHeadersSize
34
            ]);
35
            $rawMQTTHeaders .= $client->readBrokerData(4 - $rawHeadersSize);
36
        }
37
        $this->setPacketIdentifierFromRawHeaders($rawMQTTHeaders);
38
        $this->logger->debug('Determined packet identifier', ['PI' => $this->packetIdentifier]);
39
40
        return $this;
41
    }
42
43
    /**
44
     * Creates the variable header that each method has
45
     * @return string
46
     * @throws \OutOfRangeException
47
     */
48
    public function createVariableHeader(): string
49
    {
50
        $this->specialFlags |= 2;
51
        return $this->getPacketIdentifierBinaryRepresentation();
52
    }
53
54
    /**
55
     * Creates the actual payload to be sent
56
     * @return string
57
     */
58
    public function createPayload(): string
59
    {
60
        return '';
61
    }
62
63
    /**
64
     * PUBREL should ALWAYS expect an answer back (in the form of a PUBCOMP)
65
     * @return bool
66
     */
67
    public function shouldExpectAnswer(): bool
68
    {
69
        return true;
70
    }
71
72
    /**
73
     * @param ClientInterface $client
74
     * @param WritableContentInterface $originalRequest
75
     * @return bool
76
     * @throws \LogicException
77
     */
78
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
79
    {
80
        if ($originalRequest instanceof PubRec) {
81
            $this->logger->debug('Checking packet identifier on PubRel', [
82
                'pubRelPI' => $this->packetIdentifier,
83
                'originalRequestPI' => $originalRequest->packetIdentifier,
0 ignored issues
show
Bug introduced by
The property packetIdentifier is declared private in unreal4u\MQTT\Protocol\PubRec and cannot be accessed from this context.
Loading history...
84
            ]);
85
86
            if ($this->packetIdentifier !== $originalRequest->packetIdentifier) {
87
                throw new \LogicException('Packet identifiers to not match!');
88
            }
89
90
            $pubComp = new PubComp($this->logger);
91
            $pubComp->packetIdentifier = $this->packetIdentifier;
92
            $client->processObject($pubComp);
93
94
95
            return true;
96
        }
97
98
        $this->logger->warning('Original request NOT a PubRec, ignoring object entirely');
99
        return false;
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function getOriginControlPacket(): int
106
    {
107
        return PubRec::getControlPacketValue();
108
    }
109
}
110