Skip to content

Commit

Permalink
Link Builder to redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Apr 30, 2020
1 parent 2cfeea7 commit 1115895
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/LinkBuilder/LinkBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Packaged\Http\LinkBuilder;

use Packaged\Http\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class LinkBuilder
{
Expand Down Expand Up @@ -36,8 +37,7 @@ public function asUrl(): string
$scheme = $this->_scheme ?? ($this->_request->isSecure(true) ? 'https' : 'http');
$port = $this->_port ?? $this->_request->port();
return
($scheme . '://')
. implode(
($scheme . '://') . implode(
'.',
[
($this->_subDomain ?? $this->_request->subDomain()),
Expand Down Expand Up @@ -144,4 +144,9 @@ public function addQuery($key, $value)
$this->_query[$key] = $value;
return $this;
}

public function toRedirect($status = 302, $headers = [])
{
return RedirectResponse::create($this->asUrl(), $status, $headers);
}
}
5 changes: 5 additions & 0 deletions tests/LinkBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Packaged\Http\LinkBuilder\LinkBuilder;
use Packaged\Http\Request;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;

class LinkBuilderTest extends TestCase
{
Expand Down Expand Up @@ -59,6 +60,10 @@ public function testAsUrl()
$lb->setQuery(['a' => 1, 'b' => 2]);
self::assertEquals('https://secure.cubex.com/order?a=1&b=2', $lb);

$resp = $lb->toRedirect(301);
$this->assertInstanceOf(RedirectResponse::class, $resp);
$this->assertEquals(301, $resp->getStatusCode());

$request = Request::create('http://www.packaged.local/');
$request->headers->set('X_FORWARDED_PROTO', 'https');
$this->assertFalse($request->isSecure());
Expand Down

0 comments on commit 1115895

Please sign in to comment.