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

FrameAddressDecoder::decodeFrameAddress()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 2
dl 0
loc 25
ccs 0
cts 7
cp 0
crap 6
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
    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