Completed
Push — master ( 70bc12...725ad3 )
by Ross
35:29
created

Verify::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 17
nc 1
nop 8
crap 1

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\Action;
26
use Magento\Framework\App\Action\Context;
27
use Magento\Framework\App\ResponseInterface;
28
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
29
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret;
30
use Rossmitchell\Twofactor\Model\Customer\Customer;
31
use Rossmitchell\Twofactor\Model\Customer\Session;
32
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
33
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\Verify as GoogleVerify;
34
use Rossmitchell\Twofactor\Model\TwoFactorUrls;
35
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerAdmin;
36
37
class Verify extends Action
38
{
39
40
    /**
41
     * @var TwoFactorSecret
42
     */
43
    private $secret;
44
    /**
45
     * @var GoogleVerify
46
     */
47
    private $verify;
48
    /**
49
     * @var Customer
50
     */
51
    private $customerGetter;
52
    /**
53
     * @var TwoFactorUrls
54
     */
55
    private $twoFactorUrls;
56
    /**
57
     * @var IsVerified
58
     */
59
    private $isVerified;
60
    /**
61
     * @var Session
62
     */
63
    private $customerSession;
64
    /**
65
     * @var CustomerAdmin
66
     */
67
    private $customerAdmin;
68
69
    /**
70
     * Constructor
71
     *
72
     * @param Context         $context
73
     * @param Customer        $customerGetter
74
     * @param TwoFactorSecret $secret
75
     * @param GoogleVerify    $verify
76
     * @param TwoFactorUrls   $twoFactorUrls
77
     * @param IsVerified      $isVerified
78
     * @param Session         $customerSession
79
     * @param CustomerAdmin   $customerAdmin
80
     */
81 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...
82
        Context $context,
83
        Customer $customerGetter,
84
        TwoFactorSecret $secret,
85
        GoogleVerify $verify,
86
        TwoFactorUrls $twoFactorUrls,
87
        IsVerified $isVerified,
88
        Session $customerSession,
89
        CustomerAdmin $customerAdmin
90
    ) {
91 8
        parent::__construct($context);
92 8
        $this->secret          = $secret;
93 8
        $this->verify          = $verify;
94 8
        $this->customerGetter  = $customerGetter;
95 8
        $this->twoFactorUrls   = $twoFactorUrls;
96 8
        $this->isVerified      = $isVerified;
97 8
        $this->customerSession = $customerSession;
98 8
        $this->customerAdmin   = $customerAdmin;
99 8
    }
100
101
    /**
102
     * Dispatch request
103
     *
104
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
105
     * @throws \Magento\Framework\Exception\NotFoundException
106
     */
107 8
    public function execute()
108
    {
109 8
        if ($this->isEnabled() === false) {
110 2
            return $this->handleDisabled();
111
        }
112
113 6
        $secret   = $this->getRequest()->getParam('secret');
114 6
        $customer = $this->customerGetter->getCustomer();
115
116 6
        if ($customer === false) {
117 2
            return $this->handleMissingCustomer();
118
        }
119
120 4
        $verificationPassed = $this->verifySecret($customer, $secret);
121
122 4
        if ($verificationPassed === false) {
123 2
            return $this->handleError();
124
        }
125
126 2
        return $this->handleSuccess();
127
    }
128
129 8
    private function isEnabled()
130
    {
131 8
        return ($this->customerAdmin->isTwoFactorEnabled() == true);
132
    }
133
134 2
    private function handleDisabled()
135
    {
136 2
        return $this->redirect('/');
137
    }
138
139 4
    private function verifySecret(CustomerInterface $customer, $postedSecret)
140
    {
141 4
        $customerSecret = $this->secret->getValue($customer);
142
        try {
143 4
            $verified = $this->verify->verify($customerSecret, $postedSecret);
144 2
        } catch (InvalidCharactersException $exception) {
145
            $verified = false;
146
        }
147
148 4
        return $verified;
149
    }
150
151 2
    private function handleSuccess()
152
    {
153 2
        $this->isVerified->setIsVerified($this->customerSession);
154 2
        $this->addSuccessMessage();
155 2
        $accountUrl = $this->twoFactorUrls->getCustomerAccountUrl();
156
157 2
        return $this->redirect($accountUrl);
158
    }
159
160 2
    private function handleMissingCustomer()
161
    {
162 2
        $loginUrl = $this->twoFactorUrls->getCustomerLogInUrl();
163
164 2
        return $this->redirect($loginUrl);
165
    }
166
167 2
    private function handleError()
168
    {
169 2
        $this->isVerified->removeIsVerified($this->customerSession);
170 2
        $this->addErrorMessage();
171 2
        $authenticateUrl = $this->twoFactorUrls->getCustomerAuthenticationUrl();
172
173 2
        return $this->redirect($authenticateUrl);
174
    }
175
176 2
    private function addErrorMessage()
177
    {
178 2
        $this->messageManager->addErrorMessage("Two Factor Code was incorrect");
179 2
    }
180
181 2
    private function addSuccessMessage()
182
    {
183 2
        $this->messageManager->addSuccessMessage("Two Factor Code was correct");
184 2
    }
185
186 8
    private function redirect($path)
187
    {
188 8
        $redirect = $this->resultRedirectFactory->create();
189 8
        $redirect->setPath($path);
190
191 8
        return $redirect;
192
    }
193
}
194