|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraCrafts\UrlShortener\Http; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
|
6
|
|
|
use GuzzleHttp\Psr7\Request; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use function GuzzleHttp\json_decode; |
|
10
|
|
|
|
|
11
|
|
|
class FirebaseShortener extends RemoteShortener |
|
12
|
|
|
{ |
|
13
|
|
|
protected $client; |
|
14
|
|
|
protected $defaults; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Create a new Shorte.st shortener. |
|
18
|
|
|
* |
|
19
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
20
|
|
|
* @param string $token |
|
21
|
|
|
* @param string $domain |
|
22
|
|
|
* @param string $suffix |
|
23
|
|
|
*/ |
|
24
|
120 |
|
public function __construct(ClientInterface $client, string $token, string $domain, string $suffix) |
|
25
|
|
|
{ |
|
26
|
120 |
|
$this->client = $client; |
|
27
|
120 |
|
$this->defaults = [ |
|
28
|
120 |
|
'allow_redirects' => false, |
|
29
|
120 |
|
'base_uri' => 'https://firebasedynamiclinks.googleapis.com', |
|
30
|
|
|
'headers' => [ |
|
31
|
|
|
'Content-Type' => 'application/json', |
|
32
|
|
|
'Accept' => 'application/json', |
|
33
|
|
|
], |
|
34
|
|
|
'query' => [ |
|
35
|
120 |
|
'key' => $token, |
|
36
|
|
|
], |
|
37
|
|
|
'json' => [ |
|
38
|
|
|
'dynamicLinkInfo' => [ |
|
39
|
120 |
|
'domainUriPrefix' => $domain, |
|
40
|
|
|
], |
|
41
|
|
|
'suffix' => [ |
|
42
|
120 |
|
"option" => $suffix, |
|
43
|
|
|
], |
|
44
|
|
|
], |
|
45
|
|
|
]; |
|
46
|
120 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Shorten the given URL asynchronously. |
|
50
|
|
|
* |
|
51
|
|
|
* @param \Psr\Http\Message\UriInterface|string $url |
|
52
|
|
|
* @param array $options |
|
53
|
|
|
* @return \GuzzleHttp\Promise\PromiseInterface |
|
54
|
|
|
*/ |
|
55
|
96 |
|
public function shortenAsync($url, array $options = []) |
|
56
|
|
|
{ |
|
57
|
96 |
|
$options = array_merge(Arr::add($this->defaults, 'json.dynamicLinkInfo.link', $url), $options); |
|
58
|
96 |
|
$request = new Request('POST', '/v1/shortLinks'); |
|
59
|
|
|
|
|
60
|
60 |
|
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) { |
|
61
|
96 |
|
return json_decode($response->getBody()->getContents())->shortLink; |
|
62
|
96 |
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|