Completed
Push — master ( 474237...d94a2b )
by Camilo
02:11
created

PubRec::createPayload()   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\EventManager;
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
final class PubRec extends ProtocolBase implements ReadableContentInterface, WritableContentInterface
17
{
18
    use ReadableContent, WritableContent;
19
20
    public $packetIdentifier = 0;
21
22
    const CONTROL_PACKET_VALUE = 5;
23
24
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
25
    {
26
        $this->packetIdentifier = $this->extractPacketIdentifier($rawMQTTHeaders);
27
        return $this;
28
    }
29
30
    /**
31
     * Creates the variable header that each method has
32
     * @return string
33
     * @throws \OutOfRangeException
34
     */
35
    public function createVariableHeader(): string
36
    {
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
     * Some responses won't expect an answer back, others do in some situations
51
     * @return bool
52
     */
53
    public function shouldExpectAnswer(): bool
54
    {
55
        return true;
56
    }
57
58
    /**
59
     * Any class can overwrite the default behaviour
60
     * @param ClientInterface $client
61
     * @param WritableContentInterface $originalRequest
62
     * @return bool
63
     */
64
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
65
    {
66
        $pubRel = new PubRel($this->logger);
67
        $pubRel->packetIdentifier = $this->packetIdentifier;
68
        $pubComp = $client->sendData($pubRel);
69
        $this->logger->debug('Created PubRel as response, got PubComp back', ['PubComp' => $pubComp]);
70
        return true;
71
    }
72
73
    /**
74
     * Will return an object of the type the broker has returned to us
75
     *
76
     * @param string $data
77
     * @param ClientInterface $client
78
     *
79
     * @return ReadableContentInterface
80
     * @throws \DomainException
81
     */
82
    public function expectAnswer(string $data, ClientInterface $client): ReadableContentInterface
83
    {
84
        $this->logger->info('String of incoming data confirmed, returning new object', ['callee' => \get_class($this)]);
85
86
        $eventManager = new EventManager($this->logger);
87
        $object = $eventManager->analyzeHeaders($data, $client);
88
        if ($object instanceof PubRel) {
89
90
        }
91
92
        return $object;
93
    }
94
95
}
96