1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\UrlShortener; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use Illuminate\Contracts\Foundation\Application; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use LaraCrafts\UrlShortener\Contracts\Factory as FactoryContract; |
12
|
|
|
use LaraCrafts\UrlShortener\Http\BitLyShortener; |
13
|
|
|
use LaraCrafts\UrlShortener\Http\FirebaseShortener; |
14
|
|
|
use LaraCrafts\UrlShortener\Http\IsGdShortener; |
15
|
|
|
use LaraCrafts\UrlShortener\Http\OuoIoShortener; |
16
|
|
|
use LaraCrafts\UrlShortener\Http\ShorteStShortener; |
17
|
|
|
use LaraCrafts\UrlShortener\Http\TinyUrlShortener; |
18
|
|
|
|
19
|
|
|
class UrlShortenerManager implements FactoryContract |
20
|
|
|
{ |
21
|
|
|
protected $app; |
22
|
|
|
protected $customCreators; |
23
|
|
|
protected $shorteners; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new URL shortener manager instance. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
264 |
|
public function __construct(Application $app) |
32
|
|
|
{ |
33
|
264 |
|
$this->app = $app; |
34
|
264 |
|
$this->customCreators = []; |
35
|
264 |
|
$this->shorteners = []; |
36
|
264 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Call a custom driver creator. |
40
|
|
|
* |
41
|
|
|
* @param array $config |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
24 |
|
protected function callCustomCreator(array $config) |
45
|
|
|
{ |
46
|
24 |
|
return $this->customCreators[$config['driver']]($this->app, $config); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create an instance of the Bit.ly driver. |
51
|
|
|
* |
52
|
|
|
* @param array $config |
53
|
|
|
* @return \LaraCrafts\UrlShortener\Http\BitLyShortener |
54
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
55
|
|
|
*/ |
56
|
24 |
|
protected function createBitLyDriver(array $config) |
57
|
|
|
{ |
58
|
24 |
|
return new BitLyShortener( |
59
|
24 |
|
$this->app->make(ClientInterface::class), |
60
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
61
|
24 |
|
Arr::get($config, 'domain', 'bit.ly') |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create an instance of the Firebase driver. |
67
|
|
|
* |
68
|
|
|
* @param array $config |
69
|
|
|
* @return \LaraCrafts\UrlShortener\Http\FirebaseShortener |
70
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
71
|
|
|
*/ |
72
|
24 |
|
protected function createFirebaseDriver(array $config) |
73
|
|
|
{ |
74
|
24 |
|
return new FirebaseShortener( |
75
|
24 |
|
$this->app->make(ClientInterface::class), |
76
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
77
|
24 |
|
Arr::get($config, 'prefix'), |
|
|
|
|
78
|
24 |
|
Arr::get($config, 'suffix') |
|
|
|
|
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create an instance of the Is.gd driver. |
84
|
|
|
* |
85
|
|
|
* @param array $config |
86
|
|
|
* @return \LaraCrafts\UrlShortener\Http\IsGdShortener |
87
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
88
|
|
|
*/ |
89
|
48 |
|
protected function createIsGdDriver(array $config) |
90
|
|
|
{ |
91
|
48 |
|
return new IsGdShortener( |
92
|
48 |
|
$this->app->make(ClientInterface::class), |
93
|
48 |
|
Arr::get($config, 'base_uri'), |
94
|
48 |
|
Arr::get($config, 'statistics') |
|
|
|
|
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create an instance of the Ouo.io driver. |
100
|
|
|
* |
101
|
|
|
* @param array $config |
102
|
|
|
* @return \LaraCrafts\UrlShortener\Http\OuoIoShortener |
103
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
104
|
|
|
*/ |
105
|
24 |
|
protected function createOuoIoDriver(array $config) |
106
|
|
|
{ |
107
|
24 |
|
return new OuoIoShortener( |
108
|
24 |
|
$this->app->make(ClientInterface::class), |
109
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create an instance of the Shorte.st driver. |
115
|
|
|
* |
116
|
|
|
* @param array $config |
117
|
|
|
* @return \LaraCrafts\UrlShortener\Http\ShorteStShortener |
118
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
119
|
|
|
*/ |
120
|
24 |
|
protected function createShorteStDriver(array $config) |
121
|
|
|
{ |
122
|
24 |
|
return new ShorteStShortener( |
123
|
24 |
|
$this->app->make(ClientInterface::class), |
124
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Create an instance of the TinyURL driver. |
130
|
|
|
* |
131
|
|
|
* @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener |
132
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
133
|
|
|
*/ |
134
|
48 |
|
protected function createTinyUrlDriver() |
135
|
|
|
{ |
136
|
48 |
|
return new TinyUrlShortener($this->app->make(ClientInterface::class)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get a URL shortener driver instance. |
141
|
|
|
* |
142
|
|
|
* @param string|null $name |
143
|
|
|
* @return \LaraCrafts\UrlShortener\Contracts\Shortener |
144
|
|
|
*/ |
145
|
24 |
|
public function driver(string $name = null) |
146
|
|
|
{ |
147
|
24 |
|
return $this->shortener($name); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Register a custom driver creator closure. |
152
|
|
|
* |
153
|
|
|
* @param string $name |
154
|
|
|
* @param \Closure $callback |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
24 |
|
public function extend(string $name, Closure $callback) |
158
|
|
|
{ |
159
|
24 |
|
$this->customCreators[$name] = $callback->bindTo($this, $this); |
160
|
|
|
|
161
|
24 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the default URL shortener driver name. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
48 |
|
public function getDefaultDriver() |
170
|
|
|
{ |
171
|
48 |
|
return $this->app['config']['url-shortener.default']; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the URL shortener configuration. |
176
|
|
|
* |
177
|
|
|
* @param string $name |
178
|
|
|
* @return array|null |
179
|
|
|
*/ |
180
|
216 |
|
protected function getShortenerConfig(string $name) |
181
|
|
|
{ |
182
|
216 |
|
return $this->app['config']["url-shortener.shorteners.$name"]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Resolve the given URL shortener. |
187
|
|
|
* |
188
|
|
|
* @param string $name |
189
|
|
|
* @return \LaraCrafts\UrlShortener\Contracts\Shortener |
190
|
|
|
*/ |
191
|
216 |
|
protected function resolve(string $name) |
192
|
|
|
{ |
193
|
216 |
|
$config = $this->getShortenerConfig($name); |
194
|
|
|
|
195
|
216 |
|
if (is_null($config) || !array_key_exists('driver', $config)) { |
196
|
|
|
throw new InvalidArgumentException("URL shortener [{$name}] is not defined"); |
197
|
|
|
} |
198
|
|
|
|
199
|
216 |
|
if (array_key_exists($config['driver'], $this->customCreators)) { |
200
|
24 |
|
return $this->callCustomCreator($config); |
201
|
|
|
} |
202
|
|
|
|
203
|
192 |
|
$driverMethod = 'create' . Str::studly($config['driver']) . 'Driver'; |
204
|
|
|
|
205
|
192 |
|
if (method_exists($this, $driverMethod)) { |
206
|
192 |
|
return $this->$driverMethod($config); |
207
|
|
|
} |
208
|
|
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported"); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set the default URL shortener driver name. |
213
|
|
|
* |
214
|
|
|
* @param string $name |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
24 |
|
public function setDefaultDriver(string $name) |
218
|
|
|
{ |
219
|
24 |
|
$this->app['config']['url-shortener.default'] = $name; |
220
|
|
|
|
221
|
24 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritDoc} |
226
|
|
|
*/ |
227
|
216 |
|
public function shortener(string $name = null) |
228
|
|
|
{ |
229
|
216 |
|
$name = $name ?: $this->getDefaultDriver(); |
230
|
|
|
|
231
|
216 |
|
if (array_key_exists($name, $this->shorteners)) { |
232
|
|
|
return $this->shorteners[$name]; |
233
|
|
|
} |
234
|
|
|
|
235
|
216 |
|
return $this->shorteners[$name] = $this->resolve($name); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|