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

TwoFactorUrls::areWeOnTheAuthenticationPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\Model;
23
24
use Magento\Framework\UrlInterface;
25
26
class TwoFactorUrls
27
{
28
    /**
29
     * @var UrlInterface
30
     */
31
    private $url;
32
33
    /**
34
     * TwoFactorUrls constructor.
35
     *
36
     * @param UrlInterface $url
37
     */
38 40
    public function __construct(UrlInterface $url)
39
    {
40 40
        $this->url = $url;
41 40
    }
42
43 22
    public function getCustomerAuthenticationUrl()
44
    {
45 22
        return $this->url->getUrl('twofactor/customerlogin/index');
46
    }
47
48 20
    public function getCustomerVerificationUrl()
49
    {
50 20
        return $this->url->getUrl('twofactor/customerlogin/verify');
51
    }
52
53 2
    public function getCustomerAccountUrl()
54
    {
55 2
        return $this->url->getUrl('customer/account/index');
56
    }
57
58
    public function getAdminDashboardUrl()
59
    {
60
        return $this->url->getUrl('admin/dashboard/index');
61
    }
62
63 2
    public function getCustomerLogInUrl()
64
    {
65 2
        return $this->url->getUrl('customer/account/login');
66
    }
67
68
    public function getAdminAuthenticationUrl()
69
    {
70
        return $this->url->getUrl('twofactor/adminlogin/index');
71
    }
72
73
    public function getAdminVerificationUrl()
74
    {
75
        return $this->url->getUrl('twofactor/adminlogin/verify');
76
    }
77
0 ignored issues
show
introduced by
Code must not contain multiple empty lines in a row; found 2 empty lines.
Loading history...
78
79 22
    public function getCurrentUrl()
80
    {
81 22
        return $this->url->getCurrentUrl();
82
    }
83
84 22
    public function areWeOnTheAuthenticationPage($forAdmin = false)
85
    {
86 22
        $authenticationUrl = $this->getAuthenticationUrl($forAdmin);
87
88 22
        return $this->compareUrls($this->getCurrentUrl(), $authenticationUrl);
89
    }
90
91 22
    public function getAuthenticationUrl($forAdmin = false)
92
    {
93 22
        if ($forAdmin === true) {
94
            return $this->getAdminAuthenticationUrl();
95
        }
96
97 22
        return $this->getCustomerAuthenticationUrl();
98
    }
99
100 20
    public function getVerificationUrl($forAdmin = false)
101
    {
102 20
        if ($forAdmin === true) {
103
            return $this->getAdminVerificationUrl();
104
        }
105
106 20
        return $this->getCustomerVerificationUrl();
107
    }
108
109 20
    public function areWeOnTheVerificationPage($forAdmin = false)
110
    {
111 20
        $verificationUrl = $this->getVerificationUrl($forAdmin);
112
113 20
        return $this->compareUrls($this->getCurrentUrl(), $verificationUrl);
114
    }
115
116 22
    private function compareUrls($firstUrl, $secondUrl)
117
    {
118 22
        return ($this->cleanUrl($firstUrl) === $this->cleanUrl($secondUrl));
119
    }
120
121 22
    private function cleanUrl($url)
122
    {
123 22
        $parts = parse_url($url);
0 ignored issues
show
introduced by
The use of function parse_url() is discouraged
Loading history...
124 22
        $cleanUrl = $parts['host'] . '/' . $parts['path'];
125 22
        $noRewriteUrl = str_replace('/index.php', '', $cleanUrl);
126
127 22
        return trim($noRewriteUrl, '\t\n\r/');
128
    }
129
}
130