Passed
Push — master ( b162a4...87a689 )
by Ross
03:05
created

SessionTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setData() 0 6 1
A getData() 0 7 1
A unsetData() 0 6 1
A hasData() 0 7 1
A convertKeyToMethodName() 0 10 2
getSession() 0 1 ?
1
<?php
2
/**
3
 * A two factor authentication module that protects both the admin and customer logins
4
 * Copyright (C) 2017  Ross Mitchell
5
 *
6
 * This file is part of Rossmitchell/Twofactor.
7
 *
8
 * Rossmitchell/Twofactor is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace Rossmitchell\Twofactor\Traits;
23
24
use Magento\Framework\Exception\InputException;
25
26
trait SessionTrait
27
{
28
29
    public function setData($key, $value)
30
    {
31
        $methodName = $this->convertKeyToMethodName('set', $key);
32
        $session    = $this->getSession();
33
        $session->$methodName($value);
34
    }
35
36
    public function getData($key)
37
    {
38
        $methodName = $this->convertKeyToMethodName('get', $key);
39
        $session    = $this->getSession();
40
41
        return $session->$methodName();
42
    }
43
44
    public function unsetData($key)
45
    {
46
        $methodName = $this->convertKeyToMethodName('uns', $key);
47
        $session    = $this->getSession();
48
        $session->$methodName($key);
49
    }
50
51
    public function hasData($key)
52
    {
53
        $methodName = $this->convertKeyToMethodName('has', $key);
54
        $session    = $this->getSession();
55
56
        return $session->$methodName();
57
    }
58
59
    private function convertKeyToMethodName($type, $key)
60
    {
61
        $allowedMethods = ['get', 'set', 'uns', 'has'];
62
        if (!in_array($type, $allowedMethods)) {
63
            InputException::invalidFieldValue('type', $type);
64
        }
65
        $methodName = $type.str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
66
67
        return $methodName;
68
    }
69
70
    abstract public function getSession();
71
}
72