PublicKeyCredentialUserEntity::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace MadWizard\WebAuthn\Dom;
4
5
use MadWizard\WebAuthn\Credential\UserHandle;
6
use MadWizard\WebAuthn\Exception\WebAuthnException;
7
use MadWizard\WebAuthn\Format\ByteBuffer;
8
9
final class PublicKeyCredentialUserEntity extends PublicKeyCredentialEntity
10
{
11
    /**
12
     * @var ByteBuffer Binary user handle of the account (max MAX_USER_HANDLE_BYTES)
13
     */
14
    private $id;
15
16
    /**
17
     * @var string The user handle of the user account entity
18
     */
19
    private $displayName;
20
21
    /**
22
     * PublicKeyCredentialUserEntity constructor.
23
     *
24
     * @param ByteBuffer $id          The user handle of the user account entity (max length MAX_USER_HANDLE_BYTES)
25
     * @param string     $displayName A human-friendly name for the user account. Used for display purposes only, may be truncated by authenticators if too long
26
     *
27
     * @throws WebAuthnException
28
     */
29 7
    public function __construct(string $name, ByteBuffer $id, string $displayName)
30
    {
31 7
        parent::__construct($name);
32 7
        $this->setId($id);
33 5
        $this->displayName = $displayName;
34 5
    }
35
36 2
    public function getId(): ByteBuffer
37
    {
38 2
        return $this->id;
39
    }
40
41 7
    private function setId(ByteBuffer $id): void
42
    {
43 7
        if ($id->isEmpty()) {
44 1
            throw new WebAuthnException('User handle cannot be empty.');
45
        }
46
47 6
        if ($id->getLength() > UserHandle::MAX_USER_HANDLE_BYTES) {
48 1
            throw new WebAuthnException(sprintf('User handle cannot be larger than %d bytes.', UserHandle::MAX_USER_HANDLE_BYTES));
49
        }
50 5
        $this->id = $id;
51 5
    }
52
53 2
    public function getDisplayName(): string
54
    {
55 2
        return $this->displayName;
56
    }
57
58 3
    public function getAsArray(): array
59
    {
60 3
        return array_merge(
61 3
            parent::getAsArray(),
62
            [
63 3
                'id' => $this->id,
64 3
                'displayName' => $this->displayName,
65
            ]
66
        );
67
    }
68
}
69