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

FrameDecoder   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A decodeFrame() 0 19 1
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