Test Failed
Push — master ( 816aa4...81bc91 )
by Chris
02:52
created

FrameAddressDecoder   A

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 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A decodeFrameAddress() 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\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 15
    public function decodeFrameAddress(string $data, int $offset = 0): FrameAddress
18
    {
19 15
        if ((\strlen($data) - $offset) < FrameAddress::WIRE_SIZE) {
20 2
            throw new InsufficientDataException(
21 2
                "Frame address requires " . FrameAddress::WIRE_SIZE
22 2
                . " bytes, got " . (\strlen($data) - $offset) . " bytes"
23
            );
24
        }
25
26
        [
27 13
            'mac1' => $mac1, 'mac2' => $mac2, 'mac3' => $mac3, 'mac4' => $mac4, 'mac5' => $mac5, 'mac6' => $mac6,
28 13
            'flags' => $flags,
29 13
            'sequence' => $sequence,
30 13
        ] = \unpack('C8mac/C6reserved/Cflags/Csequence', $data, $offset);
31 13
32 13
        $target = new MacAddress($mac1, $mac2, $mac3, $mac4, $mac5, $mac6);
33 13
        $isAckRequired = (bool)($flags & 0x02);
34 13
        $isResponseRequired = (bool)($flags & 0x01);
35 13
36
        return new FrameAddress($target, $isAckRequired, $isResponseRequired, $sequence);
37 13
    }
38
}
39