Passed
Push — master ( 8634b9...ba407f )
by Chris
14:07
created

FrameDecoder::decodeFrame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 19
ccs 0
cts 15
cp 0
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\Header\Frame;
6
7
final class FrameDecoder
8
{
9
    public function decodeFrame(string $data): Frame
10
    {
11
        \assert(
12
            \strlen($data) === Frame::WIRE_SIZE,
13
            new \Error("Frame data length expected to be " . Frame::WIRE_SIZE . " bytes, got " . \strlen($data) . " bytes")
14
        );
15
16
        $parts = \unpack('vsize/vprotocol/Vsource', $data);
17
18
        $size = $parts['size'];
19
        $source = $parts['source'];
20
21
        $origin        =       ($parts['protocol'] & 0xC000) >> 14;
22
        $isTagged      = (bool)($parts['protocol'] & 0x2000);
23
        $isAddressable = (bool)($parts['protocol'] & 0x1000);
24
        $protocol      =       ($parts['protocol'] & 0x0FFF);
25
26
        return new Frame($size, $origin, $isTagged, $isAddressable, $protocol, $source);
27
    }
28
}
29