ProtocolVersion::getProtocolVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\DataTypes;
6
7
use unreal4u\MQTT\Exceptions\Connect\UnacceptableProtocolVersion;
8
9
use function chr;
10
use function in_array;
11
12
/**
13
 * This Value Object will always contain a valid protocol version.
14
 */
15
final class ProtocolVersion
16
{
17
    /**
18
     * Holds the current protocol version
19
     *
20
     * @var string
21
     */
22
    private $protocolVersion;
23
24
    private const SUPPORTED_PROTOCOL_VERSIONS = [
25
        '3.1.1'
26
    ];
27
28
    /**
29
     * QoSLevel constructor.
30
     *
31
     * @param string $protocolVersion
32
     * @throws UnacceptableProtocolVersion
33
     */
34 25
    public function __construct(string $protocolVersion)
35
    {
36 25
        if (in_array($protocolVersion, self::SUPPORTED_PROTOCOL_VERSIONS, true) === false) {
37 1
            throw new UnacceptableProtocolVersion('The specified protocol is invalid');
38
        }
39 24
        $this->protocolVersion = $protocolVersion;
40 24
    }
41
42
    /**
43
     * Gets the current protocol version
44
     *
45
     * @return string
46
     */
47 2
    public function getProtocolVersion(): string
48
    {
49 2
        return $this->protocolVersion;
50
    }
51
52
    /**
53
     * Will return the correct connection identifier for the current protocol
54
     *
55
     * @return string
56
     */
57 4
    public function getProtocolVersionBinaryRepresentation(): string
58
    {
59 4
        if ($this->protocolVersion === '3.1.1') {
60
            // Protocol v3.1.1 must return a 4
61 3
            return chr(4);
62
        }
63
64
        // Return a default of 0, which will be invalid anyway (but data will be sent to the broker this way)
65 1
        return chr(0);
66
    }
67
68 1
    public function __toString(): string
69
    {
70 1
        return $this->getProtocolVersion();
71
    }
72
}
73