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
|
|
|
* Create an instance of the Bit.ly driver. |
24
|
|
|
* |
25
|
|
|
* @return \LaraCrafts\UrlShortener\Http\BitLyShortener |
26
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
27
|
|
|
*/ |
28
|
24 |
|
protected function createBitLyDriver() |
29
|
|
|
{ |
30
|
24 |
|
$config = $this->getDriverConfig('bit_ly'); |
31
|
|
|
|
32
|
24 |
|
return new BitLyShortener( |
33
|
24 |
|
$this->app->make(ClientInterface::class), |
34
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
35
|
24 |
|
Arr::get($config, 'domain', 'bit.ly') |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
168 |
|
protected function createDriver($driver) |
43
|
|
|
{ |
44
|
|
|
# This fixes backwards compatibility issues with this function |
45
|
168 |
|
if (method_exists($this, $method = 'create' . Str::studly($driver) . 'Driver')) { |
46
|
168 |
|
return $this->$method(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return parent::createDriver($driver); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create an instance of the Firebase driver. |
54
|
|
|
* |
55
|
|
|
* @return \LaraCrafts\UrlShortener\Http\FirebaseShortener |
56
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
57
|
|
|
*/ |
58
|
24 |
|
protected function createFirebaseDriver() |
59
|
|
|
{ |
60
|
24 |
|
$config = $this->getDriverConfig('firebase'); |
61
|
|
|
|
62
|
24 |
|
return new FirebaseShortener( |
63
|
24 |
|
$this->app->make(ClientInterface::class), |
64
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
65
|
24 |
|
Arr::get($config, 'prefix'), |
|
|
|
|
66
|
24 |
|
Arr::get($config, 'suffix') |
|
|
|
|
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create an instance of the Is.gd driver. |
72
|
|
|
* |
73
|
|
|
* @return \LaraCrafts\UrlShortener\Http\IsGdShortener |
74
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
75
|
|
|
*/ |
76
|
24 |
|
protected function createIsGdDriver() |
77
|
|
|
{ |
78
|
24 |
|
$config = $this->getDriverConfig('is_gd'); |
79
|
|
|
|
80
|
24 |
|
return new IsGdShortener( |
81
|
24 |
|
$this->app->make(ClientInterface::class), |
82
|
24 |
|
Arr::get($config, 'link_previews'), |
|
|
|
|
83
|
24 |
|
Arr::get($config, 'statistics') |
|
|
|
|
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create an instance of the Ouo.io driver. |
89
|
|
|
* |
90
|
|
|
* @return \LaraCrafts\UrlShortener\Http\OuoIoShortener |
91
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
92
|
|
|
*/ |
93
|
24 |
|
protected function createOuoIoDriver() |
94
|
|
|
{ |
95
|
24 |
|
$config = $this->getDriverConfig('ouo_io'); |
96
|
|
|
|
97
|
24 |
|
return new OuoIoShortener( |
98
|
24 |
|
$this->app->make(ClientInterface::class), |
99
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create an instance of the Shorte.st driver. |
105
|
|
|
* |
106
|
|
|
* @return \LaraCrafts\UrlShortener\Http\ShorteStShortener |
107
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
108
|
|
|
*/ |
109
|
24 |
|
protected function createShorteStDriver() |
110
|
|
|
{ |
111
|
24 |
|
$config = $this->getDriverConfig('shorte_st'); |
112
|
|
|
|
113
|
24 |
|
return new ShorteStShortener( |
114
|
24 |
|
$this->app->make(ClientInterface::class), |
115
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Create an instance of the TinyURL driver. |
121
|
|
|
* |
122
|
|
|
* @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener |
123
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
124
|
|
|
*/ |
125
|
48 |
|
protected function createTinyUrlDriver() |
126
|
|
|
{ |
127
|
48 |
|
return new TinyUrlShortener($this->app->make(ClientInterface::class)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
24 |
|
public function getDefaultDriver() |
134
|
|
|
{ |
135
|
24 |
|
return $this->app['config']['url-shortener.default']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the driver configuration. |
140
|
|
|
* |
141
|
|
|
* @param string $name |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
120 |
|
protected function getDriverConfig(string $name) |
145
|
|
|
{ |
146
|
120 |
|
return $this->app['config']["url-shortener.drivers.$name"] ?: []; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|