Skip to content

Commit efacb55

Browse files
author
bryanadamloh97
committed
Added TinyURL driver
1 parent 6ea79de commit efacb55

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

config/url-shortener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
'bit_ly' => [
88
'driver' => 'bit_ly',
99
'domain' => env('URL_SHORTENER_PREFIX', 'bit.ly'),
10-
'token' => env('URL_SHORTENER_API_TOKEN'),
10+
'token' => env('URL_SHORTENER_API_TOKEN')
11+
],
12+
'tiny_url' => [
13+
'driver' => 'tiny_url',
14+
'domain' => env('URL_SHORTENER_PREFIX', 'tinyurl.com'),
15+
'token' => env('URL_SHORTENER_API_TOKEN')
1116
]
1217
]
1318
];

src/Drivers/BitLyDriverShortener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function shortenAsync($url, array $options = [])
3838
$request = new Request('POST', '/v4/shorten');
3939

4040
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
41-
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->link);
41+
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->link);
4242
});
4343
}
4444
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
4+
5+
use GuzzleHttp\ClientInterface;
6+
use GuzzleHttp\Psr7\Request;
7+
use Illuminate\Support\Arr;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class TinyUrlDriverShortener extends DriverShortener
11+
{
12+
protected $client;
13+
protected $object;
14+
15+
public function __construct(ClientInterface $client, $token, $domain)
16+
{
17+
$this->client = $client;
18+
$this->object = [
19+
'allow_redirects' => false,
20+
'base_uri' => 'https://api.tinyurl.com',
21+
'headers' => [
22+
'Accept' => 'application/json',
23+
'Authorization' => "Bearer {$token}",
24+
'Content-Type' => 'application/json'
25+
],
26+
'json' => [
27+
'domain' => $domain
28+
]
29+
];
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function shortenAsync($url, array $options = [])
36+
{
37+
$options = array_merge_recursive(Arr::add($this->object, 'json.url', $url), $options);
38+
$request = new Request('POST', '/create');
39+
40+
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
41+
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->data->tiny_url);
42+
});
43+
}
44+
}

src/UrlShortener.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CodeOfDigital\LaravelUrlShortener\Contracts\UrlFactory;
66
use CodeOfDigital\LaravelUrlShortener\Drivers\BitLyDriverShortener;
7+
use CodeOfDigital\LaravelUrlShortener\Drivers\TinyUrlDriverShortener;
78
use GuzzleHttp\ClientInterface;
89
use http\Exception\InvalidArgumentException;
910
use Illuminate\Foundation\Application;
@@ -53,6 +54,21 @@ protected function createBitLyDriver(array $config)
5354
);
5455
}
5556

57+
/**
58+
* Create an instance of TinyURL driver
59+
*
60+
* @param array $config
61+
* @return TinyUrlDriverShortener
62+
*/
63+
protected function createTinyUrlDriver(array $config)
64+
{
65+
return new TinyUrlDriverShortener(
66+
$this->app->make(ClientInterface::class),
67+
Arr::get($config, 'token'),
68+
Arr::get($config, 'domain', 'tinyurl.com')
69+
);
70+
}
71+
5672
/**
5773
* Get the default URL shortener driver
5874
*

src/UrlShortenerServiceProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ protected function publishAssets()
2727

2828
protected function registerMacros()
2929
{
30+
if (!class_exists(UrlGenerator::class) || !method_exists(UrlGenerator::class, 'macro'))
31+
return;
32+
3033
UrlGenerator::macro('shorten', function (...$parameters) {
31-
return app('url.shortener')->shorten(...$parameters);
34+
return app('url.shortener')->shorten(...$parameters);
3235
});
3336

3437
UrlGenerator::macro('shortenAsync', function (...$parameters) {
35-
return app('url.shortener')->shortenAsync(...$parameters);
38+
return app('url.shortener')->shortenAsync(...$parameters);
39+
});
40+
41+
UrlGenerator::macro('shortener', function () {
42+
return app('url.shortener');
3643
});
3744
}
3845

0 commit comments

Comments
 (0)