Postdispatch::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
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 18
    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 18
        $this->adminUser        = $adminUser;
87 18
        $this->isUsingTwoFactor = $isUsingTwoFactor;
88 18
        $this->session          = $session;
89 18
        $this->isVerified       = $isVerified;
90 18
        $this->adminConfig      = $adminConfig;
91 18
        $this->checker          = $checker;
92 18
        $this->fetcher          = $fetcher;
93 18
    }
94
95
    /**
96
     * @param Observer $observer
97
     *
98
     * @return void
99
     */
100 18 View Code Duplication
    public function execute(Observer $observer)
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...
101
    {
102 18
        if ($this->isTwoFactorEnabled() === false) {
103 5
            return;
104
        }
105
106 13
        if ($this->shouldTheUserBeRedirected() === false) {
107 8
            return;
108
        }
109
110 5
        if ($this->areWeOnANonRedirectingPage() === true) {
111 2
            return;
112
        }
113
114 3
        $controller = $observer->getEvent()->getData('response');
115 3
        $this->redirectToAuthenticationPage($controller);
116 3
    }
117
118 18
    private function isTwoFactorEnabled()
119
    {
120 18
        return ($this->adminConfig->isTwoFactorEnabled() == true);
121
    }
122
123 13
    private function shouldTheUserBeRedirected()
124
    {
125 13
        $adminUser = $this->adminUser;
126 13
        if ($adminUser->hasAdminUser() === false) {
127 2
            return false;
128
        }
129 11
        $user = $this->adminUser->getAdminUser();
130
131 11
        if ($this->isUsingTwoFactor->getValue($user) === false) {
132 4
            return false;
133
        }
134
135 7
        if ($this->isVerified->isVerified($this->session) === true) {
136 2
            return false;
137
        }
138
139 5
        return true;
140
    }
141
142 5 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 5
        $urls = $this->checker;
145
146 5
        if ($urls->areWeOnTheAuthenticationPage(true) === true) {
147 1
            return true;
148
        }
149
150 4
        if ($urls->areWeOnTheVerificationPage(true) === true) {
151 1
            return true;
152
        }
153
154 3
        return false;
155
    }
156
157 3
    private function redirectToAuthenticationPage($response)
158
    {
159 3
        $twoFactorCheckUrl = $this->fetcher->getAuthenticationUrl(true);
160 3
        $response->setRedirect($twoFactorCheckUrl);
161 3
    }
162
}
163