Passed
Push — master ( f127af...513769 )
by Camilo
02:40
created

PubRec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 69
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fillObject() 0 4 1
A performSpecialActions() 0 8 1
A getOriginControlPacket() 0 3 1
A createPayload() 0 3 1
A shouldExpectAnswer() 0 3 1
A createVariableHeader() 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\PacketIdentifierFunctionality;
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
15
/**
16
 * A PUBREC Packet is the response to a PUBLISH Packet with QoS 2.
17
 *
18
 * It is the second packet of the QoS 2 protocol exchange.
19
 *
20
 * First packet: PUBLISH (QoS lvl2)
21
 * Second packet: PUBREC
22
 * Third packet: PUBREL
23
 * Fourth packet: PUBCOMP
24
 *
25
 * @see https://go.gliffy.com/go/publish/12498076
26
 */
27
final class PubRec extends ProtocolBase implements ReadableContentInterface, WritableContentInterface
28
{
29
    use ReadableContent, WritableContent, PacketIdentifierFunctionality;
30
31
    const CONTROL_PACKET_VALUE = 5;
32
33
    /**
34
     * @param string $rawMQTTHeaders
35
     * @param ClientInterface $client
36
     * @return ReadableContentInterface
37
     * @throws \OutOfRangeException
38
     */
39 1
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
40
    {
41 1
        $this->setPacketIdentifierFromRawHeaders($rawMQTTHeaders);
42 1
        return $this;
43
    }
44
45
    /**
46
     * Creates the variable header that each method has
47
     * @return string
48
     * @throws \OutOfRangeException
49
     */
50 1
    public function createVariableHeader(): string
51
    {
52 1
        return $this->getPacketIdentifierBinaryRepresentation();
53
    }
54
55
    /**
56
     * Creates the actual payload to be sent
57
     * @return string
58
     */
59 1
    public function createPayload(): string
60
    {
61 1
        return '';
62
    }
63
64
    /**
65
     * Some responses won't expect an answer back, others do in some situations
66
     * @return bool
67
     */
68 1
    public function shouldExpectAnswer(): bool
69
    {
70 1
        return true;
71
    }
72
73
    /**
74
     * Any class can overwrite the default behaviour
75
     * @param ClientInterface $client
76
     * @param WritableContentInterface $originalRequest
77
     * @return bool
78
     * @throws \LogicException
79
     */
80 1
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
81
    {
82 1
        $this->controlPacketIdentifiers($originalRequest);
83 1
        $pubRel = new PubRel($this->logger);
84 1
        $pubRel->setPacketIdentifier($this->packetIdentifier);
85 1
        $pubComp = $client->processObject($pubRel);
86 1
        $this->logger->debug('Created PubRel as response, got PubComp back', ['PubComp' => $pubComp]);
87 1
        return true;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 1
    public function getOriginControlPacket(): int
94
    {
95 1
        return Publish::getControlPacketValue();
96
    }
97
}
98