Completed
Push — master ( 70bc12...725ad3 )
by Ross
35:29
created

SessionTrait::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 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 6
    public function setData($key, $value)
30
    {
31 6
        $methodName = $this->convertKeyToMethodName('set', $key);
32 6
        $session    = $this->getSession();
33 6
        $session->$methodName($value);
34 6
    }
35
36 22
    public function getData($key)
37
    {
38 22
        $methodName = $this->convertKeyToMethodName('get', $key);
39 22
        $session    = $this->getSession();
40 22
        return $session->$methodName();
41
    }
42
43 2
    public function unsetData($key)
44
    {
45 2
        $methodName = $this->convertKeyToMethodName('uns', $key);
46 2
        $session    = $this->getSession();
47 2
        $session->$methodName($key);
48 2
    }
49
50 6
    public function hasData($key)
51
    {
52 6
        $methodName = $this->convertKeyToMethodName('has', $key);
53 6
        $session    = $this->getSession();
54
55 6
        return $session->$methodName();
56
    }
57
58 24
    private function convertKeyToMethodName($type, $key)
59
    {
60 24
        $allowedMethods = ['get', 'set', 'uns', 'has'];
61 24
        if (!in_array($type, $allowedMethods)) {
62
            InputException::invalidFieldValue('type', $type);
63
        }
64 24
        $methodName = $type.str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
65
66 24
        return $methodName;
67
    }
68
69
    abstract public function getSession();
70
}
71