Skip to content

Commit f9fb5e9

Browse files
committed
Simplify redirectresponse method
1 parent d7e1e7e commit f9fb5e9

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

src/Omnipay/Common/Message/AbstractResponse.php

+49-37
Original file line numberDiff line numberDiff line change
@@ -205,51 +205,63 @@ public function redirect()
205205
*/
206206
public function getRedirectResponse()
207207
{
208-
if (!$this instanceof RedirectResponseInterface || !$this->isRedirect()) {
209-
throw new RuntimeException('This response does not support redirection.');
208+
$this->validateRedirect();
209+
210+
if ('GET' === $this->getRedirectMethod()) {
211+
return HttpRedirectResponse::create($this->getRedirectUrl());
210212
}
211213

212-
if (empty($this->getRedirectUrl())) {
213-
throw new RuntimeException('The given redirectUrl cannot be empty.');
214+
$hiddenFields = '';
215+
foreach ($this->getRedirectData() as $key => $value) {
216+
$hiddenFields .= sprintf(
217+
'<input type="hidden" name="%1$s" value="%2$s" />',
218+
htmlentities($key, ENT_QUOTES, 'UTF-8', false),
219+
htmlentities($value, ENT_QUOTES, 'UTF-8', false)
220+
)."\n";
214221
}
215222

216-
if ('GET' === $this->getRedirectMethod()) {
217-
return HttpRedirectResponse::create($this->getRedirectUrl());
218-
} elseif ('POST' === $this->getRedirectMethod()) {
219-
$hiddenFields = '';
220-
foreach ($this->getRedirectData() as $key => $value) {
221-
$hiddenFields .= sprintf(
222-
'<input type="hidden" name="%1$s" value="%2$s" />',
223-
htmlentities($key, ENT_QUOTES, 'UTF-8', false),
224-
htmlentities($value, ENT_QUOTES, 'UTF-8', false)
225-
)."\n";
226-
}
227-
228-
$output = '<!DOCTYPE html>
223+
$output = '<!DOCTYPE html>
229224
<html>
230-
<head>
231-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
232-
<title>Redirecting...</title>
233-
</head>
234-
<body onload="document.forms[0].submit();">
235-
<form action="%1$s" method="post">
236-
<p>Redirecting to payment page...</p>
237-
<p>
238-
%2$s
239-
<input type="submit" value="Continue" />
240-
</p>
241-
</form>
242-
</body>
225+
<head>
226+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
227+
<title>Redirecting...</title>
228+
</head>
229+
<body onload="document.forms[0].submit();">
230+
<form action="%1$s" method="post">
231+
<p>Redirecting to payment page...</p>
232+
<p>
233+
%2$s
234+
<input type="submit" value="Continue" />
235+
</p>
236+
</form>
237+
</body>
243238
</html>';
244-
$output = sprintf(
245-
$output,
246-
htmlentities($this->getRedirectUrl(), ENT_QUOTES, 'UTF-8', false),
247-
$hiddenFields
248-
);
239+
$output = sprintf(
240+
$output,
241+
htmlentities($this->getRedirectUrl(), ENT_QUOTES, 'UTF-8', false),
242+
$hiddenFields
243+
);
249244

250-
return HttpResponse::create($output);
245+
return HttpResponse::create($output);
246+
}
247+
248+
/**
249+
* Validate that the current Response is a valid redirect.
250+
*
251+
* @return void
252+
*/
253+
protected function validateRedirect()
254+
{
255+
if (!$this instanceof RedirectResponseInterface || !$this->isRedirect()) {
256+
throw new RuntimeException('This response does not support redirection.');
251257
}
252258

253-
throw new RuntimeException('Invalid redirect method "'.$this->getRedirectMethod().'".');
259+
if (empty($this->getRedirectUrl())) {
260+
throw new RuntimeException('The given redirectUrl cannot be empty.');
261+
}
262+
263+
if (!in_array($this->getRedirectMethod(), ['GET', 'POST'])) {
264+
throw new RuntimeException('Invalid redirect method "'.$this->getRedirectMethod().'".');
265+
}
254266
}
255267
}

0 commit comments

Comments
 (0)