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 LaraCrafts\UrlShortener\Contracts\CustomUrls as CustomUrlsContract; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class IsGdShortener extends RemoteShortener implements CustomUrlsContract |
12
|
|
|
{ |
13
|
|
|
use Concerns\CreatesCustomUrls; |
14
|
|
|
|
15
|
|
|
protected $client; |
16
|
|
|
protected $defaults; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a new Is.gd shortener. |
20
|
|
|
* |
21
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
22
|
|
|
* @param \Psr\Http\Message\UriInterface|string $baseUri |
23
|
|
|
* @param bool $statistics |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
48 |
|
public function __construct(ClientInterface $client, $baseUri, bool $statistics) |
27
|
|
|
{ |
28
|
48 |
|
$this->client = $client; |
29
|
48 |
|
$this->defaults = [ |
30
|
48 |
|
'allow_redirects' => false, |
31
|
48 |
|
'base_uri' => (string)$baseUri, |
32
|
|
|
'query' => [ |
33
|
48 |
|
'format' => 'simple', |
34
|
48 |
|
'logstats' => intval($statistics), |
35
|
|
|
], |
36
|
|
|
]; |
37
|
48 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
public function shortenAsync($url, array $options = []) |
43
|
|
|
{ |
44
|
|
|
$options = Arr::add(array_merge_recursive($this->defaults, $options), 'query.url', $url); |
45
|
|
|
$request = new Request('GET', '/create.php'); |
46
|
|
|
|
47
|
|
|
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) { |
48
|
|
|
return $response->getBody()->getContents(); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function shortenToAsync($url, string $identifier, array $options = []) |
56
|
|
|
{ |
57
|
|
|
return $this->shortenAsync($url, Arr::add($options, 'query.shorturl', $identifier)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|