Passed
Push — master ( 0010e9...022bc2 )
by Thomas
11:25
created

TokenBinding   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 55.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 49
ccs 10
cts 18
cp 0.5556
rs 10
c 1
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 6
A hasId() 0 3 1
A getStatus() 0 3 1
A getId() 0 6 2
1
<?php
2
3
namespace MadWizard\WebAuthn\Dom;
4
5
use MadWizard\WebAuthn\Exception\NotAvailableException;
6
use MadWizard\WebAuthn\Exception\UnexpectedValueException;
7
use MadWizard\WebAuthn\Format\ByteBuffer;
8
9
final class TokenBinding
10
{
11
    /**
12
     * @var string
13
     */
14
    private $status;
15
16
    /**
17
     * @var ByteBuffer|null
18
     */
19
    private $id;
20
21 3
    public function __construct(string $status, ?ByteBuffer $id)
22
    {
23 3
        if (!TokenBindingStatus::isValidValue($status)) {
24 1
            throw new UnexpectedValueException(sprintf('Invalid token binding status "%s".', $status));
25
        }
26
27 2
        $this->status = $status;
28 2
        if ($this->status === TokenBindingStatus::PRESENT && $id === null) {
29
            throw new UnexpectedValueException("Token binding id should be set if status is 'present'.");
30
        }
31 2
        if ($this->status === TokenBindingStatus::SUPPORTED && $id !== null) {
32
            throw new UnexpectedValueException("Token binding id cannot be set if status is 'supported'.");
33
        }
34 2
        $this->id = $id;
35 2
    }
36
37 2
    public function getStatus(): string
38
    {
39 2
        return $this->status;
40
    }
41
42
    /**
43
     * @return ByteBuffer Returns the token binding ID
44
     *
45
     * @throws NotAvailableException
46
     */
47
    public function getId(): ByteBuffer
48
    {
49
        if ($this->id === null) {
50
            throw new NotAvailableException('No token binding ID available.');
51
        }
52
        return $this->id;
53
    }
54
55
    public function hasId(): bool
56
    {
57
        return $this->id !== null;
58
    }
59
}
60