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\OuoIoShortener; |
12
|
|
|
use LaraCrafts\UrlShortener\Http\ShorteStShortener; |
13
|
|
|
use LaraCrafts\UrlShortener\Http\TinyUrlShortener; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @mixin \LaraCrafts\UrlShortener\Contracts\Shortener |
17
|
|
|
*/ |
18
|
|
|
class UrlShortenerManager extends Manager implements Factory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Create an instance of the Bit.ly driver. |
22
|
|
|
* |
23
|
|
|
* @return \LaraCrafts\UrlShortener\Http\BitLyShortener |
24
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
25
|
|
|
*/ |
26
|
24 |
|
protected function createBitLyDriver() |
27
|
|
|
{ |
28
|
24 |
|
$config = $this->getDriverConfig('bit_ly'); |
29
|
|
|
|
30
|
24 |
|
return new BitLyShortener( |
31
|
24 |
|
$this->app->make(ClientInterface::class), |
32
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
33
|
24 |
|
Arr::get($config, 'domain', 'bit.ly') |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
120 |
|
protected function createDriver($driver) |
41
|
|
|
{ |
42
|
|
|
# This fixes backwards compatibility issues with this function |
43
|
120 |
|
if (method_exists($this, $method = 'create' . Str::studly($driver) . 'Driver')) { |
44
|
120 |
|
return $this->$method(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return parent::createDriver($driver); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create an instance of the Ouo.io driver. |
52
|
|
|
* |
53
|
|
|
* @return \LaraCrafts\UrlShortener\Http\OuoIoShortener |
54
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
55
|
|
|
*/ |
56
|
24 |
|
protected function createOuoIoDriver() |
57
|
|
|
{ |
58
|
24 |
|
$config = $this->getDriverConfig('ouo_io'); |
59
|
|
|
|
60
|
24 |
|
return new OuoIoShortener( |
61
|
24 |
|
$this->app->make(ClientInterface::class), |
62
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create an instance of the Shorte.st driver. |
68
|
|
|
* |
69
|
|
|
* @return \LaraCrafts\UrlShortener\Http\ShorteStShortener |
70
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
71
|
|
|
*/ |
72
|
24 |
|
protected function createShorteStDriver() |
73
|
|
|
{ |
74
|
24 |
|
$config = $this->getDriverConfig('shorte_st'); |
75
|
|
|
|
76
|
24 |
|
return new ShorteStShortener( |
77
|
24 |
|
$this->app->make(ClientInterface::class), |
78
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create an instance of the TinyURL driver. |
84
|
|
|
* |
85
|
|
|
* @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener |
86
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
87
|
|
|
*/ |
88
|
48 |
|
protected function createTinyUrlDriver() |
89
|
|
|
{ |
90
|
48 |
|
return new TinyUrlShortener($this->app->make(ClientInterface::class)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
24 |
|
public function getDefaultDriver() |
97
|
|
|
{ |
98
|
24 |
|
return $this->app['config']['url-shortener.default']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the driver configuration. |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
72 |
|
protected function getDriverConfig(string $name) |
108
|
|
|
{ |
109
|
72 |
|
return $this->app['config']["url-shortener.drivers.$name"] ?: []; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|