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

MethodRegistry::getAllMethods()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 29
rs 9.7666
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
 *
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
40
            $method = Injector::inst()->get($method);
41
42
            if (!$method) {
43
                throw new UnexpectedValueException(sprintf(
44
                    'Given method "%s" does not exist/can not be resolved with Injector',
45
                    $method
46
                ));
47
            }
48
49
            if (!$method instanceof MethodInterface) {
50
                throw new UnexpectedValueException(sprintf(
51
                    'Given method "%s" does not implement %s',
52
                    $method,
53
                    MethodInterface::class
54
                ));
55
            }
56
57
            $allMethods[] = $method;
58
        }
59
60
        return $allMethods;
61
    }
62
}
63