Completed
Push — master ( 569d13...84f4c4 )
by Camilo
17s queued 10s
created

WritableContent::getRemainingLength()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
c 0
b 0
f 0
ccs 10
cts 12
cp 0.8333
rs 9.8666
cc 4
nc 3
nop 1
crap 4.074

1 Method

Rating   Name   Duplication   Size   Complexity  
A WritableContent::createSendableMessage() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
use DomainException;
8
use OutOfRangeException;
9
use Psr\Log\LoggerInterface;
10
use unreal4u\MQTT\Exceptions\MessageTooBig;
11
use unreal4u\MQTT\Utilities;
12
use function chr;
13
use function get_class;
14
use function strlen;
15
16
/**
17
 * Trait WritableContent
18
 * @package unreal4u\MQTT\Internals
19
 */
20
trait WritableContent
21
{
22
    /**
23
     * @var LoggerInterface
24
     */
25
    protected $logger;
26
27
    /**
28
     * Any special flags that are set on runtime
29
     *
30
     * PUBLISH for example needs to know QoS, the retain bit and duplicate delivery settings
31
     * PUBREL, SUBSCRIBE and UNSUBSCRIBE has always bit 1 set to true
32
     *
33
     * @var int
34
     */
35
    protected $specialFlags = 0;
36
37
    /**
38
     * Returns the fixed header part needed for all methods
39
     *
40
     * This takes into account the basic control packet value, any special flags and, in the second byte, the variable
41
     * header length
42
     *
43
     * @param int $variableHeaderLength
44
     * @return string
45
     * @throws MessageTooBig
46
     */
47 3
    final public function createFixedHeader(int $variableHeaderLength): string
48
    {
49 3
        $this->logger->debug('Creating fixed header with values', [
50 3
            'controlPacketValue' => self::getControlPacketValue(),
51 3
            'specialFlags' => $this->specialFlags,
52 3
            'variableHeaderLength' => $variableHeaderLength,
53 3
            'composed' => decbin(chr((self::getControlPacketValue() << 4) | $this->specialFlags)),
0 ignored issues
show
Bug introduced by
chr(self::getControlPack... | $this->specialFlags) of type string is incompatible with the type integer expected by parameter $number of decbin(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            'composed' => decbin(/** @scrutinizer ignore-type */ chr((self::getControlPacketValue() << 4) | $this->specialFlags)),
Loading history...
54
        ]);
55
56
        // Binary OR is safe to do because the first 4 bits are always 0 after shifting
57
        return
58 3
            chr((self::getControlPacketValue() << 4) | $this->specialFlags) .
59 3
            Utilities::formatRemainingLengthOutput($variableHeaderLength);
60
    }
61
62
    /**
63
     * Creates the entire message
64
     * @return string
65
     * @throws MessageTooBig
66
     */
67 2
    final public function createSendableMessage(): string
68
    {
69 2
        $variableHeader = $this->createVariableHeader();
70 2
        $this->logger->debug('Created variable header', ['variableHeader' => base64_encode($variableHeader)]);
71 2
        $payload = $this->createPayload();
72 2
        $this->logger->debug('Created payload', ['payload' => base64_encode($payload)]);
73 2
        $fixedHeader = $this->createFixedHeader(strlen($variableHeader . $payload));
74 2
        $this->logger->debug('Created fixed header', ['fixedHeader' => base64_encode($fixedHeader)]);
75
76 2
        return $fixedHeader . $variableHeader . $payload;
77
    }
78
79
    /**
80
     * Creates the variable header that each method has
81
     *
82
     * @return string
83
     */
84
    abstract public function createVariableHeader(): string;
85
86
    /**
87
     * Creates the actual payload to be sent
88
     *
89
     * @return string
90
     */
91
    abstract public function createPayload(): string;
92
93
    /**
94
     * Creates a UTF8 big-endian representation of the given string
95
     *
96
     * @param string $nonFormattedString
97
     * @return string
98
     * @throws OutOfRangeException
99
     */
100 14
    final public function createUTF8String(string $nonFormattedString): string
101
    {
102 14
        $returnString = '';
103 14
        if ($nonFormattedString !== '') {
104 13
            $returnString = Utilities::convertNumberToBinaryString(strlen($nonFormattedString)) . $nonFormattedString;
105
        }
106
107 14
        return $returnString;
108
    }
109
110
    /**
111
     * Will return an object of the type the broker has returned to us
112
     *
113
     * @param string $brokerBitStream
114
     * @param ClientInterface $client
115
     *
116
     * @return ReadableContentInterface
117
     * @throws DomainException
118
     */
119
    public function expectAnswer(string $brokerBitStream, ClientInterface $client): ReadableContentInterface
120
    {
121
        $this->logger->info('String of incoming data confirmed, returning new object', ['callee' => get_class($this)]);
122
123
        $eventManager = new EventManager($this->logger);
124
        return $eventManager->analyzeHeaders($brokerBitStream, $client);
125
    }
126
127
    /**
128
     * Gets the control packet value for this object
129
     *
130
     * @return int
131
     */
132 11
    final public static function getControlPacketValue(): int
133
    {
134 11
        return static::CONTROL_PACKET_VALUE;
0 ignored issues
show
Bug introduced by
The constant unreal4u\MQTT\Internals\...t::CONTROL_PACKET_VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
135
    }
136
}
137