Passed
Push — master ( aeba21...9e76ce )
by Ross
47:24
created

QRCode   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 19.44 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 2
cbo 5
dl 14
loc 72
ccs 19
cts 20
cp 0.95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 1
A getCustomer() 0 4 1
A hasAQrCode() 0 8 2
A getQrCode() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\GoogleTwoFactor\QRCode as QRCodeGenerator;
30
31
class QRCode extends Template
32
{
33
    /**
34
     * @var QRCodeGenerator
35
     */
36
    private $qRCode;
37
    /**
38
     * @var TwoFactorSecret
39
     */
40
    private $twoFactorSecret;
41
    /**
42
     * @var Customer
43
     */
44
    private $customerGetter;
45
    /**
46
     * @var CustomerConfig
47
     */
48
    private $customerConfig;
49
50
    /**
51
     * QRCode constructor.
52
     *
53
     * @param Template\Context $context
54
     * @param QRCodeGenerator  $qRCode
55
     * @param TwoFactorSecret  $twoFactorSecret
56
     * @param Customer         $customerGetter
57
     * @param CustomerConfig   $customerConfig
58
     *
59
     * @internal param array $data
60
     */
61 8 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...
62
        Template\Context $context,
63
        QRCodeGenerator $qRCode,
64
        TwoFactorSecret $twoFactorSecret,
65
        Customer $customerGetter,
66
        CustomerConfig $customerConfig,
67
        array $data = []
68
    ) {
69 8
        parent::__construct($context, $data);
70 8
        $this->qRCode          = $qRCode;
71 8
        $this->twoFactorSecret = $twoFactorSecret;
72 8
        $this->customerGetter  = $customerGetter;
73 8
        $this->customerConfig  = $customerConfig;
74 8
    }
75
76 8
    public function getCustomer()
77
    {
78 8
        return $this->customerGetter->getCustomer();
79
    }
80
81 8
    public function hasAQrCode(CustomerInterface $customer)
82
    {
83 8
        if ($this->customerConfig->isTwoFactorEnabled() == false) {
84 2
            return false;
85
        }
86
87 6
        return $this->twoFactorSecret->hasValue($customer);
88
    }
89
90 4
    public function getQrCode(CustomerInterface $customer)
91
    {
92 4
        if ($this->twoFactorSecret->hasValue($customer) === false) {
93
            throw new \Exception("Could not load the QR code because the customer does not have a secret");
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
94
        }
95
96 4
        $secret = $this->twoFactorSecret->hasValue($customer);
97 4
        $email  = $customer->getEmail();
98 4
        $qrCode = $this->qRCode->generateQRCode('Test', $email, $secret);
99
100 4
        return $qrCode;
101
    }
102
}
103