QRCode::getCustomer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Block\Customer\Account\Edit;
23
24
use Magento\Customer\Api\Data\CustomerInterface;
25
use Magento\Framework\View\Element\Template;
26
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerConfig;
27
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret;
28
use Rossmitchell\Twofactor\Model\Customer\Customer;
29
use Rossmitchell\Twofactor\Model\Customer\GetQrCode;
30
31
class QRCode extends Template
32
{
33
    /**
34
     * @var TwoFactorSecret
35
     */
36
    private $twoFactorSecret;
37
    /**
38
     * @var Customer
39
     */
40
    private $customerGetter;
41
    /**
42
     * @var CustomerConfig
43
     */
44
    private $customerConfig;
45
    /**
46
     * @var GetQrCode
47
     */
48
    private $getQrCode;
49
50
    /**
51
     * QRCode constructor.
52
     *
53
     * @param Template\Context $context
54
     * @param TwoFactorSecret  $twoFactorSecret
55
     * @param Customer         $customerGetter
56
     * @param CustomerConfig   $customerConfig
57
     * @param GetQrCode        $getQrCode
58
     * @param array            $data
59
     *
60
     * @internal param array $data
61
     */
62 4 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
        Template\Context $context,
64
        TwoFactorSecret $twoFactorSecret,
65
        Customer $customerGetter,
66
        CustomerConfig $customerConfig,
67
        GetQrCode $getQrCode,
68
        array $data = []
69
    ) {
70 4
        parent::__construct($context, $data);
71 4
        $this->twoFactorSecret = $twoFactorSecret;
72 4
        $this->customerGetter  = $customerGetter;
73 4
        $this->customerConfig  = $customerConfig;
74 4
        $this->getQrCode = $getQrCode;
75 4
    }
76
77
    /**
78
     * A simple getter method to return the current customer
79
     *
80
     * @return CustomerInterface|false
81
     */
82 4
    public function getCustomer()
83
    {
84 4
        return $this->customerGetter->getCustomer();
85
    }
86
87
    /**
88
     * Used to check if the code should be displayed - will return false if two factor is disabled in the config, or if
89
     * the customer does not have a code. Otherwise returns true
90
     *
91
     * @param CustomerInterface $customer
92
     *
93
     * @return bool
94
     */
95 4
    public function shouldQrCodeBeDisplayed(CustomerInterface $customer)
96
    {
97 4
        if ($this->customerConfig->isTwoFactorEnabled() === false) {
98 1
            return false;
99
        }
100
101 3
        if ($this->twoFactorSecret->hasValue($customer) === false) {
102 1
            return false;
103
        }
104
105 2
        return true;
106
    }
107
108
    /**
109
     * Used to get the customers QR Code. Will return false if they don't have one, otherwise will return the image
110
     * string
111
     *
112
     * @param CustomerInterface $customer
113
     *
114
     * @return bool|string
115
     */
116 2
    public function getQrCode(CustomerInterface $customer)
117
    {
118 2
        return $this->getQrCode->getQrCode($customer);
119
    }
120
}
121