Completed
Push — master ( ee2373...67eeeb )
by Chris
02:26
created

ProtocolHeaderDecoder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A decodeProtocolHeader() 0 10 2
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan\Decoding;
4
5
use DaveRandom\LibLifxLan\Decoding\Exceptions\InsufficientDataException;
6
use DaveRandom\LibLifxLan\Header\ProtocolHeader;
7
8
final class ProtocolHeaderDecoder
9
{
10
    /**
11
     * @param string $data
12
     * @param int $offset
13
     * @return ProtocolHeader
14
     * @throws InsufficientDataException
15
     */
16 3
    public function decodeProtocolHeader(string $data, int $offset = 0): ProtocolHeader
17
    {
18 3
        if ((\strlen($data) - $offset) < ProtocolHeader::WIRE_SIZE) {
19 1
            throw new InsufficientDataException(
20 1
                "Protocol header requires " . ProtocolHeader::WIRE_SIZE
21 1
                . " bytes, got " . (\strlen($data) - $offset) . " bytes"
22
            );
23
        }
24
25 2
        return new ProtocolHeader(\unpack('Preserved/vtype/vreserved', $data, $offset)['type']);
26
    }
27
}
28