Completed
Push — master ( d7d555...8d86ce )
by Thomas
06:51 queued 03:43
created

TrustDecisionManager::verifyTrust()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.048

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 17
ccs 8
cts 13
cp 0.6153
crap 8.048
rs 9.2222
1
<?php
2
3
namespace MadWizard\WebAuthn\Policy\Trust;
4
5
use MadWizard\WebAuthn\Attestation\TrustAnchor\MetadataInterface;
6
use MadWizard\WebAuthn\Exception\UntrustedException;
7
use MadWizard\WebAuthn\Exception\WebAuthnException;
8
use MadWizard\WebAuthn\Policy\Trust\Voter\TrustVoterInterface;
9
use MadWizard\WebAuthn\Server\Registration\RegistrationResultInterface;
10
11
final class TrustDecisionManager implements TrustDecisionManagerInterface
12
{
13
    /**
14
     * @var TrustVoterInterface[]
15
     */
16
    private $voters = [];
17
18 18
    public function addVoter(TrustVoterInterface $trustVoter): self
19
    {
20 18
        $this->voters[] = $trustVoter;
21 18
        return $this;
22
    }
23
24 1
    public function verifyTrust(RegistrationResultInterface $registrationResult, ?MetadataInterface $metadata): void
25
    {
26 1
        $trusted = false;
27 1
        $trustPath = $registrationResult->getVerificationResult()->getTrustPath();
28 1
        foreach ($this->voters as $voter) {
29 1
            $vote = $voter->voteOnTrust($registrationResult, $trustPath, $metadata);
30 1
            if ($vote->isTrusted()) {
31 1
                $trusted = true;
32
            } elseif ($vote->isUntrusted()) {
33
                throw UntrustedException::createWithReason($vote->getReason());
34
            } elseif (!$vote->isAbstain()) {
35
                throw new WebAuthnException('Unsupported vote type.');
36
            }
37
        }
38
39 1
        if (!$trusted) {
40
            throw UntrustedException::createWithReason('No voter trusted the registration.');
41
        }
42 1
    }
43
}
44