Skip to content

Commit 15b1c7d

Browse files
author
bryanadamloh97
committed
Add checking for throw Short URL exception
1 parent c17704b commit 15b1c7d

9 files changed

+51
-2
lines changed

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@
3636
},
3737
"autoload": {
3838
"psr-4": {
39-
"CodeOfDigital\\LaravelUrlShortener\\": "src"
39+
"CodeOfDigital\\LaravelUrlShortener\\": "src/"
40+
}
41+
},
42+
"extra": {
43+
"laravel": {
44+
"providers": [
45+
"CodeOfDigital\\LaravelUrlShortener\\UrlShortenerServiceProvider"
46+
],
47+
"aliases": {
48+
"UrlShortener": "CodeOfDigital\\LaravelUrlShortener\\Facades\\UrlShortener"
49+
}
4050
}
4151
}
4252
}

src/Contracts/AsyncShortener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface AsyncShortener extends Shortener
99
/**
1010
* Shorten the given URL asynchronously
1111
*
12-
* @param string $url
12+
* @param $url
1313
* @param array $options
1414
* @return PromiseInterface
1515
*/

src/Drivers/BitLyDriverShortener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
77
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
88
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
9+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
910
use GuzzleHttp\ClientInterface;
1011
use GuzzleHttp\Exception\RequestException;
1112
use GuzzleHttp\Psr7\Request;
1213
use Illuminate\Support\Arr;
14+
use Illuminate\Support\Str;
1315
use Psr\Http\Message\ResponseInterface;
1416

1517
class BitLyDriverShortener extends DriverShortener
@@ -39,6 +41,9 @@ public function __construct(ClientInterface $client, $token, $domain)
3941
*/
4042
public function shortenAsync($url, array $options = [])
4143
{
44+
if (!Str::startsWith($url, ['http://', 'https://']))
45+
throw new ShortUrlException('The given URL must begin with http:// or https://');
46+
4247
$options = array_merge_recursive(Arr::add($this->object, 'json.long_url', $url), ['json' => $options]);
4348
$request = new Request('POST', '/v4/shorten');
4449

src/Drivers/CuttLyDriverShortener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
77
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
88
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
9+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
910
use GuzzleHttp\ClientInterface;
1011
use GuzzleHttp\Exception\RequestException;
1112
use GuzzleHttp\Psr7\Request;
1213
use Illuminate\Support\Arr;
14+
use Illuminate\Support\Str;
1315
use Psr\Http\Message\ResponseInterface;
1416

1517
class CuttLyDriverShortener extends DriverShortener
@@ -34,6 +36,9 @@ public function __construct(ClientInterface $client, $token)
3436
*/
3537
public function shortenAsync($url, array $options = [])
3638
{
39+
if (!Str::startsWith($url, ['http://', 'https://']))
40+
throw new ShortUrlException('The given URL must begin with http:// or https://');
41+
3742
$options = array_merge_recursive(Arr::add($this->object, 'query.short', urlencode($url)), ['query' => $options]);
3843
$request = new Request('GET', '/api/api.php');
3944

src/Drivers/DriverShortener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
44

55
use CodeOfDigital\LaravelUrlShortener\Contracts\AsyncShortener;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
7+
use Illuminate\Support\Str;
68

79
abstract class DriverShortener implements AsyncShortener
810
{
911
abstract protected function getErrorMessage($code, $message = null);
1012

1113
public function shorten($url, array $options = [])
1214
{
15+
if (!Str::startsWith($url, ['http://', 'https://']))
16+
throw new ShortUrlException('The given URL must begin with http:// or https://');
17+
1318
return $this->shortenAsync($url, $options)->wait();
1419
}
1520
}

src/Drivers/IsGdDriverShortener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
66
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
77
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
89
use GuzzleHttp\ClientInterface;
910
use GuzzleHttp\Exception\RequestException;
1011
use GuzzleHttp\Psr7\Request;
1112
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Str;
1214
use Psr\Http\Message\ResponseInterface;
1315

1416
class IsGdDriverShortener extends DriverShortener
@@ -34,6 +36,9 @@ public function __construct(ClientInterface $client, $statistic)
3436
*/
3537
public function shortenAsync($url, array $options = [])
3638
{
39+
if (!Str::startsWith($url, ['http://', 'https://']))
40+
throw new ShortUrlException('The given URL must begin with http:// or https://');
41+
3742
$options = array_merge_recursive(Arr::add($this->object, 'query.url', $url), ['query' => $options]);
3843
$request = new Request('GET', '/create.php');
3944

src/Drivers/ShorteStDriverShortener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
66
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
77
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
89
use GuzzleHttp\ClientInterface;
910
use GuzzleHttp\Exception\RequestException;
1011
use GuzzleHttp\Psr7\Request;
1112
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Str;
1214
use Psr\Http\Message\ResponseInterface;
1315

1416
class ShorteStDriverShortener extends DriverShortener
@@ -37,6 +39,9 @@ public function __construct(ClientInterface $client, $token)
3739
*/
3840
public function shortenAsync($url, array $options = [])
3941
{
42+
if (!Str::startsWith($url, ['http://', 'https://']))
43+
throw new ShortUrlException('The given URL must begin with http:// or https://');
44+
4045
$options = array_merge_recursive(Arr::add($this->object, 'json.urlToShorten', $url), ['json' => $options]);
4146
$request = new Request('PUT', '/v1/data/url');
4247

src/Drivers/TinyUrlDriverShortener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
88
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
99
use CodeOfDigital\LaravelUrlShortener\Exceptions\MethodNotAllowedException;
10+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
1011
use GuzzleHttp\ClientInterface;
1112
use GuzzleHttp\Exception\RequestException;
1213
use GuzzleHttp\Psr7\Request;
1314
use Illuminate\Support\Arr;
15+
use Illuminate\Support\Str;
1416
use Psr\Http\Message\ResponseInterface;
1517

1618
class TinyUrlDriverShortener extends DriverShortener
@@ -40,6 +42,9 @@ public function __construct(ClientInterface $client, $token, $domain)
4042
*/
4143
public function shortenAsync($url, array $options = [])
4244
{
45+
if (!Str::startsWith($url, ['http://', 'https://']))
46+
throw new ShortUrlException('The given URL must begin with http:// or https://');
47+
4348
$options = array_merge_recursive(Arr::add($this->object, 'json.url', $url), ['json' => $options]);
4449
$request = new Request('POST', '/create');
4550

src/Exceptions/ShortUrlException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Exceptions;
4+
5+
use Exception;
6+
7+
class ShortUrlException extends Exception
8+
{
9+
}

0 commit comments

Comments
 (0)