Passed
Push — master ( d27c94...6659fb )
by Camilo
02:22
created

PubAck   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 17.65%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 64
ccs 3
cts 17
cp 0.1765
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createPayload() 0 3 1
A originPacketIdentifier() 0 3 1
A shouldExpectAnswer() 0 3 1
A expectAnswer() 0 3 1
A createVariableHeader() 0 3 1
A performSpecialActions() 0 7 2
A fillObject() 0 4 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 PUBACK Packet is the response to a PUBLISH Packet with QoS level 1.
17
 */
18
final class PubAck extends ProtocolBase implements ReadableContentInterface, WritableContentInterface
19
{
20
    use ReadableContent, WritableContent;
21
22
    public $packetIdentifier = 0;
23
24
    const CONTROL_PACKET_VALUE = 4;
25
26 1
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
27
    {
28 1
        $this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders);
29 1
        return $this;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     * @throws \LogicException
35
     */
36
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
37
    {
38
        /** @var Publish $originalRequest */
39
        if ($this->packetIdentifier !== $originalRequest->packetIdentifier) {
40
            throw new \LogicException('Packet identifiers to not match!');
41
        }
42
        return true;
43
    }
44
45
    public function createVariableHeader(): string
46
    {
47
        return Utilities::convertNumberToBinaryString($this->packetIdentifier);
48
    }
49
50
    /**
51
     * Creates the actual payload to be sent
52
     * @return string
53
     */
54
    public function createPayload(): string
55
    {
56
        return '';
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function expectAnswer(string $data, ClientInterface $client): ReadableContentInterface
63
    {
64
        return $this;
65
    }
66
67
    /**
68
     * Some responses won't expect an answer back, others do in some situations
69
     * @return bool
70
     */
71
    public function shouldExpectAnswer(): bool
72
    {
73
        return false;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function originPacketIdentifier(): int
80
    {
81
        return Publish::getControlPacketValue();
82
    }
83
}
84