Passed
Push — master ( 785691...679eaf )
by Ross
39:28
created

Postdispatch   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 10.94 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 6
dl 14
loc 128
ccs 42
cts 42
cp 1
rs 10

6 Methods

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