Completed
Push — master ( 70bc12...725ad3 )
by Ross
35:29
created

Postdispatch::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 17
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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