FileProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 45
ccs 18
cts 20
cp 0.9
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A __construct() 0 3 1
A getMetadata() 0 28 6
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