Passed
Push — master ( 2fc57b...1ebcaf )
by Thomas
02:57
created

FileProvider::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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