Passed
Push — master ( 83aa3d...907404 )
by Thomas
07:17
created

BundledSource   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 68
ccs 0
cts 25
cp 0
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createProviders() 0 10 3
B __construct() 0 24 8
A getEnableSets() 0 3 1
1
<?php
2
3
namespace MadWizard\WebAuthn\Metadata\Source;
4
5
use MadWizard\WebAuthn\Metadata\Provider\Apple\AppleDevicesProvider;
6
use MadWizard\WebAuthn\Metadata\Provider\MetadataProviderInterface;
7
use UnexpectedValueException;
8
9
final class BundledSource implements MetadataSourceInterface
10
{
11
    private const SETS =
12
        [
13
            'apple' => false,
14
            // 'yubico-u2f' => true,
15
        ];
16
17
    /**
18
     * @var string[]
19
     * @phpstan-var array<string, class-string>
20
     */
21
    private const PROVIDERS = [
22
        'apple' => AppleDevicesProvider::class,
23
    ];
24
25
    /**
26
     * @var array<string, bool>
27
     */
28
    private $enabledSets;
29
30
    public function __construct(array $sets = ['@all'])
31
    {
32
        $this->enabledSets = self::SETS;
33
        foreach ($sets as $set) {
34
            if ($set === '') {
35
                throw new UnexpectedValueException('Empty set name');
36
            }
37
38
            if ($set === '@all') {
39
                $this->enabledSets = array_fill_keys(array_keys(self::SETS), true);
40
            } else {
41
                $add = true;
42
                if ($set[0] === '-') {
43
                    $set = substr($set, 1);
44
                    $add = false;
45
                }
46
47
                if (!isset(self::SETS[$set])) {
48
                    throw new UnexpectedValueException(sprintf("Invalid set name '%s'.", $set));
49
                }
50
                if ($add) {
51
                    $this->enabledSets[$set] = true;
52
                } elseif (isset($this->enabledSets[$set])) {
53
                    $this->enabledSets[$set] = false;
54
                }
55
            }
56
        }
57
    }
58
59
    public function getEnableSets(): array
60
    {
61
        return array_keys(array_filter($this->enabledSets, static function ($value) { return $value; }));
62
    }
63
64
    /**
65
     * @return MetadataProviderInterface[]
66
     */
67
    public function createProviders(): array
68
    {
69
        $providers = [];
70
        foreach ($this->enabledSets as $type => $enabled) {
71
            if ($enabled) {
72
                $className = self::PROVIDERS[$type];
73
                $providers[] = new $className();
74
            }
75
        }
76
        return $providers;
77
    }
78
}
79