Passed
Pull Request — master (#18)
by Garion
01:49
created

MethodRegistry::getMethodByURLSegment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\MFA\Service;
4
5
use LogicException;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Core\Injector\Injectable;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\MFA\Extension\MemberExtension;
10
use SilverStripe\MFA\Method\MethodInterface;
11
use SilverStripe\MFA\Model\RegisteredMethod;
12
use SilverStripe\Security\Member;
13
use UnexpectedValueException;
14
15
/**
16
 * A service class that holds the configuration for enabled MFA methods and facilitates providing these methods
17
 */
18
class MethodRegistry
19
{
20
    use Configurable;
21
    use Injectable;
22
23
    /**
24
     * List of configured MFA methods. These should be class names that implement MethodInterface
25
     *
26
     * @config
27
     * @var array
28
     */
29
    private static $methods;
0 ignored issues
show
introduced by
The private property $methods is not used, and could be removed.
Loading history...
30
31
    /**
32
     * Get implementations of all configured methods
33
     *
34
     * @return MethodInterface[]
35
     * @throws UnexpectedValueException When an invalid method is registered
36
     */
37
    public function getMethods()
38
    {
39
        $configuredMethods = (array) static::config()->get('methods');
40
41
        $allMethods = [];
42
43
        foreach ($configuredMethods as $method) {
44
            $method = Injector::inst()->get($method);
45
46
            if (!$method instanceof MethodInterface) {
47
                throw new UnexpectedValueException(sprintf(
48
                    'Given method "%s" does not implement %s',
49
                    $method,
50
                    MethodInterface::class
51
                ));
52
            }
53
54
            $allMethods[] = $method;
55
        }
56
57
        return $allMethods;
58
    }
59
60
    /**
61
     * Helper method to indicate whether any MFA methods are registered
62
     *
63
     * @return bool
64
     */
65
    public function hasMethods()
66
    {
67
        return count($this->getMethods()) > 0;
68
    }
69
70
    /**
71
     * Get an authentication method object matching the given method from the given member. Returns null if the given
72
     * method could not be found attached to the Member
73
     *
74
     * @param Member|MemberExtension $member
75
     * @param string $specifiedMethod The class name of the requested method
76
     * @return RegisteredMethod|null
77
     */
78
    public function getMethodFromMember(Member $member, $specifiedMethod)
79
    {
80
        $method = null;
81
82
        // Find the actual method registration data object from the member for the specified default authenticator
83
        foreach ($member->RegisteredMFAMethods() as $candidate) {
84
            if ($candidate->MethodClassName === $specifiedMethod) {
85
                $method = $candidate;
86
                break;
87
            }
88
        }
89
90
        return $method;
91
    }
92
93
    /**
94
     * Fetches a Method by its URL Segment
95
     *
96
     * @param string $segment
97
     * @return MethodInterface|null
98
     */
99
    public function getMethodByURLSegment($segment)
100
    {
101
        foreach ($this->getMethods() as $method) {
102
            if ($method->getURLSegment() === $segment) {
103
                return $method;
104
            }
105
        }
106
107
        return null;
108
    }
109
}
110