Skip to content

Commit 94decab

Browse files
committed
Added sign() helper
1 parent ab8244f commit 94decab

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-cloudfront-url-signer` will be documented in this file.
44

5-
## 0.1.0 - 2018-02-08
5+
## 0.1.1 - 2018-02-09
66

7-
- Pre-release
7+
- Added `sign()` helper
8+
9+
## 0.1.0 - 2018-02-09
10+
11+
- Initial release

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
"autoload": {
3131
"psr-4": {
3232
"Dreamonkey\\CloudFrontUrlSigner\\": "src"
33-
}
33+
},
34+
"files": [
35+
"src/helpers.php"
36+
]
3437
},
3538
"autoload-dev": {
3639
"psr-4": {

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CloudFrontUrlSigner.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(CloudFrontClient $cloudFrontClient, string $privateK
6464
* @return string
6565
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration
6666
*/
67-
public function sign($url, $expiration)
67+
public function sign(string $url, $expiration): string
6868
{
6969
$resourceKey = Http::createFromString($url);
7070

@@ -85,7 +85,7 @@ public function sign($url, $expiration)
8585
*
8686
* @return bool
8787
*/
88-
protected function isFuture($timestamp)
88+
protected function isFuture(int $timestamp): bool
8989
{
9090
return ((int)$timestamp) >= (new DateTime())->getTimestamp();
9191
}
@@ -100,7 +100,7 @@ protected function isFuture($timestamp)
100100
* @return string
101101
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration
102102
*/
103-
protected function getExpirationTimestamp($expiration)
103+
protected function getExpirationTimestamp($expiration): string
104104
{
105105
if (is_int($expiration)) {
106106
$expiration = (new DateTime())->modify((int)$expiration . ' days');

src/UrlSigner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ interface UrlSigner
1212
*
1313
* @return string
1414
*/
15-
public function sign($url, $expiration);
15+
public function sign(string $url, $expiration): string;
1616
}

src/helpers.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Dreamonkey\CloudFrontUrlSigner\UrlSigner;
4+
5+
if (!function_exists('sign')) {
6+
/**
7+
* A helper method to sign an URL using a CloudFront canned policy.
8+
*
9+
* @param string $url
10+
* @param \DateTime|int $expiration
11+
*
12+
* @return string
13+
*/
14+
function sign(string $url, $expiration): string
15+
{
16+
return app(UrlSigner::class)->sign($url, $expiration);
17+
}
18+
}

tests/SignatureGenerationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
class SignatureGenerationTest extends TestCase
1111
{
12-
private $dummyUrl = 'http://myapp.com';
1312
private $dummyPrivateKeyPath = 'dummy/path/key.pem';
1413
private $dummyKeyPairId = 'dummyKeyPairId';
14+
private $dummyUrl = 'http://myapp.com';
1515

1616
private $mockCloudFrontClient;
1717

@@ -54,7 +54,7 @@ public function it_will_throw_an_exception_for_an_empty_private_key_path()
5454
}
5555

5656
/** @test */
57-
public function it_can_sign_a_signed_url_that_expires_at_a_certain_time()
57+
public function it_can_sign_an_url_that_expires_at_a_certain_time()
5858
{
5959
$expiration = DateTime::createFromFormat('d/m/Y H:i:s', '10/08/2115 18:15:44',
6060
new DateTimeZone('Europe/Brussels'));
@@ -70,7 +70,7 @@ public function it_can_sign_a_signed_url_that_expires_at_a_certain_time()
7070
}
7171

7272
/** @test */
73-
public function it_can_sign_a_signed_url_that_expires_after_a_relative_amount_of_days()
73+
public function it_can_sign_an_url_that_expires_after_a_relative_amount_of_days()
7474
{
7575
$expiration = 30;
7676

0 commit comments

Comments
 (0)