1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\UrlShortener; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Manager; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use LaraCrafts\UrlShortener\Contracts\Factory; |
10
|
|
|
use LaraCrafts\UrlShortener\Http\BitLyShortener; |
11
|
|
|
use LaraCrafts\UrlShortener\Http\FirebaseShortener; |
12
|
|
|
use LaraCrafts\UrlShortener\Http\IsGdShortener; |
13
|
|
|
use LaraCrafts\UrlShortener\Http\OuoIoShortener; |
14
|
|
|
use LaraCrafts\UrlShortener\Http\ShorteStShortener; |
15
|
|
|
use LaraCrafts\UrlShortener\Http\TinyUrlShortener; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @mixin \LaraCrafts\UrlShortener\Contracts\Shortener |
19
|
|
|
*/ |
20
|
|
|
class UrlShortenerManager extends Manager implements Factory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Shorten the given URL using the given driver. |
24
|
|
|
* |
25
|
|
|
* @param string $driver |
26
|
|
|
* @param string $url |
27
|
|
|
* @param array $options |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function shortenUsing(string $driver, string $url, array $options = []) |
32
|
|
|
{ |
33
|
|
|
return $this->driver($driver)->shorten($url, $options); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Shorten the given URL asynchronously using the given driver. |
38
|
|
|
* |
39
|
|
|
* @param string $driver |
40
|
|
|
* @param string $url |
41
|
|
|
* @param array $options |
42
|
|
|
* |
43
|
|
|
* @return \GuzzleHttp\Promise\PromisorInterface |
44
|
|
|
*/ |
45
|
|
|
public function shortenAsyncUsing(string $driver, string $url, array $options = []) |
46
|
|
|
{ |
47
|
|
|
return $this->driver($driver)->shortenAsync($url, $options); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create an instance of the Bit.ly driver. |
52
|
|
|
* |
53
|
|
|
* @return \LaraCrafts\UrlShortener\Http\BitLyShortener |
54
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
55
|
|
|
*/ |
56
|
24 |
|
protected function createBitLyDriver() |
57
|
|
|
{ |
58
|
24 |
|
$config = $this->getDriverConfig('bit_ly'); |
59
|
|
|
|
60
|
24 |
|
return new BitLyShortener( |
61
|
24 |
|
$this->app->make(ClientInterface::class), |
62
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
63
|
24 |
|
Arr::get($config, 'domain', 'bit.ly') |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
*/ |
70
|
168 |
|
protected function createDriver($driver) |
71
|
|
|
{ |
72
|
|
|
# This fixes backwards compatibility issues with this function |
73
|
168 |
|
if (method_exists($this, $method = 'create' . Str::studly($driver) . 'Driver')) { |
74
|
168 |
|
return $this->$method(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return parent::createDriver($driver); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create an instance of the Firebase driver. |
82
|
|
|
* |
83
|
|
|
* @return \LaraCrafts\UrlShortener\Http\FirebaseShortener |
84
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
85
|
|
|
*/ |
86
|
24 |
|
protected function createFirebaseDriver() |
87
|
|
|
{ |
88
|
24 |
|
$config = $this->getDriverConfig('firebase'); |
89
|
|
|
|
90
|
24 |
|
return new FirebaseShortener( |
91
|
24 |
|
$this->app->make(ClientInterface::class), |
92
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
93
|
24 |
|
Arr::get($config, 'prefix'), |
|
|
|
|
94
|
24 |
|
Arr::get($config, 'suffix') |
|
|
|
|
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create an instance of the Is.gd driver. |
100
|
|
|
* |
101
|
|
|
* @return \LaraCrafts\UrlShortener\Http\IsGdShortener |
102
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
103
|
|
|
*/ |
104
|
24 |
|
protected function createIsGdDriver() |
105
|
|
|
{ |
106
|
24 |
|
$config = $this->getDriverConfig('is_gd'); |
107
|
|
|
|
108
|
24 |
|
return new IsGdShortener( |
109
|
24 |
|
$this->app->make(ClientInterface::class), |
110
|
24 |
|
Arr::get($config, 'link_previews'), |
|
|
|
|
111
|
24 |
|
Arr::get($config, 'statistics') |
|
|
|
|
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create an instance of the Ouo.io driver. |
117
|
|
|
* |
118
|
|
|
* @return \LaraCrafts\UrlShortener\Http\OuoIoShortener |
119
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
120
|
|
|
*/ |
121
|
24 |
|
protected function createOuoIoDriver() |
122
|
|
|
{ |
123
|
24 |
|
$config = $this->getDriverConfig('ouo_io'); |
124
|
|
|
|
125
|
24 |
|
return new OuoIoShortener( |
126
|
24 |
|
$this->app->make(ClientInterface::class), |
127
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Create an instance of the Shorte.st driver. |
133
|
|
|
* |
134
|
|
|
* @return \LaraCrafts\UrlShortener\Http\ShorteStShortener |
135
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
136
|
|
|
*/ |
137
|
24 |
|
protected function createShorteStDriver() |
138
|
|
|
{ |
139
|
24 |
|
$config = $this->getDriverConfig('shorte_st'); |
140
|
|
|
|
141
|
24 |
|
return new ShorteStShortener( |
142
|
24 |
|
$this->app->make(ClientInterface::class), |
143
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Create an instance of the TinyURL driver. |
149
|
|
|
* |
150
|
|
|
* @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener |
151
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
152
|
|
|
*/ |
153
|
48 |
|
protected function createTinyUrlDriver() |
154
|
|
|
{ |
155
|
48 |
|
return new TinyUrlShortener($this->app->make(ClientInterface::class)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
24 |
|
public function getDefaultDriver() |
162
|
|
|
{ |
163
|
24 |
|
return $this->app['config']['url-shortener.default']; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the driver configuration. |
168
|
|
|
* |
169
|
|
|
* @param string $name |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
120 |
|
protected function getDriverConfig(string $name) |
173
|
|
|
{ |
174
|
120 |
|
return $this->app['config']["url-shortener.drivers.$name"] ?: []; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|