PublicKeyCredentialRpEntity   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 45
ccs 19
cts 19
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A fromRelyingParty() 0 5 1
A getId() 0 3 1
A getAsArray() 0 7 2
1
<?php
2
3
namespace MadWizard\WebAuthn\Dom;
4
5
use MadWizard\WebAuthn\Config\RelyingPartyInterface;
6
use MadWizard\WebAuthn\Exception\WebAuthnException;
7
use const FILTER_VALIDATE_DOMAIN;
8
9
final class PublicKeyCredentialRpEntity extends PublicKeyCredentialEntity
10
{
11
    /**
12
     * @var string|null
13
     */
14
    private $id;
15
16
    /**
17
     * PublicKeyCredentialRpEntity constructor.
18
     *
19
     * @param string|null $id Relying party ID (valid domain string)
20
     *
21
     * @throws WebAuthnException
22
     */
23 9
    public function __construct(string $name, ?string $id = null)
24
    {
25 9
        parent::__construct($name);
26 9
        if ($id !== null) {
27 3
            $id = filter_var($id, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
28 3
            if ($id === false) {
29 1
                throw new WebAuthnException('Invalid domain name');
30
            }
31 2
            $this->id = $id;
32
        }
33 8
    }
34
35 4
    public function getId(): ?string
36
    {
37 4
        return $this->id;
38
    }
39
40 7
    public function getAsArray(): array
41
    {
42 7
        $map = parent::getAsArray();
43 7
        if ($this->id !== null) {
44 2
            $map['id'] = $this->id;
45
        }
46 7
        return $map;
47
    }
48
49 3
    public static function fromRelyingParty(RelyingPartyInterface $rp): self
50
    {
51 3
        $rpEntity = new self($rp->getName(), $rp->getId());
52 3
        $rpEntity->setIcon($rp->getIconUrl());
53 3
        return $rpEntity;
54
    }
55
}
56