Passed
Push — master ( 540fca...898f33 )
by Chris
02:35
created

FrameAddressDecoder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B decodeFrameAddress() 0 25 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\FrameAddress;
7
use DaveRandom\Network\MacAddress;
8
9
final class FrameAddressDecoder
10
{
11
    /**
12
     * @param string $data
13
     * @param int $offset
14
     * @return FrameAddress
15
     * @throws InsufficientDataException
16
     */
17
    public function decodeFrameAddress(string $data, int $offset = 0): FrameAddress
18
    {
19
        if ((\strlen($data) - $offset) < FrameAddress::WIRE_SIZE) {
20
            throw new InsufficientDataException(
21
                "Frame address requires " . FrameAddress::WIRE_SIZE
22
                . " bytes, got " . (\strlen($data) - $offset) . " bytes"
23
            );
24
        }
25
26
        [
27
            'mac1' => $mac1,
28
            'mac2' => $mac2,
29
            'mac3' => $mac3,
30
            'mac4' => $mac4,
31
            'mac5' => $mac5,
32
            'mac6' => $mac6,
33
            'flags' => $flags,
34
            'sequence' => $sequence,
35
        ] = \unpack('C8mac/C6reserved/Cflags/Csequence', $data, $offset);
36
37
        $target = new MacAddress($mac1, $mac2, $mac3, $mac4, $mac5, $mac6);
38
        $isAckRequired      = (bool)($flags & 0x02);
39
        $isResponseRequired = (bool)($flags & 0x01);
40
41
        return new FrameAddress($target, $isAckRequired, $isResponseRequired, $sequence);
42
    }
43
}
44