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 InvalidArgumentException; |
10
|
|
|
use LaraCrafts\UrlShortener\Contracts\Factory; |
11
|
|
|
use LaraCrafts\UrlShortener\Http\BitLyShortener; |
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
|
|
|
*/ |
25
|
29 |
|
protected function createBitLyDriver() |
26
|
|
|
{ |
27
|
29 |
|
$config = $this->getDriverConfig('bit_ly'); |
28
|
|
|
|
29
|
29 |
|
return new BitLyShortener( |
30
|
29 |
|
$this->app->make(ClientInterface::class), |
31
|
29 |
|
Arr::get($config, 'token'), |
|
|
|
|
32
|
29 |
|
Arr::get($config, 'domain', 'bit.ly') |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
87 |
|
protected function createDriver($driver) |
40
|
|
|
{ |
41
|
|
|
# We overwrite this function with the Laravel 5.8 variant |
42
|
|
|
# because it isn't backwards compatible across supported versions |
43
|
|
|
|
44
|
87 |
|
if (isset($this->customCreators[$driver])) { |
45
|
|
|
return $this->callCustomCreator($driver); |
46
|
|
|
} else { |
47
|
87 |
|
$method = 'create' . Str::studly($driver) . 'Driver'; |
48
|
87 |
|
if (method_exists($this, $method)) { |
49
|
87 |
|
return $this->$method(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new InvalidArgumentException("Driver [$driver] not supported."); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a new instance of the Shorte.st driver. |
58
|
|
|
* |
59
|
|
|
* @return \LaraCrafts\UrlShortener\Http\ShorteStShortener |
60
|
|
|
*/ |
61
|
29 |
|
protected function createShorteStDriver() |
62
|
|
|
{ |
63
|
29 |
|
$config = $this->getDriverConfig('shorte_st'); |
64
|
|
|
|
65
|
29 |
|
return new ShorteStShortener( |
66
|
29 |
|
$this->app->make(ClientInterface::class), |
67
|
29 |
|
Arr::get($config, 'token') |
|
|
|
|
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create an instance of the TinyURL driver. |
73
|
|
|
* |
74
|
|
|
* @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener |
75
|
|
|
*/ |
76
|
29 |
|
protected function createTinyUrlDriver() |
77
|
|
|
{ |
78
|
29 |
|
return new TinyUrlShortener($this->app->make(ClientInterface::class)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
|
|
public function getDefaultDriver() |
85
|
|
|
{ |
86
|
|
|
return $this->app['config']['url-shortener.default']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the driver configuration. |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
58 |
|
protected function getDriverConfig(string $name) |
96
|
|
|
{ |
97
|
58 |
|
return $this->app['config']["url-shortener.drivers.$name"] ?: []; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|