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

Postdispatch   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 122
Duplicated Lines 25.41 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 31
loc 122
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A execute() 17 17 4
A isTwoFactorEnabled() 0 4 1
A shouldTheUserBeRedirected() 0 18 4
A areWeOnANonRedirectingPage() 14 14 3
A redirectToAuthenticationPage() 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\Admin;
23
24
use Magento\Backend\App\Action;
25
use Magento\Framework\Event\Observer;
26
use Magento\Framework\Event\ObserverInterface;
27
use Rossmitchell\Twofactor\Model\Admin\AdminUser;
28
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
29
use Rossmitchell\Twofactor\Model\Admin\Session;
30
use Rossmitchell\Twofactor\Model\Config\Admin;
31
use Rossmitchell\Twofactor\Model\TwoFactorUrls;
32
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
33
34
class Postdispatch implements ObserverInterface
35
{
36
    /**
37
     * @var AdminUser
38
     */
39
    private $adminUser;
40
    /**
41
     * @var IsUsingTwoFactor
42
     */
43
    private $isUsingTwoFactor;
44
    /**
45
     * @var Session
46
     */
47
    private $session;
48
    /**
49
     * @var IsVerified
50
     */
51
    private $isVerified;
52
    /**
53
     * @var TwoFactorUrls
54
     */
55
    private $twoFactorUrls;
56
    /**
57
     * @var Admin
58
     */
59
    private $adminConfig;
60
61
    /**
62
     * Postdispatch constructor.
63
     *
64
     * @param AdminUser        $adminUser
65
     * @param IsUsingTwoFactor $isUsingTwoFactor
66
     * @param Session          $session
67
     * @param IsVerified       $isVerified
68
     * @param TwoFactorUrls    $twoFactorUrls
69
     * @param Admin            $adminConfig
70
     */
71
    public function __construct(
72
        AdminUser $adminUser,
73
        IsUsingTwoFactor $isUsingTwoFactor,
74
        Session $session,
75
        IsVerified $isVerified,
76
        TwoFactorUrls $twoFactorUrls,
77
        Admin $adminConfig
78
    ) {
79
        $this->adminUser        = $adminUser;
80
        $this->isUsingTwoFactor = $isUsingTwoFactor;
81
        $this->session          = $session;
82
        $this->isVerified       = $isVerified;
83
        $this->twoFactorUrls    = $twoFactorUrls;
84
        $this->adminConfig      = $adminConfig;
85
    }
86
87
    /**
88
     * @param Observer $observer
89
     *
90
     * @return void
91
     */
92 View Code Duplication
    public function execute(Observer $observer)
93
    {
94
        if ($this->isTwoFactorEnabled() === false) {
95
            return;
96
        }
97
98
        if ($this->shouldTheUserBeRedirected() === false) {
99
            return;
100
        }
101
102
        if ($this->areWeOnANonRedirectingPage() === true) {
103
            return;
104
        }
105
106
        $controller = $observer->getEvent()->getData('controller_action');
107
        $this->redirectToAuthenticationPage($controller);
108
    }
109
110
    private function isTwoFactorEnabled()
111
    {
112
        return ($this->adminConfig->isTwoFactorEnabled() == true);
113
    }
114
115
    private function shouldTheUserBeRedirected()
116
    {
117
        $adminUser = $this->adminUser;
118
        if ($adminUser->hasAdminUser() === false) {
119
            return false;
120
        }
121
        $user = $this->adminUser->getAdminUser();
122
123
        if ($this->isUsingTwoFactor->getValue($user) === false) {
124
            return false;
125
        }
126
127
        if ($this->isVerified->isVerified($this->session) === true) {
128
            return false;
129
        }
130
131
        return true;
132
    }
133
134 View Code Duplication
    private function areWeOnANonRedirectingPage()
135
    {
136
        $urls = $this->twoFactorUrls;
137
138
        if ($urls->areWeOnTheAuthenticationPage(true) === true) {
139
            return true;
140
        }
141
142
        if ($urls->areWeOnTheVerificationPage(true) === true) {
143
            return true;
144
        }
145
146
        return false;
147
    }
148
149
    private function redirectToAuthenticationPage(Action $controller)
150
    {
151
        $twoFactorCheckUrl = $this->twoFactorUrls->getAdminAuthenticationUrl();
152
        $response          = $controller->getResponse();
153
        $response->setRedirect($twoFactorCheckUrl);
154
    }
155
}
156