FrameDecoder   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A decodeFrame() 0 20 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\Frame;
7
8
final class FrameDecoder
9
{
10
    /**
11
     * @param string $data
12
     * @param int $offset
13
     * @return Frame
14
     * @throws InsufficientDataException
15
     */
16 19
    public function decodeFrame(string $data, int $offset = 0): Frame
17
    {
18 19
        if ((\strlen($data) - $offset) < Frame::WIRE_SIZE) {
19 2
            throw new InsufficientDataException(
20 2
                "Frame requires " . Frame::WIRE_SIZE . " bytes, got " . (\strlen($data) - $offset) . " bytes"
21
            );
22
        }
23
24
        [
25 17
            'size' => $size,
26 17
            'protocol' => $protocol,
27 17
            'source' => $source,
28 17
        ] = \unpack('vsize/vprotocol/Vsource', $data, $offset);
29
30 17
        $origin        =       ($protocol & 0xC000) >> 14;
31 17
        $isTagged      = (bool)($protocol & 0x2000);
32 17
        $isAddressable = (bool)($protocol & 0x1000);
33 17
        $protocol      =       ($protocol & 0x0FFF);
34
35 17
        return new Frame($size, $origin, $isTagged, $isAddressable, $protocol, $source);
36
    }
37
}
38