Completed
Push — master ( f58984...07dd6c )
by Ross
26:19
created

Verify::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1.2035

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 17
loc 17
ccs 7
cts 17
cp 0.4118
rs 9.4285
cc 1
eloc 15
nc 1
nop 9
crap 1.2035

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Controller\Customerlogin;
23
24
use Magento\Customer\Api\Data\CustomerInterface;
25
use Magento\Framework\App\Action\Context;
26
use Magento\Framework\App\ResponseInterface;
27
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
28
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerAdmin;
29
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor;
30
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret;
31
use Rossmitchell\Twofactor\Model\Customer\Customer;
32
use Rossmitchell\Twofactor\Model\Customer\Session;
33
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\Verify as GoogleVerify;
34
use Rossmitchell\Twofactor\Model\Urls\Fetcher;
35
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
36
37
class Verify extends AbstractController
38
{
39
40
    /**
41
     * @var TwoFactorSecret
42
     */
43
    private $secret;
44
    /**
45
     * @var GoogleVerify
46
     */
47
    private $verify;
48
    /**
49
     * @var IsVerified
50
     */
51
    private $isVerified;
52
    /**
53
     * @var Session
54
     */
55
    private $customerSession;
56
57
    /**
58
     * Constructor
59
     *
60
     * @param Context $context
61
     * @param Customer $customerGetter
62
     * @param TwoFactorSecret $secret
63
     * @param GoogleVerify $verify
64
     * @param Fetcher $fetcher
65
     * @param IsVerified $isVerified
66
     * @param Session $customerSession
67
     * @param CustomerAdmin $customerAdmin
68
     * @param IsUsingTwoFactor $isUsingTwoFactor
69
     */
70 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...
71
        Context $context,
72
        Customer $customerGetter,
73
        TwoFactorSecret $secret,
74
        GoogleVerify $verify,
75
        Fetcher $fetcher,
76
        IsVerified $isVerified,
77
        Session $customerSession,
78
        CustomerAdmin $customerAdmin,
79
        IsUsingTwoFactor $isUsingTwoFactor
80
    ) {
81 4
        parent::__construct($context, $customerAdmin, $customerGetter, $fetcher, $isUsingTwoFactor);
82 4
        $this->secret          = $secret;
83 4
        $this->verify          = $verify;
84 4
        $this->isVerified      = $isVerified;
85 4
        $this->customerSession = $customerSession;
86 4
    }
87
88
    /**
89
     * Dispatch request
90
     *
91
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
92
     * @throws \Magento\Framework\Exception\NotFoundException
93
     */
94 4 View Code Duplication
    public function execute()
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...
95
    {
96 4
        if ($this->shouldActionBeRun() === false) {
97 2
            return $this->getRedirectAction();
98
        }
99
100 2
        $secret   = $this->getRequest()->getParam('secret');
101 2
        $customer = $this->getCustomer();
102 2
        $verificationPassed = $this->verifySecret($customer, $secret);
0 ignored issues
show
Security Bug introduced by
It seems like $customer defined by $this->getCustomer() on line 101 can also be of type false; however, Rossmitchell\Twofactor\C...\Verify::verifySecret() does only seem to accept object<Magento\Customer\...Data\CustomerInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
103
104 2
        if ($verificationPassed === false) {
105 1
            return $this->handleError();
106
        }
107
108 1
        return $this->handleSuccess();
109
    }
110
111 2
    private function verifySecret(CustomerInterface $customer, $postedSecret)
112
    {
113 2
        $customerSecret = $this->secret->getValue($customer);
114
        try {
115 2
            $verified = $this->verify->verify($customerSecret, $postedSecret);
116
        } catch (InvalidCharactersException $exception) {
117
            $verified = false;
118
        }
119
120 2
        return $verified;
121
    }
122
123 1
    private function handleSuccess()
124
    {
125 1
        $this->isVerified->setIsVerified($this->customerSession);
126 1
        $this->addSuccessMessage();
127 1
        $accountUrl = $this->getUrlFetcher()->getCustomerAccountUrl();
128
129 1
        return $this->redirect($accountUrl);
130
    }
131
132 1 View Code Duplication
    private function handleError()
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...
133
    {
134 1
        $this->isVerified->removeIsVerified($this->customerSession);
135 1
        $this->addErrorMessage();
136 1
        $authenticateUrl = $this->getUrlFetcher()->getAuthenticationUrl();
137
138 1
        return $this->redirect($authenticateUrl);
139
    }
140
141 1
    private function addErrorMessage()
142
    {
143 1
        $this->messageManager->addErrorMessage("Two Factor Code was incorrect");
144 1
    }
145
146 1
    private function addSuccessMessage()
147
    {
148 1
        $this->messageManager->addSuccessMessage("Two Factor Code was correct");
149 1
    }
150
}
151