Test Failed
Push — master ( 816aa4...81bc91 )
by Chris
02:52
created

PacketDecoder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A decodePacket() 0 13 1
A getHeaderDecoder() 0 3 1
A getValidDataLength() 0 19 3
A __construct() 0 4 1
A getMessageDecoder() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan\Decoding;
4
5
use DaveRandom\LibLifxLan\Decoding\Exceptions\DecodingException;
6
use DaveRandom\LibLifxLan\Decoding\Exceptions\InsufficientDataException;
7
use DaveRandom\LibLifxLan\Decoding\Exceptions\InvalidMessagePayloadLengthException;
8
use DaveRandom\LibLifxLan\Header\Header;
9
use DaveRandom\LibLifxLan\Packet;
10
11
final class PacketDecoder
12
{
13
    private const HEADER_OFFSET = 0;
14
    private const MESSAGE_OFFSET = Header::WIRE_SIZE;
15
16
    private $headerDecoder;
17
    private $messageDecoder;
18
19 16
    /**
20
     * @param string $buffer
21 16
     * @param int $offset
22 16
     * @param int $length
23
     * @return int
24
     * @throws InsufficientDataException
25 2
     * @throws InvalidMessagePayloadLengthException
26
     */
27 2
    private function getValidDataLength(string $buffer, int $offset, int $length): int
28
    {
29
        $dataLength = $length ?? (\strlen($buffer) - $offset);
30 2
31
        if ($dataLength < Header::WIRE_SIZE) {
32 2
            throw new InsufficientDataException(
33
                "Data length {$dataLength} less than minimum packet size " . Header::WIRE_SIZE
34
            );
35
        }
36
37
        $statedLength = \unpack('vlength', $buffer, $offset)['length'];
38
39
        if ($statedLength !== $dataLength) {
40
            throw new InvalidMessagePayloadLengthException(
41
                "Packet length is stated to be {$statedLength} bytes, buffer is {$dataLength} bytes"
42
            );
43
        }
44 12
45
        return $statedLength;
46 12
    }
47
48 12
    public function __construct(HeaderDecoder $headerDecoder = null, MessageDecoder $messageDecoder = null)
49 4
    {
50 4
        $this->headerDecoder = $headerDecoder ?? new HeaderDecoder;
51
        $this->messageDecoder = $messageDecoder ?? new MessageDecoder;
52
    }
53
54 8
    public function getHeaderDecoder(): HeaderDecoder
55
    {
56 8
        return $this->headerDecoder;
57 4
    }
58 4
59
    public function getMessageDecoder(): MessageDecoder
60
    {
61
        return $this->messageDecoder;
62 4
    }
63 4
64 4
    /**
65 4
     * @param string $buffer
66 4
     * @param int $offset
67 4
     * @param int|null $length
68
     * @return Packet
69
     * @throws DecodingException
70 4
     * @throws InsufficientDataException
71
     * @throws InvalidMessagePayloadLengthException
72
     */
73
    public function decodePacket(string $buffer, int $offset = 0, int $length = null): Packet
74
    {
75
        $dataLength = $this->getValidDataLength($buffer, $offset, $length);
0 ignored issues
show
Bug introduced by
It seems like $length can also be of type null; however, parameter $length of DaveRandom\LibLifxLan\De...r::getValidDataLength() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

75
        $dataLength = $this->getValidDataLength($buffer, $offset, /** @scrutinizer ignore-type */ $length);
Loading history...
76
77
        $header = $this->headerDecoder->decodeHeader($buffer, $offset + self::HEADER_OFFSET);
78
        $payload = $this->messageDecoder->decodeMessage(
79
            $header->getProtocolHeader()->getType(),
80
            $buffer,
81
            $offset + self::MESSAGE_OFFSET,
82
            $dataLength - Header::WIRE_SIZE
83
        );
84
85
        return new Packet($header, $payload);
86
    }
87
}
88