Aaguid::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
namespace MadWizard\WebAuthn\Attestation\Identifier;
4
5
use MadWizard\WebAuthn\Exception\ParseException;
6
use MadWizard\WebAuthn\Format\ByteBuffer;
7
8
final class Aaguid implements IdentifierInterface
9
{
10
    public const TYPE = 'aaguid';
11
12
    public const AAGUID_LENGTH = 16;
13
14
    /**
15
     * @var ByteBuffer
16
     */
17
    private $raw;
18
19 19
    public function __construct(ByteBuffer $raw)
20
    {
21 19
        if ($raw->getLength() !== self::AAGUID_LENGTH) {
22 2
            throw new ParseException(sprintf('AAGUID should be %d bytes, not %d', self::AAGUID_LENGTH, $raw->getLength()));
23
        }
24 17
        $this->raw = $raw;
25 17
    }
26
27
    public function getType(): string
28
    {
29
        return self::TYPE;
30
    }
31
32 5
    public static function parseString(string $aaguid): self
33
    {
34 5
        if (!preg_match('~^[0-9A-Fa-f]{8}(-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12}$~', $aaguid)) {
35 1
            throw new ParseException('Invalid AAGUID');
36
        }
37 4
        $hex = substr($aaguid, 0, 8) .
38 4
            substr($aaguid, 9, 4) .
39 4
            substr($aaguid, 14, 4) .
40 4
            substr($aaguid, 19, 4) .
41 4
            substr($aaguid, 24, 12);
42
43 4
        return new Aaguid(ByteBuffer::fromHex($hex));
44
    }
45
46 3
    public function toString(): string
47
    {
48 3
        $hex = $this->raw->getHex();
49 3
        return sprintf(
50 3
            '%s-%s-%s-%s-%s',
51 3
            substr($hex, 0, 8),
52 3
            substr($hex, 8, 4),
53 3
            substr($hex, 12, 4),
54 3
            substr($hex, 16, 4),
55 3
            substr($hex, 20, 12)
56
        );
57
    }
58
59 1
    public function getHex(): string
60
    {
61 1
        return $this->raw->getHex();
62
    }
63
64 5
    public function isZeroAaguid(): bool
65
    {
66
        // Check if all zero bytes - U2F authenticators use this to indicate they have no AAGUID
67 5
        return strspn($this->raw->getBinaryString(), "\0") === self::AAGUID_LENGTH;
68
    }
69
70 5
    public function equals(IdentifierInterface $identifier): bool
71
    {
72 5
        return $identifier instanceof self && $this->raw->equals($identifier->raw);
73
    }
74
}
75