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

AbstractController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 5
crap 1
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\Adminhtml\Adminlogin;
23
24
use Magento\Framework\App\Action\Action;
25
use Magento\Framework\App\Action\Context;
26
use Rossmitchell\Twofactor\Model\Config\Admin as UserAdmin;
27
use Rossmitchell\Twofactor\Model\Admin\AdminUser;
28
use Rossmitchell\Twofactor\Model\Urls\Fetcher;
29
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
30
31
abstract class AbstractController extends Action
32
{
33
    /**
34
     * @var UserAdmin
35
     */
36
    private $userAdmin;
37
    /**
38
     * @var AdminUser
39
     */
40
    private $adminGetter;
41
    /**
42
     * @var Fetcher
43
     */
44
    private $fetcher;
45
    /**
46
     * @var IsUsingTwoFactor
47
     */
48
    private $isUsingTwoFactor;
49
50
    private $redirectAction;
51
52
    private $adminModel;
53
54
    /**
55
     * @param Context          $context
56
     * @param UserAdmin        $userAdmin
57
     * @param AdminUser        $adminGetter
58
     * @param Fetcher          $fetcher
59
     * @param IsUsingTwoFactor $isUsingTwoFactor
60
     */
61 14
    public function __construct(
62
        Context $context,
63
        UserAdmin $userAdmin,
64
        AdminUser $adminGetter,
65
        Fetcher $fetcher,
66
        IsUsingTwoFactor $isUsingTwoFactor
67
    ) {
68 14
        parent::__construct($context);
69 14
        $this->userAdmin        = $userAdmin;
70 14
        $this->adminGetter      = $adminGetter;
71 14
        $this->fetcher          = $fetcher;
72 14
        $this->isUsingTwoFactor = $isUsingTwoFactor;
73 14
    }
74
75 14
    public function shouldActionBeRun()
76
    {
77 14
        if ($this->isEnabled() === false) {
78 2
            $this->redirectAction = $this->redirectToDashboard();
79
80 2
            return false;
81
        }
82
83 12
        if ($this->getAdminUser() === false) {
84 4
            $this->redirectAction = $this->handleMissingUser();
85
86 4
            return false;
87
        }
88
89 8
        if ($this->isUserUsingTwoFactor() === false) {
90 2
            $this->redirectAction = $this->redirectToDashboard();
91
92 2
            return false;
93
        }
94
95 6
        return true;
96
    }
97
98 14
    private function isEnabled()
99
    {
100 14
        return ($this->userAdmin->isTwoFactorEnabled() == true);
101
    }
102
103 12
    public function getAdminUser()
104
    {
105 12
        if (null === $this->adminModel) {
106
            try {
107 12
                $this->adminModel = $this->adminGetter->getAdminUser();
108 8
            } catch (\Exception $e) {
109 4
                $this->adminModel = false;
110
            }
111 6
        }
112
113 12
        return $this->adminModel;
114
    }
115
116 8
    private function isUserUsingTwoFactor()
117
    {
118 8
        $user = $this->getAdminUser();
119
120 8
        return $this->isUsingTwoFactor->getValue($user);
121
    }
122
123 4
    private function redirectToDashboard()
124
    {
125 4
        $url = $this->fetcher->getAdminDashboardUrl();
126
127 4
        return $this->redirect($url);
128
    }
129
130 4
    private function handleMissingUser()
131
    {
132 4
        $url = $this->fetcher->getAdminLogInUrl();
133
134 4
        return $this->redirect($url);
135
    }
136
137 12
    public function redirect($path)
138
    {
139 12
        $redirect = $this->resultRedirectFactory->create();
140 12
        $redirect->setPath($path);
141
142 12
        return $redirect;
143
    }
144
145 8
    public function getRedirectAction()
146
    {
147 8
        return $this->redirectAction;
148
    }
149
}
150