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

MethodRegistry::getAllMethods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 21
rs 9.9
c 0
b 0
f 0
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