PublicKeyCredentialRpEntity::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 3
rs 10
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