Passed
Pull Request — master (#10)
by
unknown
01:49
created

MethodRegistry   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAllMethods() 0 21 3
1
<?php
2
3
namespace SilverStripe\MFA\Service;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\MFA\Method\MethodInterface;
9
use UnexpectedValueException;
10
11
/**
12
 * A service class that holds the configuration for enabled MFA methods and facilitates providing these methods
13
 */
14
class MethodRegistry
15
{
16
    use Configurable;
17
    use Injectable;
18
19
    /**
20
     * List of configured MFA methods. These should be class names that implement MethodInterface
21
     *
22
     * @config
23
     * @var array
24
     */
25
    private static $methods;
0 ignored issues
show
introduced by
The private property $methods is not used, and could be removed.
Loading history...
26
27
    /**
28
     * Get implementations of all configured methods
29
     *
30
     * @return MethodInterface[]
31
     */
32
    public function getAllMethods()
33
    {
34
        $configuredMethods = (array) static::config()->get('methods');
35
36
        $allMethods = [];
37
38
        foreach ($configuredMethods as $method) {
39
            $method = Injector::inst()->get($method);
40
41
            if (!$method instanceof MethodInterface) {
42
                throw new UnexpectedValueException(sprintf(
43
                    'Given method "%s" does not implement %s',
44
                    $method,
45
                    MethodInterface::class
46
                ));
47
            }
48
49
            $allMethods[] = $method;
50
        }
51
52
        return $allMethods;
53
    }
54
}
55