ProtocolHeaderDecoder::decodeProtocolHeader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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 11
    public function decodeProtocolHeader(string $data, int $offset = 0): ProtocolHeader
17
    {
18 11
        if ((\strlen($data) - $offset) < ProtocolHeader::WIRE_SIZE) {
19 2
            throw new InsufficientDataException(
20 2
                "Protocol header requires " . ProtocolHeader::WIRE_SIZE
21 2
                . " bytes, got " . (\strlen($data) - $offset) . " bytes"
22
            );
23
        }
24
25 9
        return new ProtocolHeader(\unpack('Preserved/vtype/vreserved', $data, $offset)['type']);
26
    }
27
}
28