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

ProtocolVersion   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 50
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getProtocolVersionBinaryRepresentation() 0 8 2
A __construct() 0 6 2
A getProtocolVersion() 0 3 1
A __toString() 0 3 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
/**
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