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

FileProvider::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MadWizard\WebAuthn\Metadata\Provider;
4
5
use GlobIterator;
6
use MadWizard\WebAuthn\Attestation\Identifier\IdentifierInterface;
7
use MadWizard\WebAuthn\Attestation\TrustAnchor\MetadataInterface;
8
use MadWizard\WebAuthn\Exception\WebAuthnException;
9
use MadWizard\WebAuthn\Metadata\Source\StatementDirectorySource;
10
use MadWizard\WebAuthn\Metadata\Statement\MetadataStatement;
11
use MadWizard\WebAuthn\Server\Registration\RegistrationResultInterface;
12
use SplFileInfo;
13
14
final class FileProvider implements MetadataProviderInterface
15
{
16
    /**
17
     * @var StatementDirectorySource
18
     */
19
    private $source;
20
21
    public function __construct(StatementDirectorySource $source)
22
    {
23
        $this->source = $source;
24
    }
25
26
    public function getMetadata(IdentifierInterface $identifier, RegistrationResultInterface $registrationResult): ?MetadataInterface
27
    {
28
        $iterator = new GlobIterator($this->source->getMetadataDir() . DIRECTORY_SEPARATOR . '*.json');
29
30
        /**
31
         * @var SplFileInfo $fileInfo
32
         */
33
        foreach ($iterator as $fileInfo) {
34
            if (!$fileInfo->isFile()) {
35
                continue;
36
            }
37
38
            $data = file_get_contents($fileInfo->getPathname());
39
            if ($data === false) {
40
                throw new WebAuthnException(sprintf('Cannot read file %s.', $fileInfo->getPathname()));
41
            }
42
            $statement = MetadataStatement::decodeString($data);
43
44
            if ($statement->matchesIdentifier($identifier)) {
45
                return $statement;
46
            }
47
        }
48
        return null;
49
    }
50
51
    public function getDescription(): string
52
    {
53
        return sprintf('Metadata files directory=%s', $this->source->getMetadataDir());
54
    }
55
}
56