Completed
Push — master ( 639ef0...066714 )
by Camilo
02:30
created

PubRel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 57
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fillObject() 0 4 1
A performSpecialActions() 0 8 2
A createVariableHeader() 0 4 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
final class PubRel extends ProtocolBase implements ReadableContentInterface, WritableContentInterface
16
{
17
    use ReadableContent, WritableContent;
18
19
    public $packetIdentifier = 0;
20
21
    const CONTROL_PACKET_VALUE = 6;
22
23
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
24
    {
25
        $this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders);
26
        return $this;
27
    }
28
29
    /**
30
     * Creates the variable header that each method has
31
     * @return string
32
     * @throws \OutOfRangeException
33
     */
34
    public function createVariableHeader(): string
35
    {
36
        $this->specialFlags |= 2;
37
        return Utilities::convertNumberToBinaryString($this->packetIdentifier);
38
    }
39
40
    /**
41
     * Creates the actual payload to be sent
42
     * @return string
43
     */
44
    public function createPayload(): string
45
    {
46
        return '';
47
    }
48
49
    /**
50
     * PUBREL should ALWAYS expect an answer back (in the form of a PUBCOMP)
51
     * @return bool
52
     */
53
    public function shouldExpectAnswer(): bool
54
    {
55
        return true;
56
    }
57
58
    /**
59
     * @param ClientInterface $client
60
     * @param WritableContentInterface $originalRequest
61
     * @return bool
62
     * @throws \LogicException
63
     */
64
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
65
    {
66
        $this->logger->debug('Checking packet identifier on PubRel');
67
        /** @var PubRec $originalRequest */
68
        if ($this->packetIdentifier !== $originalRequest->packetIdentifier) {
69
            throw new \LogicException('Packet identifiers to not match!');
70
        }
71
        return false;
72
    }
73
}
74