Completed
Push — master ( d94a2b...d27c94 )
by Camilo
02:20
created

PacketIdentifier   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getPacketIdentifierValue() 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\Utilities;
8
9
/**
10
 * This Value Object will always contain a valid Packet Identifier
11
 */
12
final class PacketIdentifier
13
{
14
    /**
15
     * This field indicates the level of assurance for delivery of an Application Message. Can be 0, 1 or 2
16
     *
17
     * 0: At most once delivery (default)
18
     * 1: At least once delivery
19
     * 2: Exactly once delivery
20
     *
21
     * @var int
22
     */
23
    private $packetIdentifier;
24
25
    /**
26
     * QoSLevel constructor.
27
     *
28
     * @param int $packetIdentifier
29
     * @throws \InvalidArgumentException
30
     */
31
    public function __construct(int $packetIdentifier)
32
    {
33
        if ($packetIdentifier > 6535 || $packetIdentifier < 1) {
34
            throw new \InvalidArgumentException(sprintf(
35
                'The provided packet identifier is invalid. Valid values are 1-65535 (Provided: %d)',
36
                $packetIdentifier
37
            ));
38
        }
39
40
        $this->packetIdentifier = $packetIdentifier;
41
    }
42
43
    /**
44
     * Gets the current QoS level
45
     *
46
     * @return int
47
     */
48
    public function getPacketIdentifierValue(): int
49
    {
50
        return $this->packetIdentifier;
51
    }
52
53
    public function __toString(): string
54
    {
55
        return Utilities::convertNumberToBinaryString($this->packetIdentifier);
56
    }
57
}
58