Passed
Push — master ( 02da85...a64620 )
by Ross
02:46
created

UseTwoFactor::getCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Block\Customer\Account\Edit;
23
24
use Magento\Customer\Api\Data\CustomerInterface;
25
use Magento\Framework\View\Element\Template;
26
use Magento\Framework\View\Element\Template\Context;
27
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerConfig;
28
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor;
29
use Rossmitchell\Twofactor\Model\Customer\Customer;
30
use Rossmitchell\Twofactor\Model\Customer\UsingTwoFactor;
31
32
class UseTwoFactor extends Template
33
{
34
    /**
35
     * @var IsUsingTwoFactor
36
     */
37
    private $isUsingTwoFactor;
38
    /**
39
     * @var Customer
40
     */
41
    private $customerGetter;
42
    /**
43
     * @var CustomerConfig
44
     */
45
    private $customerConfig;
46
47
    /**
48
     * UseTwoFactor constructor.
49
     *
50
     * @param Context          $context
51
     * @param IsUsingTwoFactor $isUsingTwoFactor
52
     * @param Customer         $customerGetter
53
     * @param CustomerConfig   $customerConfig
54
     * @param array            $data
55
     */
56
    public function __construct(
57
        Context $context,
58
        IsUsingTwoFactor $isUsingTwoFactor,
59
        Customer $customerGetter,
60
        CustomerConfig $customerConfig,
61
        array $data = []
62
    ) {
63
        parent::__construct($context, $data);
64
        $this->isUsingTwoFactor = $isUsingTwoFactor;
65
        $this->customerGetter   = $customerGetter;
66
        $this->customerConfig = $customerConfig;
67
    }
68
69
    public function shouldBlockBeDisplayed()
70
    {
71
        return ($this->customerConfig->isTwoFactorEnabled() == true);
72
    }
73
74
    public function getCustomer()
75
    {
76
        return $this->customerGetter->getCustomer();
77
    }
78
79
    public function isUsingTwoFactor(CustomerInterface $customer)
80
    {
81
        if ($this->isUsingTwoFactor->hasValue($customer) === false) {
82
            return false;
83
        }
84
85
        return ($this->isUsingTwoFactor->getValue($customer) == true);
86
    }
87
88
    public function getSelectedForYes(CustomerInterface $customer)
89
    {
90
        return $this->getSelectedSnippet($customer, true);
91
    }
92
93
    public function getSelectedForNo(CustomerInterface $customer)
94
    {
95
        return $this->getSelectedSnippet($customer, false);
96
    }
97
98
    private function getSelectedSnippet(CustomerInterface $customer, $condition)
99
    {
100
        $html = '';
101
        if ($this->isUsingTwoFactor($customer) === $condition) {
102
            $html = ' selected="selected"';
103
        }
104
105
        return $html;
106
    }
107
}
108