Completed
Push — master ( 8c9d7d...4ca550 )
by Camilo
03:44
created

getProtocolVersionBinaryRepresentation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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