PublicKeyCredentialUserEntity   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 56
ccs 21
cts 21
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAsArray() 0 7 1
A setId() 0 10 3
A getDisplayName() 0 3 1
A __construct() 0 5 1
A getId() 0 3 1
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