Skip to content

Commit ba12408

Browse files
committed
Adds support for optional secret to sign urls or not
1 parent 09b7193 commit ba12408

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/UrlBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class UrlBuilder
1818

1919
private $params = [];
2020

21-
public function __construct($domain, $secret)
21+
public function __construct($domain, $secret = null)
2222
{
2323
$this->domain = $domain;
2424
$this->secret = $secret;

src/UrlHelper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class UrlHelper
1414
private $params;
1515
private $secret;
1616

17-
public function __construct($path, $params, $secret)
17+
public function __construct($path, $params, $secret = null)
1818
{
1919
$this->path = $path;
2020
$this->params = $params;
@@ -29,8 +29,11 @@ private function getQuery()
2929
$query .= '?' . http_build_query($this->params);
3030
}
3131

32+
$signed = "s=" . md5($this->secret . $query);
3233

33-
return $query . (count($this->params) > 0 ? '&' : '?') . "s=" . md5($this->secret . $query);
34+
35+
36+
return $query . (empty($this->secret) ? "" : (count($this->params) > 0 ? '&' : '?') . $signed);
3437
}
3538

3639
public function __toString()

tests/src/UrlTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,20 @@ public function testUrlWithOptions()
4747
$this->assertEquals($expected, $url);
4848
}
4949

50+
public function testUnsignedURL()
51+
{
52+
53+
$urlBuilder = $this->urlBuilder = new UrlBuilder('image.barnebys.sh');
54+
$urlBuilder
55+
->setHeight(200)
56+
->setWidth(200)
57+
->setPath('https://dummyimage.com/600x400/000/fff');
58+
59+
60+
$url = $urlBuilder->createURL();
61+
$expected = 'https://image.barnebys.sh/https%3A%2F%2Fdummyimage.com%2F600x400%2F000%2Ffff?h=200&w=200';
62+
63+
$this->assertEquals($expected, $url);
64+
}
5065

5166
}

0 commit comments

Comments
 (0)