Completed
Push — master ( 725ad3...990c1d )
by Ross
38:00
created

Verify::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 9
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\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\TwoFactorUrls;
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 TwoFactorUrls
50
     */
51
    private $twoFactorUrls;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
52
    /**
53
     * @var IsVerified
54
     */
55
    private $isVerified;
56
    /**
57
     * @var Session
58
     */
59
    private $customerSession;
60
61
    /**
62
     * Constructor
63
     *
64
     * @param Context          $context
65
     * @param Customer         $customerGetter
66
     * @param TwoFactorSecret  $secret
67
     * @param GoogleVerify     $verify
68
     * @param TwoFactorUrls    $twoFactorUrls
69
     * @param IsVerified       $isVerified
70
     * @param Session          $customerSession
71
     * @param CustomerAdmin    $customerAdmin
72
     * @param IsUsingTwoFactor $isUsingTwoFactor
73
     */
74 8
    public function __construct(
75
        Context $context,
76
        Customer $customerGetter,
77
        TwoFactorSecret $secret,
78
        GoogleVerify $verify,
79
        TwoFactorUrls $twoFactorUrls,
80
        IsVerified $isVerified,
81
        Session $customerSession,
82
        CustomerAdmin $customerAdmin,
83
        IsUsingTwoFactor $isUsingTwoFactor
84
    ) {
85 8
        parent::__construct($context, $customerAdmin, $customerGetter, $twoFactorUrls, $isUsingTwoFactor);
86 8
        $this->secret          = $secret;
87 8
        $this->verify          = $verify;
88 8
        $this->twoFactorUrls   = $twoFactorUrls;
89 8
        $this->isVerified      = $isVerified;
90 8
        $this->customerSession = $customerSession;
91 8
    }
92
93
    /**
94
     * Dispatch request
95
     *
96
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
97
     * @throws \Magento\Framework\Exception\NotFoundException
98
     */
99 8
    public function execute()
100
    {
101 8
        if ($this->shouldActionBeRun() === false) {
102 4
            return $this->getRedirectAction();
103
        }
104
105 4
        $secret   = $this->getRequest()->getParam('secret');
106 4
        $customer = $this->getCustomer();
107 4
        $verificationPassed = $this->verifySecret($customer, $secret);
108
109 4
        if ($verificationPassed === false) {
110 2
            return $this->handleError();
111
        }
112
113 2
        return $this->handleSuccess();
114
    }
115
116 4
    private function verifySecret(CustomerInterface $customer, $postedSecret)
117
    {
118 4
        $customerSecret = $this->secret->getValue($customer);
119
        try {
120 4
            $verified = $this->verify->verify($customerSecret, $postedSecret);
121 2
        } catch (InvalidCharactersException $exception) {
122
            $verified = false;
123
        }
124
125 4
        return $verified;
126
    }
127
128 2
    private function handleSuccess()
129
    {
130 2
        $this->isVerified->setIsVerified($this->customerSession);
131 2
        $this->addSuccessMessage();
132 2
        $accountUrl = $this->twoFactorUrls->getCustomerAccountUrl();
133
134 2
        return $this->redirect($accountUrl);
135
    }
136
137 2
    private function handleError()
138
    {
139 2
        $this->isVerified->removeIsVerified($this->customerSession);
140 2
        $this->addErrorMessage();
141 2
        $authenticateUrl = $this->twoFactorUrls->getCustomerAuthenticationUrl();
142
143 2
        return $this->redirect($authenticateUrl);
144
    }
145
146 2
    private function addErrorMessage()
147
    {
148 2
        $this->messageManager->addErrorMessage("Two Factor Code was incorrect");
149 2
    }
150
151 2
    private function addSuccessMessage()
152
    {
153 2
        $this->messageManager->addSuccessMessage("Two Factor Code was correct");
154 2
    }
155
}
156