Completed
Push — master ( 2e892a...99440d )
by Chris
04:45
created

PacketDecoder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B decodePacket() 0 27 3
A getHeaderDecoder() 0 3 1
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
    public function __construct(HeaderDecoder $headerDecoder = null, MessageDecoder $messageDecoder = null)
20
    {
21 16
        $this->headerDecoder = $headerDecoder ?? new HeaderDecoder;
22 16
        $this->messageDecoder = $messageDecoder ?? new MessageDecoder;
23
    }
24
25 2
    public function getHeaderDecoder(): HeaderDecoder
26
    {
27 2
        return $this->headerDecoder;
28
    }
29
30 2
    public function getMessageDecoder(): MessageDecoder
31
    {
32 2
        return $this->messageDecoder;
33
    }
34
35
    /**
36
     * @param string $buffer
37
     * @param int $offset
38
     * @param int|null $length
39
     * @return Packet
40
     * @throws DecodingException
41
     * @throws InsufficientDataException
42
     * @throws InvalidMessagePayloadLengthException
43
     */
44 12
    public function decodePacket(string $buffer, int $offset = 0, int $length = null): Packet
45
    {
46 12
        $dataLength = $length ?? (\strlen($buffer) - $offset);
47
48 12
        if ($dataLength < Header::WIRE_SIZE) {
49 4
            throw new InsufficientDataException(
50 4
                "Data length {$dataLength} less than minimum packet size " . Header::WIRE_SIZE
51
            );
52
        }
53
54 8
        $statedLength = \unpack('vlength', $buffer, $offset)['length'];
55
56 8
        if ($statedLength !== $dataLength) {
57 4
            throw new InvalidMessagePayloadLengthException(
58 4
                "Packet length is stated to be {$statedLength} bytes, buffer is {$dataLength} bytes"
59
            );
60
        }
61
62 4
        $header = $this->headerDecoder->decodeHeader($buffer, $offset + self::HEADER_OFFSET);
63 4
        $payload = $this->messageDecoder->decodeMessage(
64 4
            $header->getProtocolHeader()->getType(),
65 4
            $buffer,
66 4
            $offset + self::MESSAGE_OFFSET,
67 4
            $dataLength - Header::WIRE_SIZE
68
        );
69
70 4
        return new Packet($header, $payload);
71
    }
72
}
73