Completed
Push — master ( 7d448a...f2ba2f )
by Robbie
13s queued 10s
created

MethodRegistry::isBackupMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\BackupCode\Method;
9
use SilverStripe\MFA\Method\MethodInterface;
10
use UnexpectedValueException;
11
12
/**
13
 * A service class that holds the configuration for enabled MFA methods and facilitates providing these methods
14
 */
15
class MethodRegistry
16
{
17
    use Configurable;
18
    use Injectable;
19
20
    /**
21
     * List of configured MFA methods. These should be class names that implement MethodInterface
22
     *
23
     * @config
24
     * @var string[]
25
     */
26
    private static $methods = [];
0 ignored issues
show
introduced by
The private property $methods is not used, and could be removed.
Loading history...
27
28
    /**
29
     * A string referring to the classname of the method (implementing SilverStripe\MFA\Method\MethodInterface) that is
30
     * to be used as the back-up method for MFA. This alters the registration of this method to be required - a forced
31
     * registration once the user has registered at least one other method. Additionally it cannot be set as the default
32
     * method for a user to log in with.
33
     *
34
     * @config
35
     * @var string
36
     */
37
    private static $default_backup_method = Method::class;
0 ignored issues
show
introduced by
The private property $default_backup_method is not used, and could be removed.
Loading history...
38
39
    /**
40
     * Get implementations of all configured methods
41
     *
42
     * @return MethodInterface[]
43
     * @throws UnexpectedValueException When an invalid method is registered
44
     */
45
    public function getMethods()
46
    {
47
        $configuredMethods = (array) $this->config()->get('methods');
48
49
        $allMethods = [];
50
51
        foreach ($configuredMethods as $method) {
52
            $method = Injector::inst()->get($method);
53
54
            if (!$method instanceof MethodInterface) {
55
                throw new UnexpectedValueException(sprintf(
56
                    'Given method "%s" does not implement %s',
57
                    $method,
58
                    MethodInterface::class
59
                ));
60
            }
61
62
            $allMethods[] = $method;
63
        }
64
65
        return $allMethods;
66
    }
67
68
    /**
69
     * Helper method to indicate whether any MFA methods are registered
70
     *
71
     * @return bool
72
     */
73
    public function hasMethods()
74
    {
75
        return count($this->getMethods()) > 0;
76
    }
77
78
    /**
79
     * Indicates whether the given method is registered as the back-up method for MFA
80
     *
81
     * @param MethodInterface $method
82
     * @return bool
83
     */
84
    public function isBackupMethod(MethodInterface $method)
85
    {
86
        return is_a($method, $this->config()->get('default_backup_method'));
87
    }
88
89
    /**
90
     * Fetches a Method by its URL Segment
91
     *
92
     * @param string $segment
93
     * @return MethodInterface|null
94
     */
95
    public function getMethodByURLSegment($segment)
96
    {
97
        foreach ($this->getMethods() as $method) {
98
            if ($method->getURLSegment() === $segment) {
99
                return $method;
100
            }
101
        }
102
103
        return null;
104
    }
105
}
106