Skip to content

Commit 73352f2

Browse files
author
bryanadamloh97
committed
Added Firebase driver
1 parent ef5f113 commit 73352f2

9 files changed

+111
-15
lines changed

config/url-shortener.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
'cutt_ly' => [
3131
'driver' => 'cutt_ly',
3232
'token' => env('URL_SHORTENER_API_TOKEN')
33+
],
34+
35+
'firebase' => [
36+
'driver' => 'firebase',
37+
'domain' => env('URL_SHORTENER_PREFIX'),
38+
'token' => env('URL_SHORTENER_API_TOKEN'),
39+
'suffix' => env('URL_SHORTENER_SUFFIX', 'UNGUESSABLE')
3340
]
3441
]
3542
];

src/Drivers/BitLyDriverShortener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(ClientInterface $client, $token, $domain)
4242
public function shortenAsync($url, array $options = [])
4343
{
4444
if (!Str::startsWith($url, ['http://', 'https://']))
45-
throw new ShortUrlException('The given URL must begin with http:// or https://');
45+
throw new ShortUrlException('The given URL must begin with http/https');
4646

4747
$options = array_merge_recursive(Arr::add($this->object, 'json.long_url', $url), ['json' => $options]);
4848
$request = new Request('POST', '/v4/shorten');
@@ -52,7 +52,8 @@ function (ResponseInterface $response) {
5252
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->link);
5353
},
5454
function (RequestException $e) {
55-
$this->getErrorMessage($e->getCode(), $e->getMessage());
55+
$contents = json_decode($e->getResponse()->getBody()->getContents());
56+
$this->getErrorMessage($e->getCode(), $contents->description ?? $contents->message);
5657
}
5758
);
5859
}

src/Drivers/CuttLyDriverShortener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(ClientInterface $client, $token)
3838
public function shortenAsync($url, array $options = [])
3939
{
4040
if (!Str::startsWith($url, ['http://', 'https://']))
41-
throw new ShortUrlException('The given URL must begin with http:// or https://');
41+
throw new ShortUrlException('The given URL must begin with http/https');
4242

4343
$options = array_merge_recursive(Arr::add($this->object, 'query.short', $url), ['query' => $options]);
4444
$request = new Request('GET', '/api/api.php');
@@ -51,7 +51,7 @@ function (ResponseInterface $response) {
5151
return str_replace('http://', 'https://', $data->url->shortLink);
5252
},
5353
function (RequestException $e) {
54-
$this->getErrorMessage($e->getCode(), $e->getMessage());
54+
$this->getErrorMessage($e->getCode(), $e->getResponse()->getBody()->getContents());
5555
}
5656
);
5757
}

src/Drivers/DriverShortener.php

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

55
use CodeOfDigital\LaravelUrlShortener\Contracts\AsyncShortener;
6-
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
7-
use Illuminate\Support\Str;
86

