/
Url.php
254 lines (227 loc) · 6.2 KB
/
Url.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
/**
* Customer url model
*/
class Url
{
/**
* Route for customer account login page
*/
const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';
/**
* Config name for Redirect Customer to Account Dashboard after Logging in setting
*/
const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD = 'customer/startup/redirect_dashboard';
/**
* Query param name for last url visited
*/
const REFERER_QUERY_PARAM_NAME = 'referer';
/**
* @var UrlInterface
*/
protected $urlBuilder;
/**
* @var RequestInterface
*/
protected $request;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var Session
*/
protected $customerSession;
/**
* @var EncoderInterface
*/
protected $urlEncoder;
/**
* @var \Magento\Framework\Url\DecoderInterface
*/
private $urlDecoder;
/**
* @var \Magento\Framework\Url\HostChecker
*/
private $hostChecker;
/**
* @param Session $customerSession
* @param ScopeConfigInterface $scopeConfig
* @param RequestInterface $request
* @param UrlInterface $urlBuilder
* @param EncoderInterface $urlEncoder
* @param \Magento\Framework\Url\DecoderInterface|null $urlDecoder
* @param \Magento\Framework\Url\HostChecker|null $hostChecker
*/
public function __construct(
Session $customerSession,
ScopeConfigInterface $scopeConfig,
RequestInterface $request,
UrlInterface $urlBuilder,
EncoderInterface $urlEncoder,
\Magento\Framework\Url\DecoderInterface $urlDecoder = null,
\Magento\Framework\Url\HostChecker $hostChecker = null
) {
$this->request = $request;
$this->urlBuilder = $urlBuilder;
$this->scopeConfig = $scopeConfig;
$this->customerSession = $customerSession;
$this->urlEncoder = $urlEncoder;
$this->urlDecoder = $urlDecoder ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Url\DecoderInterface::class);
$this->hostChecker = $hostChecker ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Url\HostChecker::class);
}
/**
* Retrieve customer login url
*
* @return string
*/
public function getLoginUrl()
{
return $this->urlBuilder->getUrl(self::ROUTE_ACCOUNT_LOGIN, $this->getLoginUrlParams());
}
/**
* Retrieve parameters of customer login url
*
* @return array
*/
public function getLoginUrlParams()
{
$params = [];
$referer = $this->getRequestReferrer();
if (!$referer
&& !$this->scopeConfig->isSetFlag(
self::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD,
ScopeInterface::SCOPE_STORE
)
&& !$this->customerSession->getNoReferer()
) {
$referer = $this->urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$referer = $this->urlEncoder->encode($referer);
}
if ($referer) {
$params = [self::REFERER_QUERY_PARAM_NAME => $referer];
}
return $params;
}
/**
* Retrieve customer login POST URL
*
* @return string
*/
public function getLoginPostUrl()
{
$params = [];
$referer = $this->getRequestReferrer();
if ($referer) {
$params = [
self::REFERER_QUERY_PARAM_NAME => $referer,
];
}
return $this->urlBuilder->getUrl('customer/account/loginPost', $params);
}
/**
* Retrieve customer logout url
*
* @return string
*/
public function getLogoutUrl()
{
return $this->urlBuilder->getUrl('customer/account/logout');
}
/**
* Retrieve customer dashboard url
*
* @return string
*/
public function getDashboardUrl()
{
return $this->urlBuilder->getUrl('customer/account');
}
/**
* Retrieve customer account page url
*
* @return string
*/
public function getAccountUrl()
{
return $this->urlBuilder->getUrl('customer/account');
}
/**
* Retrieve customer register form url
*
* @return string
*/
public function getRegisterUrl()
{
return $this->urlBuilder->getUrl('customer/account/create');
}
/**
* Retrieve customer register form post url
*
* @return string
*/
public function getRegisterPostUrl()
{
return $this->urlBuilder->getUrl('customer/account/createpost');
}
/**
* Retrieve customer account edit form url
*
* @return string
*/
public function getEditUrl()
{
return $this->urlBuilder->getUrl('customer/account/edit');
}
/**
* Retrieve customer edit POST URL
*
* @return string
*/
public function getEditPostUrl()
{
return $this->urlBuilder->getUrl('customer/account/editpost');
}
/**
* Retrieve url of forgot password page
*
* @return string
*/
public function getForgotPasswordUrl()
{
return $this->urlBuilder->getUrl('customer/account/forgotpassword');
}
/**
* Retrieve confirmation URL for Email
*
* @param string $email
* @return string
*/
public function getEmailConfirmationUrl($email = null)
{
return $this->urlBuilder->getUrl('customer/account/confirmation', ['_query' => ['email' => $email]]);
}
/**
* @return mixed|null
*/
private function getRequestReferrer()
{
$referer = $this->request->getParam(self::REFERER_QUERY_PARAM_NAME);
if ($referer && $this->hostChecker->isOwnOrigin($this->urlDecoder->decode($referer))) {
return $referer;
}
return null;
}
}