Passed
Push — master ( 02da85...a64620 )
by Ross
02:46
created

Postdispatch   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 21.13 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 30
loc 142
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A execute() 17 17 4
A isTwoFactorEnabled() 0 4 1
A shouldTheCustomerBeRedirected() 0 17 4
A areWeOnAnAllowedPage() 13 13 3
A hasTwoFactorBeenChecked() 0 7 1
A redirectToTwoFactorCheck() 0 6 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\Observer\Controller\Frontend;
23
24
use Magento\Framework\App\Action\Action;
25
use Magento\Framework\App\ResponseFactory;
26
use Magento\Framework\Event\Observer;
27
use Magento\Framework\Event\ObserverInterface;
28
use Magento\Framework\UrlInterface;
29
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerAdmin;
30
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor;
31
use Rossmitchell\Twofactor\Model\Customer\Customer;
32
use Rossmitchell\Twofactor\Model\Customer\Session;
33
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
34
use Rossmitchell\Twofactor\Model\TwoFactorUrls;
35
36
class Postdispatch implements ObserverInterface
37
{
38
    /**
39
     * @var ResponseFactory
40
     */
41
    private $responseFactory;
42
    /**
43
     * @var UrlInterface
44
     */
45
    private $url;
46
    /**
47
     * @var Customer
48
     */
49
    private $customerGetter;
50
    /**
51
     * @var IsUsingTwoFactor
52
     */
53
    private $isUsingTwoFactor;
54
    /**
55
     * @var IsVerified
56
     */
57
    private $isVerified;
58
    /**
59
     * @var TwoFactorUrls
60
     */
61
    private $twoFactorUrls;
62
    /**
63
     * @var Session
64
     */
65
    private $customerSession;
66
    /**
67
     * @var CustomerAdmin
68
     */
69
    private $customerAdmin;
70
71
    /**
72
     * Predispatch constructor.
73
     *
74
     * @param ResponseFactory  $responseFactory
75
     * @param UrlInterface     $url
76
     * @param Customer         $customerGetter
77
     * @param IsVerified       $isVerified
78
     * @param Session          $customerSession
79
     * @param IsUsingTwoFactor $isUsingTwoFactor
80
     * @param TwoFactorUrls    $twoFactorUrls
81
     * @param CustomerAdmin    $customerAdmin
82
     */
83
    public function __construct(
84
        ResponseFactory $responseFactory,
85
        UrlInterface $url,
86
        Customer $customerGetter,
87
        IsVerified $isVerified,
88
        Session $customerSession,
89
        IsUsingTwoFactor $isUsingTwoFactor,
90
        TwoFactorUrls $twoFactorUrls,
91
        CustomerAdmin $customerAdmin
92
    ) {
93
        $this->responseFactory  = $responseFactory;
94
        $this->url              = $url;
95
        $this->customerGetter   = $customerGetter;
96
        $this->isUsingTwoFactor = $isUsingTwoFactor;
97
        $this->isVerified       = $isVerified;
98
        $this->twoFactorUrls    = $twoFactorUrls;
99
        $this->customerSession  = $customerSession;
100
        $this->customerAdmin    = $customerAdmin;
101
    }
102
103
    /**
104
     * @param Observer $observer
105
     *
106
     * @return void
107
     */
108 View Code Duplication
    public function execute(Observer $observer)
109
    {
110
        if ($this->isTwoFactorEnabled() === false) {
111
            return;
112
        }
113
114
        if ($this->shouldTheCustomerBeRedirected() === false) {
115
            return;
116
        }
117
118
        if ($this->hasTwoFactorBeenChecked() === true) {
119
            return;
120
        }
121
122
        $controller = $observer->getEvent()->getData('controller_action');
123
        $this->redirectToTwoFactorCheck($controller);
124
    }
125
126
    private function isTwoFactorEnabled()
127
    {
128
        return ($this->customerAdmin->isTwoFactorEnabled() == true);
129
    }
130
131
    private function shouldTheCustomerBeRedirected()
132
    {
133
        if ($this->areWeOnAnAllowedPage() === true) {
134
            return false;
135
        }
136
137
        $customer = $this->customerGetter->getCustomer();
138
        if ($customer === false) {
139
            return false;
140
        }
141
        $usingTwoFactor = $this->isUsingTwoFactor->getValue($customer);
142
        if ($usingTwoFactor === false) {
143
            return false;
144
        }
145
146
        return true;
147
    }
148
149 View Code Duplication
    private function areWeOnAnAllowedPage()
150
    {
151
        $twoFactorUrls = $this->twoFactorUrls;
152
        if ($twoFactorUrls->areWeOnTheAuthenticationPage(false) === true) {
153
            return true;
154
        }
155
156
        if ($twoFactorUrls->areWeOnTheVerificationPage(false) === true) {
157
            return true;
158
        }
159
160
        return false;
161
    }
162
163
    private function hasTwoFactorBeenChecked()
164
    {
165
        $session = $this->customerSession;
166
        $checked = $this->isVerified->isVerified($session);
167
168
        return ($checked === true);
169
    }
170
171
    private function redirectToTwoFactorCheck(Action $controller)
172
    {
173
        $twoFactorCheckUrl = $this->twoFactorUrls->getAuthenticationUrl(false);
174
        $response          = $controller->getResponse();
175
        $response->setRedirect($twoFactorCheckUrl);
176
    }
177
}
178