97
abstract class DriverShortener implements AsyncShortener
108
{
119
abstract protected function getErrorMessage($code, $message = null);
1210

1311
public function shorten($url, array $options = [])
1412
{
15-
if (!Str::startsWith($url, ['http://', 'https://']))
16-
throw new ShortUrlException('The given URL must begin with http:// or https://');
17-
1813
return $this->shortenAsync($url, $options)->wait();
1914
}
2015
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
4+
5+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\TooManyRequestException;
9+
use GuzzleHttp\ClientInterface;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\Psr7\Request;
12+
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Str;
14+
use Psr\Http\Message\ResponseInterface;
15+
16+
class FirebaseDriverShortener extends DriverShortener
17+
{
18+
protected $client;
19+
protected $object;
20+
21+
public function __construct(ClientInterface $client, $token, $domain, $suffix)
22+
{
23+
$this->client = $client;
24+
$this->object = [
25+
'allow_redirects' => false,
26+
'base_uri' => 'https://firebasedynamiclinks.googleapis.com',
27+
'headers' => [
28+
'Content-Type' => 'application/json',
29+
'Accept' => 'application/json'
30+
],
31+
'query' => [
32+
'key' => $token
33+
],
34+
'json' => [
35+
'dynamicLinkInfo' => [
36+
'domainUriPrefix' => $domain
37+
],
38+
'suffix' => [
39+
'option' => $suffix
40+
]
41+
]
42+
];
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function shortenAsync($url, array $options = [])
49+
{
50+
if (!Str::startsWith($url, ['http://', 'https://']))
51+
throw new ShortUrlException('The given URL must begin with http/https');
52+
53+
$options = array_merge_recursive(Arr::add($this->object, 'json.dynamicLinkInfo.link', $url), ['json' => ['dynamicLinkInfo' => $options]]);
54+
$request = new Request('POST', '/v1/shortLinks');
55+
56+
return $this->client->sendAsync($request, $options)->then(
57+
function (ResponseInterface $response) {
58+
return json_decode($response->getBody()->getContents())->shortLink;
59+
},
60+
function (RequestException $e) {
61+
$this->getErrorMessage($e->getCode(), json_decode($e->getResponse()->getBody()->getContents())->error->message);
62+
}
63+
);
64+
}
65+
66+
protected function getErrorMessage($code, $message = null)
67+
{
68+
switch ($code) {
69+
case 400: throw new BadRequestException($message);
70+
case 429: throw new TooManyRequestException($message);
71+
default: throw new InvalidResponseException($message);
72+
}
73+
}
74+
}

src/Drivers/IsGdDriverShortener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(ClientInterface $client, $statistic)
3737
public function shortenAsync($url, array $options = [])
3838
{
3939
if (!Str::startsWith($url, ['http://', 'https://']))
40-
throw new ShortUrlException('The given URL must begin with http:// or https://');
40+
throw new ShortUrlException('The given URL must begin with http/https');
4141

4242
$options = array_merge_recursive(Arr::add($this->object, 'query.url', $url), ['query' => $options]);
4343
$request = new Request('GET', '/create.php');
@@ -47,7 +47,7 @@ function (ResponseInterface $response) {
4747
return str_replace('http://', 'https://', $response->getBody()->getContents());
4848
},
4949
function (RequestException $e) {
50-
$this->getErrorMessage($e->getCode(), $e->getMessage());
50+
$this->getErrorMessage($e->getCode(), $e->getResponse()->getBody()->getContents());
5151
}
5252
);
5353
}

src/Drivers/ShorteStDriverShortener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function __construct(ClientInterface $client, $token)
4040
*/
4141
public function shortenAsync($url, array $options = [])
4242
{
43-
if (!Str::startsWith($url, ['http://', 'https://']))
44-
throw new ShortUrlException('The given URL must begin with http:// or https://');
43+
/*if (!Str::startsWith($url, ['http://', 'https://']))
44+
throw new ShortUrlException('The given URL must begin with http/https');*/
4545

4646
$options = array_merge_recursive(Arr::add($this->object, 'json.urlToShorten', $url), ['json' => $options]);
4747
$request = new Request('PUT', '/v1/data/url');

src/Drivers/TinyUrlDriverShortener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(ClientInterface $client, $token, $domain)
4343
public function shortenAsync($url, array $options = [])
4444
{
4545
if (!Str::startsWith($url, ['http://', 'https://']))
46-
throw new ShortUrlException('The given URL must begin with http:// or https://');
46+
throw new ShortUrlException('The given URL must begin with http/https');
4747

4848
$options = array_merge_recursive(Arr::add($this->object, 'json.url', $url), ['json' => $options]);
4949
$request = new Request('POST', '/create');
@@ -53,7 +53,9 @@ function (ResponseInterface $response) {
5353
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->data->tiny_url);
5454
},
5555
function (RequestException $e) {
56-
$this->getErrorMessage($e->getCode(), $e->getMessage());
56+
$contents = json_decode($e->getResponse()->getBody()->getContents());
57+
$errorMessage = implode(' ', $contents->errors);
58+
$this->getErrorMessage($e->getCode(), $errorMessage);
5759
}
5860
);
5961
}

src/UrlShortener.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use CodeOfDigital\LaravelUrlShortener\Contracts\UrlFactory;
66
use CodeOfDigital\LaravelUrlShortener\Drivers\BitLyDriverShortener;
77
use CodeOfDigital\LaravelUrlShortener\Drivers\CuttLyDriverShortener;
8+
use CodeOfDigital\LaravelUrlShortener\Drivers\FirebaseDriverShortener;
89
use CodeOfDigital\LaravelUrlShortener\Drivers\IsGdDriverShortener;
910
use CodeOfDigital\LaravelUrlShortener\Drivers\ShorteStDriverShortener;
1011
use CodeOfDigital\LaravelUrlShortener\Drivers\TinyUrlDriverShortener;
@@ -119,6 +120,22 @@ protected function createCuttLyDriver(array $config)
119120
);
120121
}
121122

123+
/**
124+
* Create an instance of Firebase driver
125+
*
126+
* @param array $config
127+
* @return FirebaseDriverShortener
128+
*/
129+
protected function createFirebaseDriver(array $config)
130+
{
131+
return new FirebaseDriverShortener(
132+
$this->app->make(ClientInterface::class),
133+
Arr::get($config, 'token'),
134+
Arr::get($config, 'domain'),
135+
Arr::get($config, 'suffix')
136+
);
137+
}
138+
122139
/**
123140
* Get the default URL shortener driver
124141
*

0 commit comments

Comments
 (0)