Skip to content

Commit d90c996

Browse files
author
bryanadamloh97
committed
Added Bit.ly driver instance for URL shortener
1 parent 4dbba73 commit d90c996

File tree

6 files changed

+165
-6
lines changed

6 files changed

+165
-6
lines changed

src/Contracts/AsyncShortener.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Contracts;
4+
5+
use GuzzleHttp\Promise\PromiseInterface;
6+
7+
interface AsyncShortener extends Shortener
8+
{
9+
/**
10+
* Shorten the given URL asynchronously
11+
*
12+
* @param string $url
13+
* @param array $options
14+
* @return PromiseInterface
15+
*/
16+
public function shortenAsync($url, array $options = []);
17+
}

src/Contracts/Shortener.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Contracts;
4+
5+
interface Shortener
6+
{
7+
/**
8+
* Shorten the given URL
9+
*
10+
* @param string $url
11+
* @param array $options
12+
* @return string
13+
*/
14+
public function shorten($url, array $options = []);
15+
}
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 BitLyDriverShortener 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-ssl.bitly.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.long_url', $url), $options);
38+
$request = new Request('POST', '/v4/shorten');
39+
40+
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
41+
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->link);
42+
});
43+
}
44+
}

src/Drivers/DriverShortener.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
4+
5+
use CodeOfDigital\LaravelUrlShortener\Contracts\AsyncShortener;
6+
7+
abstract class DriverShortener implements AsyncShortener
8+
{
9+
public function shorten($url, array $options = [])
10+
{
11+
return $this->shortenAsync($url, $options)->wait();
12+
}
13+
}

src/UrlShortenerInstance.php renamed to src/UrlShortener.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace CodeOfDigital\LaravelUrlShortener;
44

55
use CodeOfDigital\LaravelUrlShortener\Contracts\UrlFactory;
6+
use CodeOfDigital\LaravelUrlShortener\Drivers\BitLyDriverShortener;
7+
use GuzzleHttp\ClientInterface;
68
use http\Exception\InvalidArgumentException;
79
use Illuminate\Foundation\Application;
10+
use Illuminate\Support\Arr;
811
use Illuminate\Support\Str;
912

10-
class UrlShortenerInstance implements UrlFactory
13+
class UrlShortener implements UrlFactory
1114
{
1215
protected $app;
1316
protected $shorteners;
@@ -23,6 +26,33 @@ public function __construct(Application $app)
2326
$this->shorteners = [];
2427
}
2528

29+
/**
30+
* Dynamically call the default driver instance
31+
*
32+
* @param $method
33+
* @param $parameters
34+
* @return mixed
35+
*/
36+
public function __call($method, $parameters)
37+
{
38+
return $this->driver()->$method(...$parameters);
39+
}
40+
41+
/**
42+
* Create an instance of Bit.ly driver
43+
*
44+
* @param array $config
45+
* @return BitLyDriverShortener
46+
*/
47+
protected function createBitLyDriver(array $config)
48+
{
49+
return new BitLyDriverShortener(
50+
$this->app->make(ClientInterface::class),
51+
Arr::get($config, 'token'),
52+
Arr::get($config, 'domain', 'bit.ly')
53+
);
54+
}
55+
2656
/**
2757
* Get the default URL shortener driver
2858
*
@@ -38,18 +68,41 @@ public function getUrlDefaultDriver(): string
3868
*
3969
* @return $this
4070
*/
41-
public function setUrlDefaultDriver($name): UrlShortenerInstance
71+
public function setUrlDefaultDriver($name): UrlShortener
4272
{
4373
$this->app['config']['url-shortener.default'] = $name;
4474
return $this;
4575
}
4676

47-
public function getUrlShortenerConfig($name)
77+
/**
78+
* Get the URL shortener configuration in array form
79+
*
80+
* @param $name
81+
* @return mixed
82+
*/
83+
protected function getUrlShortenerConfig($name)
84+
{
85+
return $this->app['config']["url-shortener.shorteners.{$name}"];
86+
}
87+
88+
/**
89+
* Get a URL shortener driver instance
90+
*
91+
* @param null $name
92+
* @return mixed
93+
*/
94+
public function driver($name = null)
4895
{
49-
return config("url-shortener.shorteners.{$name}");
96+
return $this->shortener($name);
5097
}
5198

52-
public function resolveUrlDriver($name)
99+
/**
100+
* Resolve the URL driver by creating an instance of the given URL shortener
101+
*
102+
* @param $name
103+
* @return mixed
104+
*/
105+
protected function resolveUrlDriver($name)
53106
{
54107
$config = $this->getUrlShortenerConfig($name);
55108

src/UrlShortenerServiceProvider.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace CodeOfDigital\LaravelUrlShortener;
44

5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\ClientInterface;
7+
use Illuminate\Routing\UrlGenerator;
58
use Illuminate\Support\ServiceProvider;
69
use Illuminate\Support\Str;
710

@@ -11,6 +14,7 @@ public function boot()
1114
{
1215
$this->mergeConfigFrom(__DIR__.'/../config/url-shortener.php', 'url-shortener');
1316
$this->publishAssets();
17+
$this->registerMacros();
1418
}
1519

1620
protected function publishAssets()
@@ -21,10 +25,23 @@ protected function publishAssets()
2125
$this->publishes([__DIR__.'/../config/url-shortener.php' => config_path('url-shortener.php')]);
2226
}
2327

28+
protected function registerMacros()
29+
{
30+
UrlGenerator::macro('shorten', function (...$parameters) {
31+
return app('url.shortener')->shorten(...$parameters);
32+
});
33+
34+
UrlGenerator::macro('shortenAsync', function (...$parameters) {
35+
return app('url.shortener')->shortenAsync(...$parameters);
36+
});
37+
}
38+
2439
public function register()
2540
{
41+
$this->app->alias('url.shortener', UrlShortener::class);
42+
$this->app->bindIf(ClientInterface::class, Client::class);
2643
$this->app->singleton('url.shortener', function ($app) {
27-
return new UrlShortenerInstance($app);
44+
return new UrlShortener($app);
2845
});
2946
}
3047
}

0 commit comments

Comments
 (0)