Passed
Push — master ( ce7412...bc6f2a )
by Ross
02:55
created

Session::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 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\Model\Customer;
23
24
25
use Magento\Catalog\Model\Session\Proxy;
26
use Magento\Framework\Exception\InputException;
27
28
class Session
29
{
30
    /**
31
     * @var Proxy
32
     */
33
    private $customerSession;
34
35
    /**
36
     * Session constructor.
37
     *
38
     * @param Proxy $customerSession
39
     */
40
    public function __construct(Proxy $customerSession)
41
    {
42
        $this->customerSession = $customerSession;
43
    }
44
45
    public function setData($key, $value)
46
    {
47
        $methodName = $this->convertKeyToMethodName('set', $key);
48
        $session    = $this->getSession();
49
        $session->$methodName($value);
50
    }
51
52
    public function getData($key)
53
    {
54
        $methodName = $this->convertKeyToMethodName('get', $key);
55
        $session    = $this->getSession();
56
57
        return $session->$methodName();
58
    }
59
60
    public function unsetData($key)
61
    {
62
        $methodName = $this->convertKeyToMethodName('uns', $key);
63
        $session    = $this->getSession();
64
        $session->$methodName($key);
65
    }
66
67
    public function hasData($key)
68
    {
69
        $methodName = $this->convertKeyToMethodName('has', $key);
70
        $session    = $this->getSession();
71
72
        return $session->$methodName();
73
    }
74
75
    private function convertKeyToMethodName($type, $key)
76
    {
77
        $allowedMethods = ['get', 'set', 'uns', 'has'];
78
        if (!in_array($type, $allowedMethods)) {
79
            InputException::invalidFieldValue('type', $type);
80
        }
81
        $methodName = $type.str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
82
83
        return $methodName;
84
    }
85
86
    private function getSession()
87
    {
88
        $session = $this->customerSession;
89
        $this->startSession($session);
90
91
        return $session;
92
    }
93
94
    private function startSession(Proxy $session)
95
    {
96
        if ($session->isSessionExists() === false) {
97
            $session->start();
98
        }
99
    }
100
}
101