Completed
Push — master ( 67eeeb...b488c0 )
by Chris
04:41
created

FrameAddressDecoder::decodeFrameAddress()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 2
dl 0
loc 25
ccs 18
cts 18
cp 1
crap 2
rs 8.8571
c 0
b 0
f 0
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 8
    public function decodeFrameAddress(string $data, int $offset = 0): FrameAddress
18
    {
19 8
        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 6
            'mac1' => $mac1,
28 6
            'mac2' => $mac2,
29 6
            'mac3' => $mac3,
30 6
            'mac4' => $mac4,
31 6
            'mac5' => $mac5,
32 6
            'mac6' => $mac6,
33 6
            'flags' => $flags,
34 6
            'sequence' => $sequence,
35 6
        ] = \unpack('C8mac/C6reserved/Cflags/Csequence', $data, $offset);
36
37 6
        $target = new MacAddress($mac1, $mac2, $mac3, $mac4, $mac5, $mac6);
38 6
        $isAckRequired      = (bool)($flags & 0x02);
39 6
        $isResponseRequired = (bool)($flags & 0x01);
40
41 6
        return new FrameAddress($target, $isAckRequired, $isResponseRequired, $sequence);
42
    }
43
}
44