Passed
Push — master ( b162a4...87a689 )
by Ross
03:05
created

Postdispatch::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 7
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\Customer\Attribute\IsUsingTwoFactor;
30
use Rossmitchell\Twofactor\Model\Customer\Customer;
31
use Rossmitchell\Twofactor\Model\Customer\Session;
32
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
33
use Rossmitchell\Twofactor\Model\TwoFactorUrls;
34
35
class Postdispatch implements ObserverInterface
36
{
37
    /**
38
     * @var ResponseFactory
39
     */
40
    private $responseFactory;
41
    /**
42
     * @var UrlInterface
43
     */
44
    private $url;
45
    /**
46
     * @var Customer
47
     */
48
    private $customerGetter;
49
    /**
50
     * @var IsUsingTwoFactor
51
     */
52
    private $isUsingTwoFactor;
53
    /**
54
     * @var IsVerified
55
     */
56
    private $isVerified;
57
    /**
58
     * @var TwoFactorUrls
59
     */
60
    private $twoFactorUrls;
61
    /**
62
     * @var Session
63
     */
64
    private $customerSession;
65
66
    /**
67
     * Predispatch constructor.
68
     *
69
     * @param ResponseFactory  $responseFactory
70
     * @param UrlInterface     $url
71
     * @param Customer         $customerGetter
72
     * @param IsVerified       $isVerified
73
     * @param Session          $customerSession
74
     * @param IsUsingTwoFactor $isUsingTwoFactor
75
     * @param TwoFactorUrls    $twoFactorUrls
76
     */
77 View Code Duplication
    public function __construct(
78
        ResponseFactory $responseFactory,
79
        UrlInterface $url,
80
        Customer $customerGetter,
81
        IsVerified $isVerified,
82
        Session $customerSession,
83
        IsUsingTwoFactor $isUsingTwoFactor,
84
        TwoFactorUrls $twoFactorUrls
85
    ) {
86
        $this->responseFactory  = $responseFactory;
87
        $this->url              = $url;
88
        $this->customerGetter   = $customerGetter;
89
        $this->isUsingTwoFactor = $isUsingTwoFactor;
90
        $this->isVerified       = $isVerified;
91
        $this->twoFactorUrls    = $twoFactorUrls;
92
        $this->customerSession = $customerSession;
93
    }
94
95
    /**
96
     * @param Observer $observer
97
     *
98
     * @return void
99
     */
100
    public function execute(Observer $observer)
101
    {
102
        if ($this->shouldTheCustomerBeRedirected() === false) {
103
            return;
104
        }
105
106
        if ($this->hasTwoFactorBeenChecked() === true) {
107
            return;
108
        }
109
110
        $controller = $observer->getEvent()->getData('controller_action');
111
        $this->redirectToTwoFactorCheck($controller);
112
    }
113
114
    private function shouldTheCustomerBeRedirected()
115
    {
116
        if ($this->areWeOnAnAllowedPage() === true) {
117
            return false;
118
        }
119
120
        $customer = $this->customerGetter->getCustomer();
121
        if ($customer === false) {
122
            return false;
123
        }
124
        $usingTwoFactor = $this->isUsingTwoFactor->getValue($customer);
125
        if ($usingTwoFactor === false) {
126
            return false;
127
        }
128
129
        return true;
130
    }
131
132 View Code Duplication
    private function areWeOnAnAllowedPage()
133
    {
134
        $twoFactorUrls = $this->twoFactorUrls;
135
        if ($twoFactorUrls->areWeOnTheAuthenticationPage(false) === true) {
136
            return true;
137
        }
138
139
        if ($twoFactorUrls->areWeOnTheVerificationPage(false) === true) {
140
            return true;
141
        }
142
143
        return false;
144
    }
145
146
    private function hasTwoFactorBeenChecked()
147
    {
148
        $session = $this->customerSession;
149
        $checked = $this->isVerified->isVerified($session);
150
151
        return ($checked === true);
152
    }
153
154
    private function redirectToTwoFactorCheck(Action $controller)
155
    {
156
        $twoFactorCheckUrl = $this->twoFactorUrls->getAuthenticationUrl(false);
157
        $response          = $controller->getResponse();
158
        $response->setRedirect($twoFactorCheckUrl);
159
    }
160
}
161