Completed
Push — master ( 479bd3...ba046f )
by Ross
24:36
created

SessionTrait::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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 33
    public function getData($key)
37
    {
38 33
        $methodName = $this->convertKeyToMethodName('get', $key);
39 33
        $session    = $this->getSession();
40 33
        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 18
    public function hasData($key)
51
    {
52 18
        $methodName = $this->convertKeyToMethodName('has', $key);
53 18
        $session    = $this->getSession();
54
55 18
        return $session->$methodName();
56
    }
57
58 34
    private function convertKeyToMethodName($type, $key)
59
    {
60 34
        $methodName = $type.str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
61
62 34
        return $methodName;
63
    }
64
65
    abstract public function getSession();
66
}
67