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

AbstractController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 11.61 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
lcom 1
cbo 7
dl 13
loc 112
ccs 41
cts 41
cp 1
rs 10
c 1
b 0
f 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
B shouldActionBeRun() 0 22 4
A getRedirectAction() 0 4 1
A isEnabled() 0 4 1
A handleDisabled() 0 4 1
A getCustomer() 0 8 2
A handleMissingCustomer() 0 6 1
A isCustomerUsingTwoFactor() 0 6 1
A handleNonOptInCustomer() 0 4 1
A redirect() 0 7 1

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\Controller\Customerlogin;
23
24
use Magento\Framework\App\Action\Action;
25
use Magento\Framework\App\Action\Context;
26
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerAdmin;
27
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor;
28
use Rossmitchell\Twofactor\Model\Customer\Customer;
29
use Rossmitchell\Twofactor\Model\TwoFactorUrls;
30
31
abstract class AbstractController extends Action
32
{
33
34
    /**
35
     * @var CustomerAdmin
36
     */
37
    private $customerAdmin;
38
    /**
39
     * @var Customer
40
     */
41
    private $customerGetter;
42
    /**
43
     * @var TwoFactorUrls
44
     */
45
    private $twoFactorUrls;
46
47
    private $redirectAction;
48
49
    private $customerModel;
50
    /**
51
     * @var IsUsingTwoFactor
52
     */
53
    private $isUsingTwoFactor;
54
55 12 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...
56
        Context $context,
57
        CustomerAdmin $customerAdmin,
58
        Customer $customerGetter,
59
        TwoFactorUrls $twoFactorUrls,
60
        IsUsingTwoFactor $isUsingTwoFactor
61
    ) {
62 12
        parent::__construct($context);
63 12
        $this->customerAdmin  = $customerAdmin;
64 12
        $this->customerGetter = $customerGetter;
65 12
        $this->twoFactorUrls  = $twoFactorUrls;
66 12
        $this->isUsingTwoFactor = $isUsingTwoFactor;
67 12
    }
68
69 12
    public function shouldActionBeRun()
70
    {
71 12
        if ($this->isEnabled() === false) {
72 2
            $this->redirectAction = $this->handleDisabled();
73
74 2
            return false;
75
        }
76
77 10
        if ($this->getCustomer() === false) {
78 2
            $this->redirectAction = $this->handleMissingCustomer();
79
80 2
            return false;
81
        }
82
83 8
        if ($this->isCustomerUsingTwoFactor() === false) {
84 2
            $this->redirectAction = $this->handleNonOptInCustomer();
85
86 2
            return false;
87
        }
88
89 6
        return true;
90
    }
91
92 6
    public function getRedirectAction()
93
    {
94 6
        return $this->redirectAction;
95
    }
96
97 12
    private function isEnabled()
98
    {
99 12
        return ($this->customerAdmin->isTwoFactorEnabled() == true);
100
    }
101
102 2
    private function handleDisabled()
103
    {
104 2
        return $this->redirect('/');
105
    }
106
107 10
    public function getCustomer()
108
    {
109 10
        if (null === $this->customerModel) {
110 10
            $this->customerModel = $this->customerGetter->getCustomer();
111 5
        }
112
113 10
        return $this->customerModel;
114
    }
115
116 2
    private function handleMissingCustomer()
117
    {
118 2
        $loginUrl = $this->twoFactorUrls->getCustomerLogInUrl();
119
120 2
        return $this->redirect($loginUrl);
121
    }
122
123 8
    private function isCustomerUsingTwoFactor()
124
    {
125 8
        $customer = $this->getCustomer();
126
127 8
        return $this->isUsingTwoFactor->getValue($customer);
128
    }
129
130 2
    private function handleNonOptInCustomer()
131
    {
132 2
        return $this->redirect('/');
133
    }
134
135 10
    public function redirect($path)
136
    {
137 10
        $redirect = $this->resultRedirectFactory->create();
138 10
        $redirect->setPath($path);
139
140 10
        return $redirect;
141
    }
142
}
143