Completed
Push — master ( bade18...269a91 )
by Robbie
11s
created

AuthenticationMethod::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace SilverStripe\MFA\Model;
3
4
use SilverStripe\Core\Injector\Injector;
5
use SilverStripe\MFA\AuthenticationMethod\AuthenticatorInterface;
6
use SilverStripe\MFA\AuthenticationMethodInterface;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Member;
9
10
/**
11
 * @package SilverStripe\MFA\Model
12
 *
13
 * @property string MethodClassName
14
 * @property array Data
15
 */
16
class AuthenticationMethod extends DataObject
17
{
18
    private static $table_name = 'MFAAuthenticationMethod';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
19
20
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
21
        // The class name of the AuthenticationMethodInterface that this record refers to
22
        'MethodClassName' => 'Varchar',
23
        // Data stored as a JSON blob that may contain detail specific to this registration of the authenticator
24
        'Data' => 'Text',
25
    ];
26
27
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
28
        'Member' => Member::class,
29
    ];
30
31
    /**
32
     * @var AuthenticationMethodInterface
33
     */
34
    protected $method;
35
36
    /**
37
     * @return AuthenticatorInterface
38
     */
39
    public function getAuthenticator()
40
    {
41
        return $this->getMethod()->getAuthenticator();
42
    }
43
44
    /**
45
     * @return mixed
46
     */
47
    public function getRegistrar()
48
    {
49
        return $this->getMethod()->getRegistrar();
50
    }
51
52
    /**
53
     * Accessor for Data field to ensure it's presented as an array instead of a JSON blob
54
     *
55
     * @return array
56
     */
57
    public function getData()
58
    {
59
        return (array) json_decode($this->getField('Data'), true);
60
    }
61
62
    /**
63
     * Setter for the Data field to ensure it's saved as a JSON blob
64
     *
65
     * @param mixed $data
66
     */
67
    public function setData($data)
68
    {
69
        $this->setField('Data', json_encode($data));
70
    }
71
72
    /**
73
     * @return AuthenticationMethodInterface
74
     */
75
    protected function getMethod()
76
    {
77
        if (!$this->method) {
78
            $this->method = Injector::inst()->create($this->MethodClassName);
79
        }
80
81
        return $this->method;
82
    }
83
}
84